fix: prevent duplicate user accounts by email, phone, and OAuth
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
d3604e9d6b
commit
00152bb77e
2
db/migrations/000092_users_email_phone_unique.down.sql
Normal file
2
db/migrations/000092_users_email_phone_unique.down.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
DROP INDEX IF EXISTS idx_users_phone_unique;
|
||||||
|
DROP INDEX IF EXISTS idx_users_email_lower_unique;
|
||||||
35
db/migrations/000092_users_email_phone_unique.up.sql
Normal file
35
db/migrations/000092_users_email_phone_unique.up.sql
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
-- Normalize empty contact fields before enforcing uniqueness.
|
||||||
|
UPDATE users SET email = NULL WHERE email IS NOT NULL AND TRIM(email) = '';
|
||||||
|
UPDATE users SET phone_number = NULL WHERE phone_number IS NOT NULL AND TRIM(phone_number) = '';
|
||||||
|
UPDATE users SET email = LOWER(TRIM(email)) WHERE email IS NOT NULL;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM users
|
||||||
|
WHERE email IS NOT NULL
|
||||||
|
GROUP BY LOWER(email)
|
||||||
|
HAVING COUNT(*) > 1
|
||||||
|
) THEN
|
||||||
|
RAISE EXCEPTION 'duplicate user emails exist; resolve before applying 000092_users_email_phone_unique';
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM users
|
||||||
|
WHERE phone_number IS NOT NULL
|
||||||
|
GROUP BY TRIM(phone_number)
|
||||||
|
HAVING COUNT(*) > 1
|
||||||
|
) THEN
|
||||||
|
RAISE EXCEPTION 'duplicate user phone numbers exist; resolve before applying 000092_users_email_phone_unique';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email_lower_unique
|
||||||
|
ON users (LOWER(email))
|
||||||
|
WHERE email IS NOT NULL;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_phone_unique
|
||||||
|
ON users (phone_number)
|
||||||
|
WHERE phone_number IS NOT NULL;
|
||||||
|
|
@ -360,10 +360,20 @@ WHERE id = $1;
|
||||||
-- name: CheckPhoneEmailExist :one
|
-- name: CheckPhoneEmailExist :one
|
||||||
SELECT
|
SELECT
|
||||||
EXISTS (
|
EXISTS (
|
||||||
SELECT 1 FROM users u1 WHERE u1.phone_number = $1
|
SELECT 1
|
||||||
|
FROM users u1
|
||||||
|
WHERE sqlc.narg('phone_number')::TEXT IS NOT NULL
|
||||||
|
AND TRIM(sqlc.narg('phone_number')::TEXT) <> ''
|
||||||
|
AND u1.phone_number IS NOT NULL
|
||||||
|
AND TRIM(u1.phone_number) = TRIM(sqlc.narg('phone_number')::TEXT)
|
||||||
) AS phone_exists,
|
) AS phone_exists,
|
||||||
EXISTS (
|
EXISTS (
|
||||||
SELECT 1 FROM users u2 WHERE u2.email = $2
|
SELECT 1
|
||||||
|
FROM users u2
|
||||||
|
WHERE sqlc.narg('email')::TEXT IS NOT NULL
|
||||||
|
AND TRIM(sqlc.narg('email')::TEXT) <> ''
|
||||||
|
AND u2.email IS NOT NULL
|
||||||
|
AND LOWER(TRIM(u2.email)) = LOWER(TRIM(sqlc.narg('email')::TEXT))
|
||||||
) AS email_exists;
|
) AS email_exists;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -435,8 +445,16 @@ SELECT
|
||||||
created_at,
|
created_at,
|
||||||
updated_at
|
updated_at
|
||||||
FROM users
|
FROM users
|
||||||
WHERE (email = $1 AND $1 IS NOT NULL)
|
WHERE (
|
||||||
OR (phone_number = $2 AND $2 IS NOT NULL)
|
sqlc.narg('email')::TEXT IS NOT NULL
|
||||||
|
AND TRIM(sqlc.narg('email')::TEXT) <> ''
|
||||||
|
AND LOWER(TRIM(email)) = LOWER(TRIM(sqlc.narg('email')::TEXT))
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
sqlc.narg('phone_number')::TEXT IS NOT NULL
|
||||||
|
AND TRIM(sqlc.narg('phone_number')::TEXT) <> ''
|
||||||
|
AND TRIM(phone_number) = TRIM(sqlc.narg('phone_number')::TEXT)
|
||||||
|
)
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
-- name: UpdatePassword :exec
|
-- name: UpdatePassword :exec
|
||||||
|
|
|
||||||
|
|
@ -62,10 +62,20 @@ func (q *Queries) BulkReactivateUsersByRole(ctx context.Context, arg BulkReactiv
|
||||||
const CheckPhoneEmailExist = `-- name: CheckPhoneEmailExist :one
|
const CheckPhoneEmailExist = `-- name: CheckPhoneEmailExist :one
|
||||||
SELECT
|
SELECT
|
||||||
EXISTS (
|
EXISTS (
|
||||||
SELECT 1 FROM users u1 WHERE u1.phone_number = $1
|
SELECT 1
|
||||||
|
FROM users u1
|
||||||
|
WHERE $1::TEXT IS NOT NULL
|
||||||
|
AND TRIM($1::TEXT) <> ''
|
||||||
|
AND u1.phone_number IS NOT NULL
|
||||||
|
AND TRIM(u1.phone_number) = TRIM($1::TEXT)
|
||||||
) AS phone_exists,
|
) AS phone_exists,
|
||||||
EXISTS (
|
EXISTS (
|
||||||
SELECT 1 FROM users u2 WHERE u2.email = $2
|
SELECT 1
|
||||||
|
FROM users u2
|
||||||
|
WHERE $2::TEXT IS NOT NULL
|
||||||
|
AND TRIM($2::TEXT) <> ''
|
||||||
|
AND u2.email IS NOT NULL
|
||||||
|
AND LOWER(TRIM(u2.email)) = LOWER(TRIM($2::TEXT))
|
||||||
) AS email_exists
|
) AS email_exists
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -801,8 +811,16 @@ SELECT
|
||||||
created_at,
|
created_at,
|
||||||
updated_at
|
updated_at
|
||||||
FROM users
|
FROM users
|
||||||
WHERE (email = $1 AND $1 IS NOT NULL)
|
WHERE (
|
||||||
OR (phone_number = $2 AND $2 IS NOT NULL)
|
$1::TEXT IS NOT NULL
|
||||||
|
AND TRIM($1::TEXT) <> ''
|
||||||
|
AND LOWER(TRIM(email)) = LOWER(TRIM($1::TEXT))
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
$2::TEXT IS NOT NULL
|
||||||
|
AND TRIM($2::TEXT) <> ''
|
||||||
|
AND TRIM(phone_number) = TRIM($2::TEXT)
|
||||||
|
)
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ var (
|
||||||
ErrUserNotFound = errors.New("user not found")
|
ErrUserNotFound = errors.New("user not found")
|
||||||
ErrEmailAlreadyRegistered = errors.New("email is already registered")
|
ErrEmailAlreadyRegistered = errors.New("email is already registered")
|
||||||
ErrPhoneAlreadyRegistered = errors.New("phone number is already registered")
|
ErrPhoneAlreadyRegistered = errors.New("phone number is already registered")
|
||||||
|
ErrGoogleAccountAlreadyRegistered = errors.New("google account is already linked to another user")
|
||||||
|
ErrAppleAccountAlreadyRegistered = errors.New("apple account is already linked to another user")
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -61,10 +61,11 @@ func (s *Store) LinkGoogleAccount(
|
||||||
googleID string,
|
googleID string,
|
||||||
) error {
|
) error {
|
||||||
|
|
||||||
return s.queries.LinkGoogleAccount(ctx, dbgen.LinkGoogleAccountParams{
|
err := s.queries.LinkGoogleAccount(ctx, dbgen.LinkGoogleAccountParams{
|
||||||
ID: userID,
|
ID: userID,
|
||||||
GoogleID: pgtype.Text{String: googleID, Valid: true},
|
GoogleID: pgtype.Text{String: googleID, Valid: true},
|
||||||
})
|
})
|
||||||
|
return mapUserUniqueViolation(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) LinkAppleAccount(
|
func (s *Store) LinkAppleAccount(
|
||||||
|
|
@ -73,21 +74,23 @@ func (s *Store) LinkAppleAccount(
|
||||||
appleID string,
|
appleID string,
|
||||||
emailVerified bool,
|
emailVerified bool,
|
||||||
) error {
|
) error {
|
||||||
return s.queries.LinkAppleAccount(ctx, dbgen.LinkAppleAccountParams{
|
err := s.queries.LinkAppleAccount(ctx, dbgen.LinkAppleAccountParams{
|
||||||
ID: userID,
|
ID: userID,
|
||||||
AppleID: pgtype.Text{String: appleID, Valid: true},
|
AppleID: pgtype.Text{String: appleID, Valid: true},
|
||||||
AppleEmailVerified: pgtype.Bool{Bool: emailVerified, Valid: true},
|
AppleEmailVerified: pgtype.Bool{Bool: emailVerified, Valid: true},
|
||||||
})
|
})
|
||||||
|
return mapUserUniqueViolation(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) CreateAppleUser(
|
func (s *Store) CreateAppleUser(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
aUser domain.AppleUser,
|
aUser domain.AppleUser,
|
||||||
) (domain.User, error) {
|
) (domain.User, error) {
|
||||||
|
email := normalizeEmail(aUser.Email)
|
||||||
res, err := s.queries.CreateAppleUser(ctx, dbgen.CreateAppleUserParams{
|
res, err := s.queries.CreateAppleUser(ctx, dbgen.CreateAppleUserParams{
|
||||||
FirstName: pgtype.Text{String: aUser.GivenName, Valid: aUser.GivenName != ""},
|
FirstName: pgtype.Text{String: aUser.GivenName, Valid: aUser.GivenName != ""},
|
||||||
LastName: pgtype.Text{String: aUser.FamilyName, Valid: aUser.FamilyName != ""},
|
LastName: pgtype.Text{String: aUser.FamilyName, Valid: aUser.FamilyName != ""},
|
||||||
Email: pgtype.Text{String: aUser.Email, Valid: aUser.Email != ""},
|
Email: pgtype.Text{String: email, Valid: email != ""},
|
||||||
AppleID: pgtype.Text{String: aUser.ID, Valid: true},
|
AppleID: pgtype.Text{String: aUser.ID, Valid: true},
|
||||||
AppleEmailVerified: pgtype.Bool{Bool: aUser.VerifiedEmail, Valid: true},
|
AppleEmailVerified: pgtype.Bool{Bool: aUser.VerifiedEmail, Valid: true},
|
||||||
Role: string(domain.RoleStudent),
|
Role: string(domain.RoleStudent),
|
||||||
|
|
@ -95,7 +98,7 @@ func (s *Store) CreateAppleUser(
|
||||||
EmailVerified: aUser.VerifiedEmail,
|
EmailVerified: aUser.VerifiedEmail,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.User{}, err
|
return domain.User{}, mapUserUniqueViolation(err)
|
||||||
}
|
}
|
||||||
return mapDBUser(res, nil, nil), nil
|
return mapDBUser(res, nil, nil), nil
|
||||||
}
|
}
|
||||||
|
|
@ -118,11 +121,11 @@ func (s *Store) CreateGoogleUser(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
gUser domain.GoogleUser,
|
gUser domain.GoogleUser,
|
||||||
) (domain.User, error) {
|
) (domain.User, error) {
|
||||||
|
email := normalizeEmail(gUser.Email)
|
||||||
res, err := s.queries.CreateGoogleUser(ctx, dbgen.CreateGoogleUserParams{
|
res, err := s.queries.CreateGoogleUser(ctx, dbgen.CreateGoogleUserParams{
|
||||||
FirstName: pgtype.Text{String: gUser.GivenName, Valid: true},
|
FirstName: pgtype.Text{String: gUser.GivenName, Valid: true},
|
||||||
LastName: pgtype.Text{String: gUser.FamilyName, Valid: true},
|
LastName: pgtype.Text{String: gUser.FamilyName, Valid: true},
|
||||||
Email: pgtype.Text{String: gUser.Email, Valid: true},
|
Email: pgtype.Text{String: email, Valid: email != ""},
|
||||||
GoogleID: pgtype.Text{String: gUser.ID, Valid: true},
|
GoogleID: pgtype.Text{String: gUser.ID, Valid: true},
|
||||||
GoogleEmailVerified: pgtype.Bool{Bool: gUser.VerifiedEmail, Valid: true},
|
GoogleEmailVerified: pgtype.Bool{Bool: gUser.VerifiedEmail, Valid: true},
|
||||||
Role: string(domain.RoleStudent),
|
Role: string(domain.RoleStudent),
|
||||||
|
|
@ -133,7 +136,7 @@ func (s *Store) CreateGoogleUser(
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.User{}, err
|
return domain.User{}, mapUserUniqueViolation(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mapDBUser(res, nil, nil), nil
|
return mapDBUser(res, nil, nil), nil
|
||||||
|
|
@ -204,6 +207,8 @@ func (s *Store) CreateUserWithoutOtp(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
user domain.User,
|
user domain.User,
|
||||||
) (domain.User, error) {
|
) (domain.User, error) {
|
||||||
|
email := normalizeEmail(user.Email)
|
||||||
|
phone := normalizePhone(user.PhoneNumber)
|
||||||
|
|
||||||
userRes, err := s.queries.CreateUser(ctx, dbgen.CreateUserParams{
|
userRes, err := s.queries.CreateUser(ctx, dbgen.CreateUserParams{
|
||||||
FirstName: pgtype.Text{String: user.FirstName},
|
FirstName: pgtype.Text{String: user.FirstName},
|
||||||
|
|
@ -218,8 +223,8 @@ func (s *Store) CreateUserWithoutOtp(
|
||||||
},
|
},
|
||||||
// UserName: user.UserName,
|
// UserName: user.UserName,
|
||||||
|
|
||||||
Email: pgtype.Text{String: user.Email, Valid: user.Email != ""},
|
Email: pgtype.Text{String: email, Valid: email != ""},
|
||||||
PhoneNumber: pgtype.Text{String: user.PhoneNumber, Valid: user.PhoneNumber != ""},
|
PhoneNumber: pgtype.Text{String: phone, Valid: phone != ""},
|
||||||
|
|
||||||
Role: string(user.Role),
|
Role: string(user.Role),
|
||||||
Password: user.Password,
|
Password: user.Password,
|
||||||
|
|
@ -272,7 +277,7 @@ func (s *Store) CreateUserWithoutOtp(
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.User{}, err
|
return domain.User{}, mapUserUniqueViolation(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var updatedAt *time.Time
|
var updatedAt *time.Time
|
||||||
|
|
@ -289,6 +294,8 @@ func (s *Store) CreateUser(
|
||||||
user domain.User,
|
user domain.User,
|
||||||
usedOtpId int64,
|
usedOtpId int64,
|
||||||
) (domain.User, error) {
|
) (domain.User, error) {
|
||||||
|
email := normalizeEmail(user.Email)
|
||||||
|
phone := normalizePhone(user.PhoneNumber)
|
||||||
|
|
||||||
if usedOtpId > 0 {
|
if usedOtpId > 0 {
|
||||||
if err := s.queries.MarkOtpAsUsed(ctx, dbgen.MarkOtpAsUsedParams{
|
if err := s.queries.MarkOtpAsUsed(ctx, dbgen.MarkOtpAsUsedParams{
|
||||||
|
|
@ -312,8 +319,8 @@ func (s *Store) CreateUser(
|
||||||
},
|
},
|
||||||
// UserName: user.UserName,
|
// UserName: user.UserName,
|
||||||
|
|
||||||
Email: pgtype.Text{String: user.Email, Valid: user.Email != ""},
|
Email: pgtype.Text{String: email, Valid: email != ""},
|
||||||
PhoneNumber: pgtype.Text{String: user.PhoneNumber, Valid: user.PhoneNumber != ""},
|
PhoneNumber: pgtype.Text{String: phone, Valid: phone != ""},
|
||||||
|
|
||||||
Role: string(user.Role),
|
Role: string(user.Role),
|
||||||
Password: user.Password,
|
Password: user.Password,
|
||||||
|
|
@ -348,7 +355,7 @@ func (s *Store) CreateUser(
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.User{}, err
|
return domain.User{}, mapUserUniqueViolation(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var updatedAt *time.Time
|
var updatedAt *time.Time
|
||||||
|
|
@ -1033,9 +1040,11 @@ func (s *Store) PurgeDueUserDeletions(ctx context.Context, limit int32) (int64,
|
||||||
|
|
||||||
// CheckPhoneEmailExist checks if phone or email exists in an organization
|
// CheckPhoneEmailExist checks if phone or email exists in an organization
|
||||||
func (s *Store) CheckPhoneEmailExist(ctx context.Context, phone, email string) (phoneExists, emailExists bool, err error) {
|
func (s *Store) CheckPhoneEmailExist(ctx context.Context, phone, email string) (phoneExists, emailExists bool, err error) {
|
||||||
|
phone = normalizePhone(phone)
|
||||||
|
email = normalizeEmail(email)
|
||||||
res, err := s.queries.CheckPhoneEmailExist(ctx, dbgen.CheckPhoneEmailExistParams{
|
res, err := s.queries.CheckPhoneEmailExist(ctx, dbgen.CheckPhoneEmailExistParams{
|
||||||
PhoneNumber: pgtype.Text{String: phone},
|
PhoneNumber: pgtype.Text{String: phone, Valid: phone != ""},
|
||||||
Email: pgtype.Text{String: email},
|
Email: pgtype.Text{String: email, Valid: email != ""},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, false, err
|
return false, false, err
|
||||||
|
|
@ -1050,6 +1059,8 @@ func (s *Store) GetUserByEmailPhone(
|
||||||
email string,
|
email string,
|
||||||
phone string,
|
phone string,
|
||||||
) (domain.User, error) {
|
) (domain.User, error) {
|
||||||
|
email = normalizeEmail(email)
|
||||||
|
phone = normalizePhone(phone)
|
||||||
|
|
||||||
u, err := s.queries.GetUserByEmailPhone(ctx, dbgen.GetUserByEmailPhoneParams{
|
u, err := s.queries.GetUserByEmailPhone(ctx, dbgen.GetUserByEmailPhoneParams{
|
||||||
Email: pgtype.Text{
|
Email: pgtype.Text{
|
||||||
|
|
|
||||||
11
internal/repository/user_contact.go
Normal file
11
internal/repository/user_contact.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package repository
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func normalizeEmail(email string) string {
|
||||||
|
return strings.ToLower(strings.TrimSpace(email))
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizePhone(phone string) string {
|
||||||
|
return strings.TrimSpace(phone)
|
||||||
|
}
|
||||||
34
internal/repository/user_unique.go
Normal file
34
internal/repository/user_unique.go
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Yimaru-Backend/internal/domain"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mapUserUniqueViolation(err error) error {
|
||||||
|
if !IsUniqueViolation(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if !errors.As(err, &pgErr) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
name := strings.ToLower(pgErr.ConstraintName)
|
||||||
|
switch {
|
||||||
|
case strings.Contains(name, "email"):
|
||||||
|
return domain.ErrEmailAlreadyRegistered
|
||||||
|
case strings.Contains(name, "phone"):
|
||||||
|
return domain.ErrPhoneAlreadyRegistered
|
||||||
|
case strings.Contains(name, "google"):
|
||||||
|
return domain.ErrGoogleAccountAlreadyRegistered
|
||||||
|
case strings.Contains(name, "apple"):
|
||||||
|
return domain.ErrAppleAccountAlreadyRegistered
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -160,37 +160,10 @@ func (s *Service) LoginWithApple(
|
||||||
return domain.LoginSuccess{}, errors.New("missing apple user id")
|
return domain.LoginSuccess{}, errors.New("missing apple user id")
|
||||||
}
|
}
|
||||||
|
|
||||||
var user domain.User
|
user, isNewAccount, err := s.resolveAppleUser(ctx, aUser)
|
||||||
var err error
|
|
||||||
isNewAccount := false
|
|
||||||
|
|
||||||
user, err = s.userStore.GetUserByAppleID(ctx, aUser.ID)
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, domain.ErrUserNotFound) {
|
|
||||||
return domain.LoginSuccess{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if aUser.Email == "" {
|
|
||||||
return domain.LoginSuccess{}, errors.New("email is required on first sign in with apple")
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err = s.userStore.GetUserByEmailPhone(ctx, aUser.Email, "")
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, domain.ErrUserNotFound) {
|
|
||||||
return domain.LoginSuccess{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err = s.userStore.CreateAppleUser(ctx, aUser)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.LoginSuccess{}, err
|
return domain.LoginSuccess{}, err
|
||||||
}
|
}
|
||||||
isNewAccount = true
|
|
||||||
} else {
|
|
||||||
if err := s.userStore.LinkAppleAccount(ctx, user.ID, aUser.ID, aUser.VerifiedEmail); err != nil {
|
|
||||||
return domain.LoginSuccess{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if user.Status == domain.UserStatusPending {
|
if user.Status == domain.UserStatusPending {
|
||||||
return domain.LoginSuccess{}, domain.ErrUserNotVerified
|
return domain.LoginSuccess{}, domain.ErrUserNotVerified
|
||||||
|
|
|
||||||
|
|
@ -143,33 +143,10 @@ func (s *Service) LoginWithGoogle(
|
||||||
gUser domain.GoogleUser,
|
gUser domain.GoogleUser,
|
||||||
) (domain.LoginSuccess, error) {
|
) (domain.LoginSuccess, error) {
|
||||||
|
|
||||||
var user domain.User
|
user, isNewAccount, err := s.resolveGoogleUser(ctx, gUser)
|
||||||
var err error
|
|
||||||
isNewAccount := false
|
|
||||||
|
|
||||||
user, err = s.userStore.GetUserByGoogleID(ctx, gUser.ID)
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, domain.ErrUserNotFound) {
|
|
||||||
return domain.LoginSuccess{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err = s.userStore.GetUserByEmailPhone(ctx, gUser.Email, "")
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, domain.ErrUserNotFound) {
|
|
||||||
return domain.LoginSuccess{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err = s.userStore.CreateGoogleUser(ctx, gUser)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.LoginSuccess{}, err
|
return domain.LoginSuccess{}, err
|
||||||
}
|
}
|
||||||
isNewAccount = true
|
|
||||||
} else {
|
|
||||||
if err := s.userStore.LinkGoogleAccount(ctx, user.ID, gUser.ID); err != nil {
|
|
||||||
return domain.LoginSuccess{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if user.Status == domain.UserStatusPending {
|
if user.Status == domain.UserStatusPending {
|
||||||
return domain.LoginSuccess{}, domain.ErrUserNotVerified
|
return domain.LoginSuccess{}, domain.ErrUserNotVerified
|
||||||
|
|
|
||||||
170
internal/services/authentication/oauth_resolve.go
Normal file
170
internal/services/authentication/oauth_resolve.go
Normal file
|
|
@ -0,0 +1,170 @@
|
||||||
|
package authentication
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Yimaru-Backend/internal/domain"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isIdentityConflict(err error) bool {
|
||||||
|
return errors.Is(err, domain.ErrEmailAlreadyRegistered) ||
|
||||||
|
errors.Is(err, domain.ErrPhoneAlreadyRegistered) ||
|
||||||
|
errors.Is(err, domain.ErrGoogleAccountAlreadyRegistered) ||
|
||||||
|
errors.Is(err, domain.ErrAppleAccountAlreadyRegistered)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) resolveGoogleUser(
|
||||||
|
ctx context.Context,
|
||||||
|
gUser domain.GoogleUser,
|
||||||
|
) (domain.User, bool, error) {
|
||||||
|
if user, err := s.userStore.GetUserByGoogleID(ctx, gUser.ID); err == nil {
|
||||||
|
return user, false, nil
|
||||||
|
} else if !errors.Is(err, domain.ErrUserNotFound) {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, err := s.userStore.GetUserByEmailPhone(ctx, gUser.Email, ""); err == nil {
|
||||||
|
if err := s.linkGoogleAccountSafe(ctx, user.ID, gUser.ID); err != nil {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
user, err = s.userStore.GetUserByGoogleID(ctx, gUser.ID)
|
||||||
|
if err != nil {
|
||||||
|
user, err = s.userStore.GetUserByEmailPhone(ctx, gUser.Email, "")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
return user, false, nil
|
||||||
|
} else if !errors.Is(err, domain.ErrUserNotFound) {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := s.userStore.CreateGoogleUser(ctx, gUser)
|
||||||
|
if err != nil {
|
||||||
|
user, recoverErr := s.recoverGoogleUserAfterConflict(ctx, gUser, err)
|
||||||
|
if recoverErr != nil {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
return user, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) resolveAppleUser(
|
||||||
|
ctx context.Context,
|
||||||
|
aUser domain.AppleUser,
|
||||||
|
) (domain.User, bool, error) {
|
||||||
|
if user, err := s.userStore.GetUserByAppleID(ctx, aUser.ID); err == nil {
|
||||||
|
return user, false, nil
|
||||||
|
} else if !errors.Is(err, domain.ErrUserNotFound) {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if aUser.Email == "" {
|
||||||
|
return domain.User{}, false, errors.New("email is required on first sign in with apple")
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, err := s.userStore.GetUserByEmailPhone(ctx, aUser.Email, ""); err == nil {
|
||||||
|
if err := s.linkAppleAccountSafe(ctx, user.ID, aUser.ID, aUser.VerifiedEmail); err != nil {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
user, err = s.userStore.GetUserByAppleID(ctx, aUser.ID)
|
||||||
|
if err != nil {
|
||||||
|
user, err = s.userStore.GetUserByEmailPhone(ctx, aUser.Email, "")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
return user, false, nil
|
||||||
|
} else if !errors.Is(err, domain.ErrUserNotFound) {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := s.userStore.CreateAppleUser(ctx, aUser)
|
||||||
|
if err != nil {
|
||||||
|
user, recoverErr := s.recoverAppleUserAfterConflict(ctx, aUser, err)
|
||||||
|
if recoverErr != nil {
|
||||||
|
return domain.User{}, false, err
|
||||||
|
}
|
||||||
|
return user, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) linkGoogleAccountSafe(ctx context.Context, userID int64, googleID string) error {
|
||||||
|
err := s.userStore.LinkGoogleAccount(ctx, userID, googleID)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if errors.Is(err, domain.ErrGoogleAccountAlreadyRegistered) {
|
||||||
|
_, lookupErr := s.userStore.GetUserByGoogleID(ctx, googleID)
|
||||||
|
return lookupErr
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) linkAppleAccountSafe(ctx context.Context, userID int64, appleID string, emailVerified bool) error {
|
||||||
|
err := s.userStore.LinkAppleAccount(ctx, userID, appleID, emailVerified)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if errors.Is(err, domain.ErrAppleAccountAlreadyRegistered) {
|
||||||
|
_, lookupErr := s.userStore.GetUserByAppleID(ctx, appleID)
|
||||||
|
return lookupErr
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) recoverGoogleUserAfterConflict(
|
||||||
|
ctx context.Context,
|
||||||
|
gUser domain.GoogleUser,
|
||||||
|
createErr error,
|
||||||
|
) (domain.User, error) {
|
||||||
|
if !isIdentityConflict(createErr) {
|
||||||
|
return domain.User{}, createErr
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, err := s.userStore.GetUserByGoogleID(ctx, gUser.ID); err == nil {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, err := s.userStore.GetUserByEmailPhone(ctx, gUser.Email, ""); err == nil {
|
||||||
|
if linkErr := s.linkGoogleAccountSafe(ctx, user.ID, gUser.ID); linkErr != nil {
|
||||||
|
return domain.User{}, linkErr
|
||||||
|
}
|
||||||
|
if user, err := s.userStore.GetUserByGoogleID(ctx, gUser.ID); err == nil {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return domain.User{}, createErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) recoverAppleUserAfterConflict(
|
||||||
|
ctx context.Context,
|
||||||
|
aUser domain.AppleUser,
|
||||||
|
createErr error,
|
||||||
|
) (domain.User, error) {
|
||||||
|
if !isIdentityConflict(createErr) {
|
||||||
|
return domain.User{}, createErr
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, err := s.userStore.GetUserByAppleID(ctx, aUser.ID); err == nil {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if user, err := s.userStore.GetUserByEmailPhone(ctx, aUser.Email, ""); err == nil {
|
||||||
|
if linkErr := s.linkAppleAccountSafe(ctx, user.ID, aUser.ID, aUser.VerifiedEmail); linkErr != nil {
|
||||||
|
return domain.User{}, linkErr
|
||||||
|
}
|
||||||
|
if user, err := s.userStore.GetUserByAppleID(ctx, aUser.ID); err == nil {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return domain.User{}, createErr
|
||||||
|
}
|
||||||
|
|
@ -12,18 +12,24 @@ func (s *Service) CheckPhoneEmailExist(ctx context.Context, phoneNum, email stri
|
||||||
|
|
||||||
func (s *Service) SendRegisterCode(ctx context.Context, medium domain.OtpMedium, sentTo string, provider domain.SMSProvider) error {
|
func (s *Service) SendRegisterCode(ctx context.Context, medium domain.OtpMedium, sentTo string, provider domain.SMSProvider) error {
|
||||||
var err error
|
var err error
|
||||||
|
var existing domain.User
|
||||||
// check if user exists
|
// check if user exists
|
||||||
switch medium {
|
switch medium {
|
||||||
case domain.OtpMediumEmail:
|
case domain.OtpMediumEmail:
|
||||||
_, err = s.userStore.GetUserByEmailPhone(ctx, sentTo, "")
|
existing, err = s.userStore.GetUserByEmailPhone(ctx, sentTo, "")
|
||||||
case domain.OtpMediumSms:
|
case domain.OtpMediumSms:
|
||||||
_, err = s.userStore.GetUserByEmailPhone(ctx, "", sentTo)
|
existing, err = s.userStore.GetUserByEmailPhone(ctx, "", sentTo)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && err != domain.ErrUserNotFound {
|
if err != nil && err != domain.ErrUserNotFound {
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err == nil && existing.ID != 0 {
|
||||||
|
if medium == domain.OtpMediumEmail {
|
||||||
|
return domain.ErrEmailAlreadyRegistered
|
||||||
|
}
|
||||||
|
return domain.ErrPhoneAlreadyRegistered
|
||||||
|
}
|
||||||
|
|
||||||
// send otp based on the medium
|
// send otp based on the medium
|
||||||
return s.SendOtp(ctx, 0, sentTo, domain.OtpRegister, medium, provider)
|
return s.SendOtp(ctx, 0, sentTo, domain.OtpRegister, medium, provider)
|
||||||
|
|
@ -36,15 +42,12 @@ func (s *Service) RegisterUser(ctx context.Context, registerReq domain.RegisterU
|
||||||
return domain.User{}, err
|
return domain.User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if registerReq.OtpMedium == domain.OtpMediumEmail {
|
if registerReq.Email != "" && emailExists {
|
||||||
if emailExists {
|
|
||||||
return domain.User{}, domain.ErrEmailAlreadyRegistered
|
return domain.User{}, domain.ErrEmailAlreadyRegistered
|
||||||
}
|
}
|
||||||
} else {
|
if registerReq.PhoneNumber != "" && phoneExists {
|
||||||
if phoneExists {
|
|
||||||
return domain.User{}, domain.ErrPhoneAlreadyRegistered
|
return domain.User{}, domain.ErrPhoneAlreadyRegistered
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Hash the password
|
// Hash the password
|
||||||
hashedPassword, err := hashPassword(registerReq.Password)
|
hashedPassword, err := hashPassword(registerReq.Password)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user