-
Notifications
You must be signed in to change notification settings - Fork 5
/
login_controller.go
60 lines (50 loc) · 1.6 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
55
56
57
58
59
60
package authentication_controller
import (
"github.com/gin-gonic/gin"
"github.com/gocms-io/gocms/utility/errors"
"net/http"
"github.com/gocms-io/gocms/domain/acl/authentication/authentication_model"
)
/**
* @api {post} /login Login
* @apiName Login
* @apiGroup Authentication
*
* @apiUse LoginInput
* @apiUse UserDisplay
* @apiUse AuthHeaderResponse
*/
func (ac *AuthController) login(c *gin.Context) {
var loginInput authentication_model.LoginInput
// get login values
if c.BindJSON(&loginInput) != nil {
errors.Response(c, http.StatusUnauthorized, "Missing Email or Password", REDIRECT_LOGIN)
return
}
// auth user
user, authed := ac.ServicesGroup.AuthService.AuthUser(loginInput.Email, loginInput.Password)
if !authed {
errors.ResponseWithSoftRedirect(c, http.StatusUnauthorized, errors.ApiError_Bad_Email_Password, REDIRECT_LOGIN)
return
}
// verify user is enabled
if !user.Enabled {
errors.ResponseWithSoftRedirect(c, http.StatusUnauthorized, errors.ApiError_Bad_Email_Password, REDIRECT_LOGIN)
return
}
// verify user has activated email
if !user.Verified {
errors.ResponseWithSoftRedirect(c, http.StatusUnauthorized, "Your primary email has not yet been verified. A new verification email will be sent.", REDIRECT_LOGIN)
ac.ServicesGroup.EmailService.SendEmailActivationCode(user.Email)
return
}
// create token
tokenString, err := ac.createToken(user.Id)
if err != nil {
errors.ResponseWithSoftRedirect(c, http.StatusUnauthorized, "Error generating token.", REDIRECT_LOGIN)
return
}
c.Header("X-AUTH-TOKEN", tokenString)
c.JSON(http.StatusOK, user.GetUserDisplay())
return
}