Open tomriddle1234 opened 6 years ago
@tomriddle1234 you can define own LoginResponse function. See
and the following is default behavior:
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"
});
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 douserRoutes.POST("/login", jwtMiddleware.LoginHandler, redirectToIndexPage)
and my redirectToIndexPage,
But this is not working and gin gives me like,
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 ?