-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_controller.go
105 lines (88 loc) · 2.78 KB
/
account_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package controllers
import (
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/zhsyourai/URCF-engine/http/controllers/shard"
"github.com/zhsyourai/URCF-engine/http/gin-jwt"
"github.com/zhsyourai/URCF-engine/services/account"
"net/http"
"time"
)
func NewAccountController(middleware *gin_jwt.JwtMiddleware, generator *gin_jwt.JwtGenerator) *AccountController {
return &AccountController{
service: account.GetInstance(),
middleware: middleware,
generator: generator,
}
}
// AccountController is our /uaa controller.
type AccountController struct {
service account.Service
middleware *gin_jwt.JwtMiddleware
generator *gin_jwt.JwtGenerator
}
func (c *AccountController) Handler(root *gin.RouterGroup) {
root.GET("/register", c.RegisterHandler)
root.POST("/login", c.LoginHandler)
root.POST("/logout", c.middleware.Handler, c.LogoutHandler)
root.POST("/change_password", c.middleware.Handler, c.ChangePassword)
}
func (c *AccountController) RegisterHandler(ctx *gin.Context) {
registerRequest := &shard.RegisterRequest{}
ctx.Bind(registerRequest)
user, err := c.service.Register(registerRequest.Username, registerRequest.Password, registerRequest.Roles)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
registerResponse := shard.RegisterResponse{
Username: user.Username,
Role: user.Roles,
CreateDate: user.CreateDate,
}
ctx.JSON(http.StatusOK, registerResponse)
}
func (c *AccountController) LoginHandler(ctx *gin.Context) {
loginRequest := &shard.LoginRequest{}
ctx.Bind(loginRequest)
user, err := c.service.Verify(loginRequest.Username, loginRequest.Password)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
return
}
token, err := c.generator.GenerateJwt(time.Hour, time.Hour*3, jwt.MapClaims{
"uid": user.ID,
"username": user.Username,
"roles": user.Roles,
})
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
return
}
ctx.JSON(http.StatusOK, gin.H{
"access_token": token,
"type": "bearer",
"expire_in": time.Hour.Seconds(),
})
}
func (c *AccountController) ChangePassword(ctx *gin.Context) {
changePasswordRequest := &shard.ChangePasswordRequest{}
ctx.Bind(changePasswordRequest)
token, err := c.middleware.ExtractToken(ctx)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
return
}
claims := token.Claims.(jwt.MapClaims)
if err := c.service.ChangePassword(claims["uid"].(string),
changePasswordRequest.OldPassword, changePasswordRequest.NewPassword); err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
}
}
func (c *AccountController) LogoutHandler(ctx *gin.Context) {
//token, ok := ctx.Get("jwt")
//if !ok {
//}
//jwtToken := token.(*jwt.Token)
//claims := jwtToken.Claims.(jwt.MapClaims)
}