summaryrefslogtreecommitdiff
path: root/api/auth/auth.go
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-04-08 14:37:33 +0200
committerSanto Cariotti <santo@dcariotti.me>2025-04-08 14:39:13 +0200
commit1f0d9ec8452f15c27cd33c4e3874454c35993743 (patch)
treec453a31ae5eb823aaf48868eea9fc4daf65f108b /api/auth/auth.go
parentc5b10e28b358308d8349b940af09f64368172f2e (diff)
Use internal/pkg structure
Diffstat (limited to 'api/auth/auth.go')
-rw-r--r--api/auth/auth.go57
1 files changed, 0 insertions, 57 deletions
diff --git a/api/auth/auth.go b/api/auth/auth.go
deleted file mode 100644
index b382beb..0000000
--- a/api/auth/auth.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package auth
-
-import (
- "errors"
- "os"
- "strings"
- "time"
-
- "github.com/golang-jwt/jwt/v5"
-)
-
-var jwtKey = []byte(os.Getenv("JWT_SECRET"))
-
-type Claims struct {
- UserID int `json:"user_id"`
- jwt.RegisteredClaims
-}
-
-func GenerateJWT(userID int) (string, error) {
- expirationTime := time.Now().Add(5 * time.Hour)
- claims := &Claims{
- UserID: userID,
- RegisteredClaims: jwt.RegisteredClaims{
- ExpiresAt: jwt.NewNumericDate(expirationTime),
- },
- }
-
- token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
- tokenString, err := token.SignedString(jwtKey)
- if err != nil {
- return "", err
- }
- return tokenString, nil
-}
-
-func ValidateJWT(tokenString string) (*Claims, error) {
- claims := &Claims{}
- // A token has a form `Bearer ...`
- tokenParts := strings.Split(tokenString, " ")
- if len(tokenParts) != 2 {
- return nil, errors.New("not valid JWT")
- }
-
- token, err := jwt.ParseWithClaims(tokenParts[1], claims, func(token *jwt.Token) (interface{}, error) {
- return jwtKey, nil
- })
-
- if err != nil {
- return nil, err
- }
-
- if !token.Valid {
- return nil, err
- }
-
- return claims, nil
-}