-
Notifications
You must be signed in to change notification settings - Fork 13
/
login_dev.go
45 lines (33 loc) · 1.19 KB
/
login_dev.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
package account
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/coretrix/hitrix/pkg/binding"
"github.com/coretrix/hitrix/pkg/view/account"
"github.com/coretrix/hitrix/service"
)
type LoginDevForm struct {
Username string `binding:"required,min=6,max=60" json:"Username" form:"username"`
Password string `binding:"required,min=8,max=60" json:"Password" form:"password"`
}
func (l *LoginDevForm) Login(c *gin.Context) (string, string, error) {
err := binding.ShouldBindJSON(c, l)
if err != nil {
return "", "", err
}
ormService := service.DI().OrmEngineForContext(c.Request.Context())
passwordService := service.DI().Password()
devPanelUserEntity := service.DI().App().DevPanel.UserEntity
ok := ormService.CachedSearchOne(devPanelUserEntity, "UserEmailIndex", l.Username)
if !ok {
return "", "", errors.New("invalid username or password")
}
if !passwordService.VerifyPassword(l.Password, devPanelUserEntity.GetPassword()) {
return "", "", errors.New("invalid username or password")
}
token, refreshToken, err := account.GenerateDevTokenAndRefreshToken(ormService, devPanelUserEntity.GetID())
if err != nil {
return "", "", err
}
return token, refreshToken, err
}