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

feat: Add google auth #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: Add google auth #53

wants to merge 1 commit into from

Conversation

gabrielmachin
Copy link
Collaborator

NOTION Ticket

Type of change

  • Fix
  • Story
  • Chore

Description of the change

Add google auth using firebase-admin library.

Copy link
Collaborator

@mlvieras mlvieras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add instructions on the Readme for how to setup firebase? What does a person that wants to setup the project need to do to get Firebase running? Where are credentials stored? What does the frontend need to do to bridge the gap? (can be a link to a tutorial or documentation, I just want it referenced somewhere)

@@ -0,0 +1,10 @@
import Firebase from 'firebase-admin';
import serviceAccount from './ServiceAccount.json';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we want this. This will always crash on startup if the developer does not have this file. What if they don't want to support Google auth? Maybe we should conditionally import this file.

Copy link
Collaborator Author

@gabrielmachin gabrielmachin Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm trying to make that work with dynamic import.

@@ -67,6 +75,82 @@ export class AuthService {
};
};

static firebaseAuth = async (idToken: SocialAuth): Promise<ReturnAuth> => {
const decodeValue = await getAuth().verifyIdToken(idToken.idToken);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try-catch this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be useful to just throw the error given by firebase in case that something goes wrong? Or do we want to log it and throw a more generic error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'm not sure, depends on what errors might be thrown. Some we might not want to throw. Can you please check what that method throws? We might not need to do anything, I just want to be sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will look into it.

if (!email) throw new ApiError(errors.INVALID_CREDENTIALS);

const cryptPassword = await bcrypt.hash(
faker.internet.password({ length: 30 }),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we should use faker in production. I'd prefer randomly generating a string of characters.

let userAlreadyExist = false;

try {
user = await prisma.user.create({ data });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we call the UserService here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean calling the create method of the UserService, I didn't use it since there are some differences between the behavior of the sign up with email/password and the one with firebase, and didn't want to make it more complex. But maybe it's not as complex as I imagine.

If you mean just creating a new method in the UserService for creating the user with firebase auth then I think it's a good idea to move it to the UserService.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not that complex, already fix this.

name,
email,
password: cryptPassword,
signUpMethod: SignUpMethod.GOOGLE,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also do this with the PASSWORD method on the register method above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's done in the UserService.create() method.

}

if (!userAlreadyExist) {
addToMailQueue('Sign up Email', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a dictionary of valid email identifiers and use it here. This can be prone to bugs since a small typo could prevent sending an email. What do you think? We could create a ticket for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants