Skip to content

Commit 1830240

Browse files
authored
fix: create firebase user errors (#473)
1 parent 053bace commit 1830240

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/auth/auth.service.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier';
99
import { Logger } from 'src/logger/logger';
1010
import {
11+
CREATE_USER_ALREADY_EXISTS,
1112
CREATE_USER_FIREBASE_ERROR,
1213
CREATE_USER_INVALID_EMAIL,
1314
CREATE_USER_WEAK_PASSWORD,
@@ -55,24 +56,21 @@ export class AuthService {
5556
return firebaseUser;
5657
} catch (err) {
5758
const errorCode = err.code;
59+
5860
if (errorCode === 'auth/invalid-email') {
5961
this.logger.warn(
6062
`Create user: user tried to create email with invalid email: ${email} - ${err}`,
6163
);
6264
throw new HttpException(CREATE_USER_INVALID_EMAIL, HttpStatus.BAD_REQUEST);
63-
}
64-
if (
65-
errorCode === 'auth/weak-password' ||
66-
err.message.includes('The password must be a string with at least 6 characters')
67-
) {
65+
} else if (errorCode === 'auth/weak-password' || errorCode === 'auth/invalid-password') {
6866
this.logger.warn(`Create user: user tried to create email with weak password - ${err}`);
6967
throw new HttpException(CREATE_USER_WEAK_PASSWORD, HttpStatus.BAD_REQUEST);
70-
}
71-
if (errorCode === 'auth/email-already-in-use' && errorCode === 'auth/email-already-exists') {
72-
this.logger.log(
73-
`Create user: Firebase user already exists so fetching firebase user: ${email}`,
74-
);
75-
return await this.getFirebaseUser(email);
68+
} else if (
69+
errorCode === 'auth/email-already-in-use' ||
70+
errorCode === 'auth/email-already-exists'
71+
) {
72+
this.logger.warn(`Create user: Firebase user already exists: ${email}`);
73+
throw new HttpException(CREATE_USER_ALREADY_EXISTS, HttpStatus.BAD_REQUEST);
7674
} else {
7775
this.logger.error(`Create user: Error creating firebase user - ${email}: ${err}`);
7876
throw new HttpException(CREATE_USER_FIREBASE_ERROR, HttpStatus.BAD_REQUEST);

src/user/user.service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class UserService {
6363
}
6464

6565
const firebaseUser = await this.authService.createFirebaseUser(email, password);
66+
6667
const user = await this.userRepository.save({
6768
...createUserDto,
6869
firebaseUid: firebaseUser.uid,

src/utils/errors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const CREATE_USER_FIREBASE_ERROR = 'CREATE_USER_FIREBASE_ERROR';
22
export const CREATE_USER_INVALID_EMAIL = 'CREATE_USER_INVALID_EMAIL';
33
export const CREATE_USER_WEAK_PASSWORD = 'CREATE_USER_WEAK_PASSWORD';
4+
export const CREATE_USER_ALREADY_EXISTS = 'CREATE_USER_ALREADY_EXISTS';

0 commit comments

Comments
 (0)