Skip to content

Commit

Permalink
fix: create firebase user errors (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Jun 13, 2024
1 parent 053bace commit 1830240
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier';
import { Logger } from 'src/logger/logger';
import {
CREATE_USER_ALREADY_EXISTS,
CREATE_USER_FIREBASE_ERROR,
CREATE_USER_INVALID_EMAIL,
CREATE_USER_WEAK_PASSWORD,
Expand Down Expand Up @@ -55,24 +56,21 @@ export class AuthService {
return firebaseUser;
} catch (err) {
const errorCode = err.code;

if (errorCode === 'auth/invalid-email') {
this.logger.warn(
`Create user: user tried to create email with invalid email: ${email} - ${err}`,
);
throw new HttpException(CREATE_USER_INVALID_EMAIL, HttpStatus.BAD_REQUEST);
}
if (
errorCode === 'auth/weak-password' ||
err.message.includes('The password must be a string with at least 6 characters')
) {
} else if (errorCode === 'auth/weak-password' || errorCode === 'auth/invalid-password') {
this.logger.warn(`Create user: user tried to create email with weak password - ${err}`);
throw new HttpException(CREATE_USER_WEAK_PASSWORD, HttpStatus.BAD_REQUEST);
}
if (errorCode === 'auth/email-already-in-use' && errorCode === 'auth/email-already-exists') {
this.logger.log(
`Create user: Firebase user already exists so fetching firebase user: ${email}`,
);
return await this.getFirebaseUser(email);
} else if (
errorCode === 'auth/email-already-in-use' ||
errorCode === 'auth/email-already-exists'
) {
this.logger.warn(`Create user: Firebase user already exists: ${email}`);
throw new HttpException(CREATE_USER_ALREADY_EXISTS, HttpStatus.BAD_REQUEST);
} else {
this.logger.error(`Create user: Error creating firebase user - ${email}: ${err}`);
throw new HttpException(CREATE_USER_FIREBASE_ERROR, HttpStatus.BAD_REQUEST);
Expand Down
1 change: 1 addition & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class UserService {
}

const firebaseUser = await this.authService.createFirebaseUser(email, password);

const user = await this.userRepository.save({
...createUserDto,
firebaseUid: firebaseUser.uid,
Expand Down
1 change: 1 addition & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const CREATE_USER_FIREBASE_ERROR = 'CREATE_USER_FIREBASE_ERROR';
export const CREATE_USER_INVALID_EMAIL = 'CREATE_USER_INVALID_EMAIL';
export const CREATE_USER_WEAK_PASSWORD = 'CREATE_USER_WEAK_PASSWORD';
export const CREATE_USER_ALREADY_EXISTS = 'CREATE_USER_ALREADY_EXISTS';

0 comments on commit 1830240

Please sign in to comment.