chore: and otp to current user resp

This commit is contained in:
Noah Hsu 2022-08-06 17:21:32 +08:00
parent a6ed4afdae
commit d01958a6bf

View File

@ -23,7 +23,7 @@ var (
type LoginReq struct { type LoginReq struct {
Username string `json:"username" binding:"required"` Username string `json:"username" binding:"required"`
Password string `json:"password"` Password string `json:"password"`
OTPCode string `json:"otp_code"` OtpCode string `json:"otp_code"`
} }
func Login(c *gin.Context) { func Login(c *gin.Context) {
@ -55,7 +55,7 @@ func Login(c *gin.Context) {
} }
// check 2FA // check 2FA
if user.OtpSecret != "" { if user.OtpSecret != "" {
if !totp.Validate(req.OTPCode, user.OtpSecret) { if !totp.Validate(req.OtpCode, user.OtpSecret) {
common.ErrorStrResp(c, "Invalid 2FA code", 402) common.ErrorStrResp(c, "Invalid 2FA code", 402)
loginCache.Set(ip, count+1) loginCache.Set(ip, count+1)
return return
@ -71,12 +71,22 @@ func Login(c *gin.Context) {
loginCache.Del(ip) loginCache.Del(ip)
} }
type UserResp struct {
model.User
Otp bool `json:"otp"`
}
// CurrentUser get current user by token // CurrentUser get current user by token
// if token is empty, return guest user // if token is empty, return guest user
func CurrentUser(c *gin.Context) { func CurrentUser(c *gin.Context) {
user := c.MustGet("user").(*model.User) user := c.MustGet("user").(*model.User)
userResp := *user userResp := UserResp{
User: *user,
}
userResp.Password = "" userResp.Password = ""
if userResp.OtpSecret != "" {
userResp.Otp = true
}
common.SuccessResp(c, userResp) common.SuccessResp(c, userResp)
} }