简体   繁体   中英

Calling Token Values in Other Functions with JWT Token in Golang

I am working on a project using golang. The frameworks I used are gin,gorm, jwt, crypto. When I used c.GET in user controllers page which I define login and signup function as well as token it works but when I try to use it on other pages it returns nil.How can I make this work in other pages too?

Here is my validate function which works;

func Validate(c *gin.Context) {
    user, _ := c.Get("user")
    un := user.(Models.User).UserName
    c.JSON(http.StatusOK, gin.H{
        "message":  user,
        "username": un,
    })
}

Here is my call on the other page which returns nil;

userck, _ := c.Get("user")
    i := userck.(Models.User).ID

Thank you for your help.

Thank you for your time, I am all new to backend programming. I figure out that I needed to add middleware to all roots for validation and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM