-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_controller.go
54 lines (51 loc) · 1.38 KB
/
login_controller.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
package api
import (
"errors"
"fmt"
"github.com/gofiber/fiber"
"github.com/zackartz/blog-api/auth"
"github.com/zackartz/blog-api/models"
"github.com/zackartz/blog-api/utils"
"github.com/zackartz/blog-api/utils/formaterror"
"golang.org/x/crypto/bcrypt"
"net/http"
)
func (s *Server) Login(ctx *fiber.Ctx) {
var user models.User
if err := ctx.BodyParser(&user); err != nil {
utils.Error(ctx, http.StatusUnprocessableEntity, err)
return
}
fmt.Printf("%s", user.Password)
user.Prepare()
err := user.Validate("login")
if err != nil {
utils.Error(ctx, http.StatusUnprocessableEntity, err)
return
}
token, err := s.SignIn(user.Email, user.Password)
if err != nil {
formattedError := formaterror.FormatError(err.Error())
utils.Error(ctx, http.StatusUnprocessableEntity, formattedError)
return
}
utils.JSON(ctx, http.StatusOK, struct {
Token string `json:"token"`
}{
Token: token,
})
}
func (s *Server) SignIn(email, password string) (string, error) {
var err error
user := &models.User{}
err = s.DB.Debug().Model(models.User{}).Where("email = ?", email).Take(&user).Error
if err != nil {
return "", errors.New("email")
}
err = models.VerifyPassword(user.Password, password)
if err != nil && err == bcrypt.ErrMismatchedHashAndPassword {
fmt.Printf("%s, %s : %v", password, user.Password, err)
return "", errors.New("password")
}
return auth.CreateToken(user.ID)
}