Panic error: interface conversion jwt.claims is jwt.mapclaims

I am getting the following error when using my api in Postman, the error is that it tells me that the conversion of jwt.claims is jwt.mapClaims, the problem is that I do not know how to make the change … my code is … .

if token.Valid {
ctx: = context.WithValue (r.Context (), "user", token.Claims. (* models.Claim) .User)
next (w, r.WithContext (ctx))
}

the error is probably in token.Claims. (* models.Claim) .User), but I do not know how to convert it to mapClaims.

someone who can give me some help? How to implement it?

Greetings.

Hi. Could you post the panic text?

The panic text it’s only that, I use jwt-go, negroni, and one package that I create by myself. So, I can send the panic text, is not a problem, I will let you here. Wait a moment.

It expects a pointer to jwt.mapClaims. that is *jwt.mapClaims but you gives it just a jwt.mapClaims. read up on pointers here: https://www.golang-book.com/books/intro/8

I understand that, but I don’t know how must I pass the claims with *jwt.MapClaims as Pointer, I already use the *jwt.mapClaims but is not working. Maybe I am using in bad way.

Could you help me?

I’ve solve that panic error, but now I have another one.

When I try to do something with my api /api/comments/

I getting bad request , the before code to do that is:

if token.Valid {
ctx := context.WithValue(r.Context(), “user”, token.Claims.(*models.Claim).User)
next(w, r.WithContext(ctx))
} else {
m.Code = http.StatusUnauthorized
m.Message = “Su token no es válido”
commons.DisplayMessage(w, m)
}

and the new, where I solve the error it’s:

if claims , ok := token.Claims.(*models.Claim); ok && token.Valid {
ctx := context.WithValue(r.Context(), “user”, *claims)
next(w,r.WithContext(ctx))
} else {
m.Code = http.StatusBadRequest
m.Message = “Su token no es válido”
commons.DisplayMessage(w,m)
}

but I don’t get the response and that is because the token it’s not valide, but I use the right token.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.