chore: common err resp log

This commit is contained in:
Noah Hsu
2022-06-28 18:12:53 +08:00
parent 67bc66fedf
commit d1efec4539
10 changed files with 68 additions and 56 deletions

View File

@@ -1,12 +1,13 @@
package controllers
import (
"time"
"github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"time"
)
var loginCache = cache.NewMemCache[int]()
@@ -32,24 +33,24 @@ func Login(c *gin.Context) {
// check username
var req LoginReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400, true)
common.ErrorResp(c, err, 400)
return
}
user, err := db.GetUserByName(req.Username)
if err != nil {
common.ErrorResp(c, err, 400, true)
common.ErrorResp(c, err, 400)
return
}
// validate password
if err := user.ValidatePassword(req.Password); err != nil {
common.ErrorResp(c, err, 400, true)
common.ErrorResp(c, err, 400)
loginCache.Set(ip, count+1)
return
}
// generate token
token, err := common.GenerateToken(user.Username)
if err != nil {
common.ErrorResp(c, err, 400)
common.ErrorResp(c, err, 400, true)
return
}
common.SuccessResp(c, gin.H{"token": token})