diff --git a/back/jwtutils/jwt.go b/back/jwtutils/jwt.go index 0f492fd..514dcd9 100644 --- a/back/jwtutils/jwt.go +++ b/back/jwtutils/jwt.go @@ -1,13 +1,25 @@ package jwtutils import ( + "os" + + "sync" + "github.com/golang-jwt/jwt/v5" + "github.com/joho/godotenv" ) -var JWTSecret = os.GetEnv("JWTSecret") +var ( + JWTSecret []byte + once sync.Once +) func ValidarJWT(tokenString string) (jwt.MapClaims, error) { - token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + once.Do(func() { + godotenv.Load() + JWTSecret = []byte(os.Getenv("JWTString")) + }) + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (any, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, jwt.ErrSignatureInvalid } diff --git a/front/src/lib/hooks/login.ts b/front/src/lib/hooks/login.ts index 59f2b21..d861e44 100644 --- a/front/src/lib/hooks/login.ts +++ b/front/src/lib/hooks/login.ts @@ -6,7 +6,7 @@ import { setCurrentUser } from './user'; async function handleResponse(response: Response): Promise { if (!response.ok) { const error = await response.json().catch(() => ({ message: 'Error desconocido' })); - throw new Error(error.message || 'Error en la petición'); + throw new Error(error.error || 'Error en la petición'); } return response.json(); }