Skip to content

Commit 3e12d38

Browse files
authored
Merge pull request #328 from yokowu/feat-user-tip
feat: 优化用户登录注册提示
2 parents 6279eee + bce7acb commit 3e12d38

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

backend/errcode/errcode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var (
1414
ErrUserNotFound = web.NewBadRequestErr("err-user-not-found")
1515
ErrUserLock = web.NewBadRequestErr("err-user-lock")
1616
ErrPassword = web.NewBadRequestErr("err-password")
17+
ErrAccountAlreadyExist = web.NewBadRequestErr("err-account-already-exist")
1718
ErrInviteCodeInvalid = web.NewBadRequestErr("err-invite-code-invalid")
1819
ErrEmailInvalid = web.NewBadRequestErr("err-email-invalid")
1920
ErrOAuthStateInvalid = web.NewBadRequestErr("err-oauth-state-invalid")

backend/errcode/locale.en.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ other = "User not found"
77
[err-user-lock]
88
other = "User is locked"
99

10+
[err-account-already-exist]
11+
other = "Account already exists"
12+
1013
[err-password]
1114
other = "Incorrect password"
1215

backend/errcode/locale.zh.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ other = "用户不存在"
77
[err-user-lock]
88
other = "用户已锁定"
99

10+
[err-account-already-exist]
11+
other = "当前账号已经注册"
12+
1013
[err-only-admin]
1114
other = "仅管理员可操作"
1215

backend/internal/user/repo/user.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,25 @@ func (r *UserRepo) innerValidateInviteCode(ctx context.Context, tx *db.Tx, code
136136
return ic, nil
137137
}
138138

139-
func (r *UserRepo) CreateUser(ctx context.Context, user *db.User) (*db.User, error) {
139+
func (r *UserRepo) CreateUser(ctx context.Context, us *db.User) (*db.User, error) {
140140
var res *db.User
141141
err := entx.WithTx(ctx, r.db, func(tx *db.Tx) error {
142142
if err := r.checkLimit(ctx, tx); err != nil {
143143
return err
144144
}
145+
n, err := tx.User.Query().Where(user.Email(us.Email)).Count(ctx)
146+
if err != nil {
147+
return err
148+
}
149+
if n > 0 {
150+
return errcode.ErrAccountAlreadyExist
151+
}
145152
u, err := tx.User.Create().
146-
SetUsername(user.Username).
147-
SetEmail(user.Email).
148-
SetPassword(user.Password).
149-
SetStatus(user.Status).
150-
SetPlatform(user.Platform).
153+
SetUsername(us.Username).
154+
SetEmail(us.Email).
155+
SetPassword(us.Password).
156+
SetStatus(us.Status).
157+
SetPlatform(us.Platform).
151158
Save(ctx)
152159
if err != nil {
153160
return err
@@ -377,7 +384,8 @@ func (r *UserRepo) OAuthRegister(ctx context.Context, platform consts.UserPlatfo
377384
Where(useridentity.Platform(platform), useridentity.IdentityID(req.ID)).
378385
First(ctx)
379386
if err == nil {
380-
return fmt.Errorf("user already exists for platform %s and identity ID %s", platform, req.ID)
387+
e := fmt.Errorf("user already exists for platform %s and identity ID %s", platform, req.ID)
388+
return errcode.ErrAccountAlreadyExist.Wrap(e)
381389
}
382390
if !db.IsNotFound(err) {
383391
return err

0 commit comments

Comments
 (0)