Gin: Exclude middleware from some routes.

Created on 9 Oct 2017  路  2Comments  路  Source: gin-gonic/gin

Hello I am working on a rest api using golang with gin as a router. Also I am using jwt authentication process for the api. I saved the token to the encrypted session which indicates whether or not they are logged in. I would like to use a middleware to verify whether or not this token exists, and then kick the user back to the login page if it does not. I want to exclude some routes from using the authentication middleware. I have some of my routes in which I don't want the authentication middleware. Can anybody know how I can achieve this?
Right now I have tried the following code under middleware:

if c.Request.URL.Path != "/api/v1/login" && c.Request.URL.Path != "/api/v1/signup" && c.Request.URL.Path != "/api/v1/signup"{
// authentication process
}
I have number of routes and all are using the same group "/api/v1/".
What is the best way to ignore the authentication middleware from some of the routes.

Most helpful comment

@Swatiparbhakar you can achieve that using Groups.
i hope this code helps you :

   r := gin.New()

// CORS
r.Use(middlewares.Cors())
r.Use(ginrus.Ginrus(loggers.LoggerImpl, time.RFC3339, true))

// Recovery middleware
r.Use(gin.Recovery())

    /* ---------------------------  Public routes  --------------------------- */

public := r.Group("/api/v1")
public.Use(middlewares.DbCloneSession(ds))
public.Use(gzip.Gzip(gzip.DefaultCompression))

public.POST("/login", controllers.Login)

/* ---------------------------  Private routes  --------------------------- */

private := r.Group("/api/v1")
private.Use(middlewares.DbCloneSession(ds))
    // here im using JWT
private.Use(middlewares.Jwt())
private.Use(gzip.Gzip(gzip.DefaultCompression))

All 2 comments

@Swatiparbhakar you can achieve that using Groups.
i hope this code helps you :

   r := gin.New()

// CORS
r.Use(middlewares.Cors())
r.Use(ginrus.Ginrus(loggers.LoggerImpl, time.RFC3339, true))

// Recovery middleware
r.Use(gin.Recovery())

    /* ---------------------------  Public routes  --------------------------- */

public := r.Group("/api/v1")
public.Use(middlewares.DbCloneSession(ds))
public.Use(gzip.Gzip(gzip.DefaultCompression))

public.POST("/login", controllers.Login)

/* ---------------------------  Private routes  --------------------------- */

private := r.Group("/api/v1")
private.Use(middlewares.DbCloneSession(ds))
    // here im using JWT
private.Use(middlewares.Jwt())
private.Use(gzip.Gzip(gzip.DefaultCompression))

@hellorachid Thank you for your reply. Your solution is perfect for my requirement.
Thanks!!!
I want to ask one more thing: Is it required to use cors middleware with the routes?
What are there benefits in the api's?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olegsobchuk picture olegsobchuk  路  3Comments

mdnight picture mdnight  路  3Comments

wangcn picture wangcn  路  3Comments

Bloomca picture Bloomca  路  3Comments

ccaza picture ccaza  路  3Comments