Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create user firebase errors #473

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Loading