appleboy / gin-jwt

JWT Middleware for Gin framework
MIT License
2.77k stars 389 forks source link

How to redirect after jwtMiddleware.LoginHandler ? #160

Open tomriddle1234 opened 6 years ago

tomriddle1234 commented 6 years ago

I have a router like this, the jwtMiddleware.LoginHandler can authenticate login fine, however I wish to redirect to home page after. I intended to do userRoutes.POST("/login", jwtMiddleware.LoginHandler, redirectToIndexPage)

and my redirectToIndexPage,

func redirectToIndexPage(c *gin.Context) {
    c.Request.URL.Path = "/"
    c.Request.Method = "GET"
    router.HandleContext(c)
}

But this is not working and gin gives me like,

[GIN] 2018/08/31 - 23:01:48 | 200 |     821.205µs |       127.0.0.1 | GET      /u/login
[GIN] 2018/08/31 - 23:01:48 | 200 |   30.109852ms |       127.0.0.1 | GET      /u/login
[GIN] 2018/08/31 - 23:01:53 | 200 |     605.753µs |       127.0.0.1 | GET      /
[GIN] 2018/08/31 - 23:01:53 | 200 |      655.98µs |       127.0.0.1 | GET      /
[GIN] 2018/08/31 - 23:01:53 | 200 |  198.896143ms |       127.0.0.1 | GET      /u/login
[GIN] 2018/08/31 - 23:01:53 | 200 |  198.902927ms |       127.0.0.1 | GET      /u/login

As you can see, it seems redirect happens before login gets return, it was slow because password hashing takes time. And the login page in browser doesn't redirect to "/".

I'm new to Go and Gin and your nice gin-jwt,what should I do if I want to redirect ? Update My stupidity, I can do redirect at the front end once received token ! Can this kind of redirect be done at back end ?

appleboy commented 6 years ago

@tomriddle1234 you can define own LoginResponse function. See

https://github.com/appleboy/gin-jwt/blob/d31681b963c5bd248786b5974e4e29466f5369aa/auth_jwt.go#L65-L66

and the following is default behavior:

https://github.com/appleboy/gin-jwt/blob/d31681b963c5bd248786b5974e4e29466f5369aa/auth_jwt.go#L268-L276

tomriddle1234 commented 6 years ago

I missed that in doc, thanks.

But I still have a noob question about this.

In such my own LoginResponse function, if I do redirect, why does it just append some html to current address ?

func redirectToIndexPage(c *gin.Context) {
    c.Request.URL.Path = "/"
    router.HandleContext(c)
}

This will append "/" html aka the index page, after the json string of the default jwtMiddleware.LoginHandler as I saw it in the browser develop tool.

So I'm guessing that Gin is pipe lining all handler function in one response ? If I wish to firstly fire a response of json, then fire a backend redirect, how should I do it ?

Here is how I did it with frontend help.

$.ajax({  
      type:"POST",
      url:url,
      data:JSON.stringify(jsondata),
      success:function(data){
        console.log("Successfully send json post to login.")
        console.log(data)
        if (data.hasOwnProperty('token')){
          //redirect once received token
          window.location.replace("/");
        }
      },
      statusCode:{
        401:function (){
          console.log("failed");
          $("#fail-alert").fadeTo(2000, 500).slideUp(500, function(){
            $("#fail-alert").slideUp(500);
          });
        },
      },
      dataType:"json",
      contentType:"application/json"
    });