From 58a00120f8ba9c136cc71fbeefa6ffcbf4100bad Mon Sep 17 00:00:00 2001 From: Maxtune Lee <60775796+MaxtuneLee@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:51:02 +0800 Subject: [PATCH] fix(user): accept both email and id when login (#97) * fix(user): accept both email and id when login * fix(service/verify login): verify by email or uid depending on suffix --------- Co-authored-by: A.S.K <95036645+aimixsaka@users.noreply.github.com> --- src/service/user.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/service/user.go b/src/service/user.go index 13bb531..85bf43b 100644 --- a/src/service/user.go +++ b/src/service/user.go @@ -131,19 +131,19 @@ func VerifyAccountRegister(ctx *gin.Context, username string) (string, error) { // This function is used to verify the user's email is exist or not when login // This username is email or uid func VerifyAccountLogin(ctx *gin.Context, username string) (string, error) { - var user *model.User - user, err := model.UserByField("email", username) - if err != nil || user == nil { - return "", result.UserNotExist + var user *model.User + var err error + if strings.Contains(username, "@njupt.edu.cn") { + user, err = model.UserByField("email", username) + } else { + user, err = model.UserByField("uid", username) } - - if user == nil { - user, err := model.UserByField("uid", username) - if err != nil || user == nil { - return "", result.UserNotExist - } + + if err != nil { + return "", err } + ticket, err := util.GenerateTokenWithExp(ctx, model.LoginTicketJWTSubKey(*user.Uid), model.LOGIN_TICKET_EXP) if err != nil || ticket == "" { return "", err