-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
46 lines (37 loc) · 963 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
package api
import (
"net/http"
"github.com/thomasfady/xsstower/models"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type LoginForm struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
}
// @BasePath /api/
// @Summary Login
// @Description Login
// @Produce json
// @Router /login [post]
// @Param LoginForm body LoginForm true "Credentials"
// @Tags Authentication
func PostLogin(c *gin.Context) {
var form LoginForm
if err := c.ShouldBind(&form); err != nil {
c.JSON(http.StatusNotAcceptable, err.Error())
return
} else {
user := models.GetUserByEmail(form.Email)
if user.CheckPassword(form.Password) {
session := sessions.Default(c)
session.Set("user_id", user.ID)
session.Set("is_admin", user.IsAdmin)
session.Save()
c.JSON(http.StatusOK, user)
return
} else {
c.JSON(http.StatusUnauthorized, "Bad email or password")
}
}
}