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

Add Firebase authentication for Merchant Sign Up #54

Merged
merged 73 commits into from
Jun 29, 2020

Conversation

karenfrilya97
Copy link
Collaborator

@karenfrilya97 karenfrilya97 commented Jun 22, 2020

We decided to use Firebase to handle authentication since it has a lot of authentication capabilities.
This PR adds Firebase code to handle authentication when merchant signs up.
This also stores merchant's account to Firebase so merchant can sign in with then same account in the future.

Some additional feature given by Firebase Authentication is checking email format, password strength and if existing user has the same email.

@karenfrilya97 karenfrilya97 changed the base branch from master to karen/sign-up-handling June 22, 2020 04:27
Copy link
Collaborator

@hzxcaryn hzxcaryn left a comment

Choose a reason for hiding this comment

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

We should not couple firebase with our component! Here's what you can do:

  • Initialise firebase in a firebase.ts file in web/src. This file should contain the firebase.initializeApp() line and things like const auth = firebase.auth; and export it and use that instead.

Other comments:

  • Change to use react hooks and Functional Components! :D
  • Using arrow functions will remove the need for binding so we can remove autobind as well.

web/app.yaml Show resolved Hide resolved
web/src/firebase-auth.ts Outdated Show resolved Hide resolved
const PASSWORDS_DO_NOT_MATCH = 'Passwords do not match.';

const INVALID_EMAIL = 'Please input a valid email address.';
const WEAK_PASSWORD = 'Password should be at least 6 characters.';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this error? Is there a error msg sent from firebase?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah firebase has these errors.

setConfirmPassword(event.target.value);
};

const handleVpaChange = (event: React.ChangeEvent<HTMLInputElement>) => {
event.target.value ? setVpaError('') : setVpaError(VPA_EMPTY);
event.target.value ? setVpaError('') : setVpaError(Errors.VPA_EMPTY);
setVpa(event.target.value);
};

const handleSubmit = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you disable the submit button if there is some error

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay!

setConfirmPasswordError(Errors.PASSWORDS_DO_NOT_MATCH);
if (!vpa) setVpaError(Errors.VPA_EMPTY);

if (!name || !email || !password || password !== confirmPassword || !vpa) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to avoid doing a second time check here? If there is a growing list of validation checks (e.g., 20 items), this is a long line of repetition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to using library so we don't have to do this.

try {
await firebaseAuth.createUserWithEmailAndPassword(email, password);
} catch (error) {
if (error.code === 'auth/invalid-email') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can change to switch statement?

"Generally speaking, if-else is best used when there are two discrete values or a few different ranges of values for which to test. When there are more than two discrete values for which to test, the switch statement is the most optimal choice" - source

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay! Yep, switch-case is better in this case.

Base automatically changed from karen/sign-up-handling to master June 29, 2020 03:54
@karenfrilya97 karenfrilya97 merged commit c2bc798 into master Jun 29, 2020
@karenfrilya97 karenfrilya97 deleted the karen/firebase-sign-up branch June 29, 2020 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants