-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
50 lines (42 loc) · 989 Bytes
/
login.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
package routers
import (
"net/http"
"github.com/fancxxy/gocomic/backend/models"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
// @Summary Login
// @Produce json
// @Param login body routers.Login true "email and password"
// @Success 200 {object} routers.Response
// @Failure 400 {object} routers.Response
// @Failure 401 {object} routers.Response
// @Router /login [post]
func login(c *gin.Context) {
session := sessions.Default(c)
var request Login
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusUnauthorized, Response{
Code: 401,
Msg: "email and password are mandatory",
Data: nil,
})
return
}
user, err := models.Login(request.Email, request.Password)
if err != nil {
c.JSON(http.StatusUnauthorized, Response{
Code: 401,
Msg: err.Error(),
Data: nil,
})
return
}
session.Set("user", user)
session.Save()
c.JSON(http.StatusOK, Response{
Code: 200,
Msg: "login succeed",
Data: nil,
})
}