-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_in.go
44 lines (37 loc) · 1 KB
/
sign_in.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
package handler
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/l-orlov/slim-fairy/bot/internal/model"
"github.com/l-orlov/slim-fairy/bot/internal/service"
"github.com/l-orlov/slim-fairy/bot/internal/store"
"github.com/pkg/errors"
)
func (h *Handler) signInUser(c *gin.Context) {
user := &model.UserToSignIn{}
err := c.BindJSON(user)
if err != nil {
log.Printf("c.BindJSON: %v", err)
h.writeErrorResponse(c, http.StatusBadRequest, "invalid request")
return
}
token, err := h.svc.SignInUser(c, user)
if err != nil {
log.Printf("h.svc.SignInUser: %v", err)
msg := "failed to sign-in user"
if errors.Is(err, store.ErrNotFound) {
h.writeErrorResponse(c, http.StatusNotFound, msg)
return
}
if errors.Is(err, service.ErrWrongPassword) {
h.writeErrorResponse(c, http.StatusUnauthorized, "wrong password")
return
}
h.writeErrorResponse(c, http.StatusInternalServerError, msg)
return
}
c.JSON(http.StatusOK, map[string]interface{}{
"accessToken": token,
})
}