how do i fetch the jwt payload data from a cookie created with the gofiber golang framework?

finally figured it out

func GetAccessDetails(c *fiber.Ctx) (*AccessDetails, error) {
    ad := &AccessDetails{}

    cookie := c.Cookies("access_token")

    var err error
    token, err := jwt.Parse(cookie, func(token *jwt.Token) (interface{}, error) {
        return []byte(os.Getenv("ACCESS_SECRET")), nil
    })

    if err != nil {
        return nil, err
    }

    payload := token.Claims.(jwt.MapClaims)

    ad.Email = payload["sub"].(string)
    ad.AccessUuid = payload["access_uuid"].(string)

    return ad, nil
}

so since i used mapClaims to create the token, so i can grab it with this

token, err := jwt.Parse(cookie, func(token *jwt.Token) (interface{}, error) {
        return []byte(os.Getenv("ACCESS_SECRET")), nil
    })

and then assigns the elements of ad := &AccessDetails{} as follows

    payload := token.Claims.(jwt.MapClaims)

    ad.Email = payload["sub"].(string)
    ad.AccessUuid = payload["access_uuid"].(string)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top