Created
June 28, 2019 06:00
-
-
Save lucianomlima/3d2f407180d9bf05ab57b410eea26088 to your computer and use it in GitHub Desktop.
Save user custom data after signup with firebase
This file contains hidden or 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
// Users collection | |
const usersRef = firebase.firestore().collection('users'); | |
// User signup with email and password | |
firebase.auth().createUserWithEmailAndPassword(email, password) | |
.then(async (credential) => { | |
const user = { | |
uid: credential.uid, | |
// Custom data | |
name, | |
nickname, | |
age, | |
}; | |
// Create a new document on users collection with credential UID | |
usersRef.doc(credential.uid).set(user) | |
.then(() => { | |
// User document created successfully | |
}) | |
.catch(() => { | |
// Error when creating user document | |
}); | |
}) | |
.catch((error) => { | |
// Error when create credential - invalid email | |
if (error.code === 'auth/invalid-email') { | |
} | |
// Error when create credential - email already registered | |
if (error.code === 'auth/email-already-in-use') { | |
} | |
// See all error codes on https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sign-inwith-email-and-password | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment