Skip to content

Commit

Permalink
optimized user auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Zhang committed Jul 27, 2019
1 parent 92d443f commit 80449a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func main() {
app.POST("/users/:id", routes.PostUser) // 更改用户
app.DELETE("/users/:id", routes.DeleteUser) // 删除用户
app.POST("/login", routes.Login) // 用户登录
app.GET("/me", routes.GetMe) // 获取自己账户
}

// 路由ping
Expand Down
19 changes: 19 additions & 0 deletions backend/routes/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,22 @@ func Login(c *gin.Context) {
Data: tokenStr,
})
}

func GetMe(c *gin.Context) {
// 获取token string
tokenStr := c.GetHeader("Authorization")

// 校验token
user, err := services.CheckToken(tokenStr)
if err != nil {
HandleError(http.StatusUnauthorized, c, errors.New("not authorized"))
return
}
user.Password = ""

c.JSON(http.StatusOK, Response{
Status: "ok",
Message: "success",
Data: user,
})
}

0 comments on commit 80449a8

Please sign in to comment.