Skip to content

Instantly share code, notes, and snippets.

@clairechabas
Last active July 31, 2019 15:41
Show Gist options
  • Save clairechabas/2d24d98296bc84b21acc96606dd6f819 to your computer and use it in GitHub Desktop.
Save clairechabas/2d24d98296bc84b21acc96606dd6f819 to your computer and use it in GitHub Desktop.
Sign-up action creator using Firebase Auth
import { SIGNUP_SUCCESS, SIGNUP_ERROR } from "./actionTypes";
import firebase from "../../services/firebase";
// Signing up with Firebase
export const signup = (email, password) => async dispatch => {
try {
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then(dataBeforeEmail => {
firebase.auth().onAuthStateChanged(function(user) {
user.sendEmailVerification();
});
})
.then(dataAfterEmail => {
firebase.auth().onAuthStateChanged(function(user) {
if (user.emailVerified) {
// Email is verified
dispatch({
type: SIGNUP_SUCCESS,
payload:
"Your account was successfully created! Now you need to verify your e-mail address, please go check your inbox."
});
} else {
// Email is not verified
dispatch({
type: SIGNUP_ERROR,
payload:
"Something went wrong, we couldn't create your account. Please try again."
});
}
});
})
.catch(function(error) {
dispatch({
type: SIGNUP_ERROR,
payload:
"Something went wrong, we couldn't create your account. Please try again."
});
});
} catch (err) {
dispatch({
type: SIGNUP_ERROR,
payload:
"Something went wrong, we couldn't create your account. Please try again."
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment