Created
October 2, 2017 07:52
-
-
Save krizzu/92890c9cbb506fd08fb1975bd7c319ed to your computer and use it in GitHub Desktop.
Example for simple authentication using RNFirebase
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
// ... | |
// Inside our Component's class | |
_authenticateUser = async () => { | |
const { firebase } = this.props; // Parent passes reference to Firebase through props | |
const { userPassword, userEmail } = this.state; | |
let userInfo = null; | |
try { | |
userInfo = await firebase.auth().signInWithEmailAndPassword(userEmail, userPassword) | |
} catch(error) { | |
// Error will contain information like Invalid credentials or User does not exist | |
return this.setState(() => ({ | |
authError: error.toString(), | |
isFetchingAuth: false, | |
isAuthenticated: false | |
})) | |
} | |
// We're authenticated! | |
// We don't need to store userInfo, just check if value is not null. | |
this.setState(() => ({ | |
isFetchingAuth: false, | |
isAuthenticated: !!userInfo | |
})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment