From 40fe993eb40095b3188948c18c8ed64de086fea4 Mon Sep 17 00:00:00 2001 From: icey-yu <119291641+icey-yu@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:27:21 +0800 Subject: [PATCH] fix: Reset password by email failed (#595) * fix: ResetPasswordReq pb * fix: reset psw error * Revert "fix: ResetPasswordReq pb" This reverts commit 7e884dc225a523e219333332859ec236a2b3fdd1. --- internal/rpc/chat/password.go | 31 ++++++++++++++++--------------- pkg/common/db/database/chat.go | 3 +++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/internal/rpc/chat/password.go b/internal/rpc/chat/password.go index 7c9657a0a7..73d34a36f8 100644 --- a/internal/rpc/chat/password.go +++ b/internal/rpc/chat/password.go @@ -16,7 +16,6 @@ package chat import ( "context" - "github.com/openimsdk/tools/errs" "github.com/openimsdk/chat/pkg/common/constant" @@ -28,6 +27,11 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq) if req.Password == "" { return nil, errs.ErrArgs.WrapMsg("password must be set") } + if req.AreaCode == "" || req.PhoneNumber == "" { + if !(req.AreaCode == "" && req.PhoneNumber == "") { + return nil, errs.ErrArgs.WrapMsg("area code and phone number must set together") + } + } var verifyCodeID string var err error if req.Email == "" { @@ -39,22 +43,19 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq) if err != nil { return nil, err } - + var account string if req.Email == "" { - attribute, err := o.Database.TakeAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber) - if err != nil { - return nil, err - } - err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, attribute.UserID, req.Password, verifyCodeID) + account = BuildCredentialPhone(req.AreaCode, req.PhoneNumber) } else { - attribute, err := o.Database.TakeAttributeByEmail(ctx, req.Email) - if err != nil { - return nil, err - } - err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, attribute.UserID, req.Password, verifyCodeID) - if err != nil { - return nil, err - } + account = req.Email + } + cred, err := o.Database.TakeCredentialByAccount(ctx, account) + if err != nil { + return nil, err + } + err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, cred.UserID, req.Password, verifyCodeID) + if err != nil { + return nil, err } return &chat.ResetPasswordResp{}, nil } diff --git a/pkg/common/db/database/chat.go b/pkg/common/db/database/chat.go index 55acd558a0..5898fd73e3 100644 --- a/pkg/common/db/database/chat.go +++ b/pkg/common/db/database/chat.go @@ -255,6 +255,9 @@ func (o *ChatDatabase) UpdatePasswordAndDeleteVerifyCode(ctx context.Context, us if err := o.account.UpdatePassword(ctx, userID, password); err != nil { return err } + if codeID == "" { + return nil + } if err := o.verifyCode.Delete(ctx, codeID); err != nil { return err }