28 lines
505 B
Go
28 lines
505 B
Go
|
|
package models
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/golang-jwt/jwt/v5"
|
||
|
|
)
|
||
|
|
|
||
|
|
var jwtKey = []byte("your-secret-key") //TODO: Move to env/config
|
||
|
|
|
||
|
|
|
||
|
|
func ExtractClaims(tokenStr string) (*Claims, error) {
|
||
|
|
claims := &Claims{}
|
||
|
|
|
||
|
|
token, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) {
|
||
|
|
return jwtKey, nil
|
||
|
|
})
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if !token.Valid {
|
||
|
|
return nil, fmt.Errorf("invalid token")
|
||
|
|
}
|
||
|
|
|
||
|
|
return claims, nil
|
||
|
|
}
|