Skip to content

Commit

Permalink
refactor: simplify IsDuplicatedUser
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Nov 13, 2023
1 parent 53cce71 commit fcb44cb
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,20 +577,27 @@ func IsDuplicatedEmail(tx *storage.Connection, email, aud string, currentUser *U

userIDs := make(map[string]uuid.UUID)
for _, identity := range identities {
if !identity.IsForSSOProvider() {
if (currentUser != nil && currentUser.ID != identity.UserID) || (currentUser == nil) {
if _, ok := userIDs[identity.UserID.String()]; !ok {
if !identity.IsForSSOProvider() {
userIDs[identity.UserID.String()] = identity.UserID
}
}
}

var currentUserId uuid.UUID
if currentUser != nil {
currentUserId = currentUser.ID
}

for _, userID := range userIDs {
user, err := FindUserByID(tx, userID)
if err != nil {
return nil, errors.Wrap(err, "unable to find user from email identity for duplicates")
}
if user.Aud == aud {
return user, nil
if userID != currentUserId {
user, err := FindUserByID(tx, userID)
if err != nil {
return nil, errors.Wrap(err, "unable to find user from email identity for duplicates")
}
if user.Aud == aud {
return user, nil
}
}
}

Expand Down

0 comments on commit fcb44cb

Please sign in to comment.