package routes
import (
"alexapp.pck.com/handler"
"fmt"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"net/http"
)
var tokenAuth *jwtauth.JWTAuth
//InitIndexRoutes function
func InitRestrictedRoutes(c *chi.Mux, h *handler.Handler) {
// Protected routes
c.Group(func(r chi.Router) {
// Seek, verify and validate JWT tokens
r.Use(jwtauth.Verifier(tokenAuth))
// Handle valid / invalid tokens. In this example, we use
// the provided authenticator middleware, but you can write your
// own very easily, look at the Authenticator method in jwtauth.go
// and tweak it, its not scary.
r.Use(jwtauth.Authenticator)
r.Get("/restricted", func(w http.ResponseWriter, r *http.Request) {
_, claims, _ := jwtauth.FromContext(r.Context())
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["user_id"])))
})
})
}
It seems that for a reason the var tokenAuth *jwtauth.JWTAuth is of value nil in the actual jwtauth.Verifier(tokenAuth) code.
So this gives me the error :
It seems that for a reason the var tokenAuth *jwtauth.JWTAuth is of value nil in the actual jwtauth.Verifier(tokenAuth) code. So this gives me the error :
In the actual example, you have: var tokenAuth *jwtauth.JWTAuth
//And you assign a value to it in the init() function : // tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
In my case, I already have the token generated and saved somewhere else and all I want to do is use that token.
So when the code arrives in my initRestrictedFunction , what should the value for : tokenAuth be ? On the line: r.Use(jwtauth.Verifier(tokenAuth))
Thanks