This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
86 lines (67 loc) · 2.15 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package controllers
import (
"errors"
"net/http"
"time"
"github.com/cyruzin/feelthemovies/internal/app/config"
model "github.com/cyruzin/feelthemovies/internal/app/models"
"github.com/cyruzin/feelthemovies/internal/pkg/errhandler"
"github.com/cyruzin/feelthemovies/internal/pkg/validation"
jwt "github.com/dgrijalva/jwt-go"
)
// AuthUser authenticates the user.
func (s *Setup) AuthUser(w http.ResponseWriter, r *http.Request) {
request := model.Auth{}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
errhandler.DecodeError(w, r, s.logger, errDecode, http.StatusInternalServerError)
return
}
ctx := r.Context()
if err := s.validator.StructCtx(ctx, request); err != nil {
validation.ValidatorMessage(w, err)
return
}
dbPassword, err := s.model.Authenticate(ctx, request.Email)
if err != nil {
errhandler.DecodeError(w, r, s.logger, errAuth, http.StatusInternalServerError)
return
}
if checkPassword := s.CheckPasswordHash(request.Password, dbPassword); !checkPassword {
errhandler.DecodeError(w, r, s.logger, errUnauthorized, http.StatusUnauthorized)
return
}
authenticationInfo, err := s.model.GetAuthenticationInfo(ctx, request.Email)
if err != nil {
errhandler.DecodeError(w, r, s.logger, errFetch, http.StatusInternalServerError)
return
}
token, err := s.GenerateToken(authenticationInfo)
if err != nil {
errhandler.DecodeError(w, r, s.logger, err.Error(), http.StatusInternalServerError)
return
}
userInfo := model.AuthJWT{Token: token}
s.ToJSON(w, http.StatusOK, &userInfo)
}
// GenerateToken generates a new JWT Token.
func (s *Setup) GenerateToken(info *model.Auth) (string, error) {
cfg, err := config.Load()
if err != nil {
return "", err
}
secret := []byte(cfg.JWTSecret)
var claims model.AuthClaims
claims.ID = info.ID
claims.Name = info.Name
claims.Email = info.Email
claims.StandardClaims = jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * 2).Unix(),
Issuer: cfg.AppName,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedString, err := token.SignedString(secret)
if err != nil {
return "", errors.New(errGeneratingToken)
}
return signedString, nil
}