'Use reactjs to get login info from golang session
I am making a backend using go, frontend using react.
Now I build a login page and use session to remember user login status.
I originally used postman to test the golang func, and now I want to write it in reactjs, but I found that I can't just use get, and I need to add cookie information
In the code field of postman, it is found that after sending the command, it will automatically add header: cookie data, and when using curl, adding "-v" option will also return setCookie information, but I have no idea how to get the cookie information on the client side.
Here is my golang func, can somebody give me suggestion about js cookie?
func main() {
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/setSession", setUser)
getSession(r)
}
func setUser(c *gin.Context) {
session := sessions.Default(c)
session.Set("currentUser", *db)
session.Options(sessions.Options{
MaxAge: 50,
})
session.Save()
c.String(200, "session setup: %v", session.Get("currentUser"))
}
func getSession(r *gin.Engine) {
r.GET("/Hey", api.Cookie(), func(c *gin.Context) {
userInfo := getCurrentUser(c)
if userInfo.Username == "admin" {
c.String(http.StatusOK, "Hello "+userInfo.Username)
} else {
c.String(http.StatusOK, "Failed")
}
})
}
func getCurrentUser(c *gin.Context) (userInfo User) {
session := sessions.Default(c)
userInfo = session.Get("nowUser").(User)
return
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
