Last active
July 31, 2019 15:41
-
-
Save clairechabas/2d24d98296bc84b21acc96606dd6f819 to your computer and use it in GitHub Desktop.
Sign-up action creator using Firebase Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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