Fix: Correct inverted logic in signUpWithoutWorkspace causing 'User already exists' error (#15086)
## Problem When trying to signup on localhost:3001 with a new random email, users were getting an error message saying 'User already exists', even though they were new users. ## Root Cause The `signUpWithoutWorkspace` method in `sign-in-up.service.ts` had inverted logic when checking if a user exists. It was using `findUserByEmailOrThrow` which: - **Returns the user** if found - **Throws the provided error** if NOT found This caused the opposite behavior: - ❌ **New user (doesn't exist)**: Threw 'User already exists' error - ❌ **Existing user**: Continued to create duplicate user ## Solution Changed to use `findUserByEmail` and explicitly check if the user exists before throwing the appropriate error: ```typescript const existingUser = await this.userService.findUserByEmail(newUserParams.email); if (existingUser) { throw new AuthException( 'User already exist', AuthExceptionCode.USER_ALREADY_EXIST, { userFriendlyMessage: msg`User already exists` }, ); } ``` This matches the correct pattern already used in `signUpInWorkspace` (line 431-441 in auth.resolver.ts). ## Changes - Fixed inverted logic in `signUpWithoutWorkspace` method - Now correctly validates that user does NOT exist before creating new user - Matches the pattern used in `signUpInWorkspace` ## Testing The fix corrects the logic so that: - ✅ New users can sign up successfully - ✅ Existing users get the correct 'User already exists' error
This commit is contained in:
@@ -30,5 +30,5 @@ export const AuthExceptionCode = appendCommonExceptionCode({
|
||||
'TWO_FACTOR_AUTHENTICATION_PROVISION_REQUIRED',
|
||||
TWO_FACTOR_AUTHENTICATION_VERIFICATION_REQUIRED:
|
||||
'TWO_FACTOR_AUTHENTICATION_VERIFICATION_REQUIRED',
|
||||
USER_ALREADY_EXIST: 'USER_ALREADY_EXIST',
|
||||
USER_ALREADY_EXISTS: 'USER_ALREADY_EXISTS',
|
||||
} as const);
|
||||
|
||||
@@ -465,15 +465,18 @@ export class SignInUpService {
|
||||
newUserParams: SignInUpNewUserPayload,
|
||||
authParams: AuthProviderWithPasswordType['authParams'],
|
||||
) {
|
||||
await this.userService.findUserByEmailOrThrow(
|
||||
const userExists = await this.userService.findUserByEmail(
|
||||
newUserParams.email,
|
||||
new AuthException(
|
||||
'User already exist',
|
||||
AuthExceptionCode.USER_ALREADY_EXIST,
|
||||
{ userFriendlyMessage: msg`User already exists` },
|
||||
),
|
||||
);
|
||||
|
||||
if (userExists) {
|
||||
throw new AuthException(
|
||||
'User already exists',
|
||||
AuthExceptionCode.USER_ALREADY_EXISTS,
|
||||
{ userFriendlyMessage: msg`User already exists` },
|
||||
);
|
||||
}
|
||||
|
||||
return this.saveNewUser(
|
||||
await this.computePartialUserFromUserPayload(newUserParams, authParams),
|
||||
await this.setDefaultImpersonateAndAccessFullAdminPanel(),
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ export const authGraphqlApiExceptionHandler = (exception: AuthException) => {
|
||||
case AuthExceptionCode.SIGNUP_DISABLED:
|
||||
case AuthExceptionCode.MISSING_ENVIRONMENT_VARIABLE:
|
||||
case AuthExceptionCode.INVALID_JWT_TOKEN_TYPE:
|
||||
case AuthExceptionCode.USER_ALREADY_EXIST:
|
||||
case AuthExceptionCode.USER_ALREADY_EXISTS:
|
||||
throw new ForbiddenError(exception);
|
||||
case AuthExceptionCode.GOOGLE_API_AUTH_DISABLED:
|
||||
case AuthExceptionCode.MICROSOFT_API_AUTH_DISABLED:
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ export const getAuthExceptionRestStatus = (exception: AuthException) => {
|
||||
case AuthExceptionCode.MISSING_ENVIRONMENT_VARIABLE:
|
||||
case AuthExceptionCode.EMAIL_NOT_VERIFIED:
|
||||
case AuthExceptionCode.INVALID_JWT_TOKEN_TYPE:
|
||||
case AuthExceptionCode.USER_ALREADY_EXIST:
|
||||
case AuthExceptionCode.USER_ALREADY_EXISTS:
|
||||
return 403;
|
||||
case AuthExceptionCode.TWO_FACTOR_AUTHENTICATION_PROVISION_REQUIRED:
|
||||
case AuthExceptionCode.TWO_FACTOR_AUTHENTICATION_VERIFICATION_REQUIRED:
|
||||
|
||||
Reference in New Issue
Block a user