enben94
enben94

Reputation: 21

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.

Upvotes: 0

Views: 187

Answers (1)

enben94
enben94

Reputation: 21

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.

Upvotes: 1

Related Questions