React Native Auth Firebase
React Native Auth Firebase
2. Firebase
The first thing you'll need to have is an active Firebase project.
If you already have an existing project you can skip to the Platform Specific
Installation section.
a. Create New Project
- Visit the Firebase console.
- Click the Add project box as shown below:
- A dialog will appear as shown below
• Enter your new project name and modify the project id and region if
necessary
• Click Create Project when finished
• Your project will now be created - this can take a few seconds
• Once created click the Continue button
Once downloaded, place this file in the root of your project at android/app/google-
services.json.
In order for Android to parse this file, add the google-services gradle plugin as a
dependency to your project in the project level build.gradle file:
buildscript {
//...
dependencies {
//...
classpath 'com.google.gms:google-services:4.2.0'
}
}
To apply the plugin to your project, add the following to the VERY BOTTOM of your
app android/app/build.gradle file:
dependencies {
// This should be here already
implementation project(':react-native-firebase')
// Firebase dependencies
implementation "com.google.android.gms:play-services-
base:16.1.0"
implementation "com.google.firebase:firebase-core:16.0.9"
...
dependencies {
// ...
implementation "com.google.firebase:firebase-
auth:17.0.0"
// ...
import io.invertase.firebase.RNFirebasePackage;
import io.invertase.firebase.auth.RNFirebaseAuthPackage; // <-- Add this
line
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNFirebasePackage(),
new RNFirebaseAuthPackage() // <-- Add this line
);
}
};
// ...
}
const successImageUri =
'https://cdn.pixabay.com/photo/2015/06/09/16/12/icon-803718_1280.png';
componentDidMount() {
this.unsubscribe = firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({ user: user.toJSON() });
} else {
// User has been signed out, reset the state
this.setState({
user: null,
message: '',
codeInput: '',
phoneNumber: '+44',
confirmResult: null,
RNFirebase Authentication – Rintis M. S. 5
});
}
});
}
componentWillUnmount() {
if (this.unsubscribe) this.unsubscribe();
}
signIn = () => {
const { phoneNumber } = this.state;
this.setState({ message: 'Sending code ...' });
firebase.auth().signInWithPhoneNumber(phoneNumber)
.then(confirmResult => this.setState({ confirmResult, message: 'Code has
been sent!' }))
.catch(error => this.setState({ message: `Sign In With Phone Number
Error: ${error.message}` }));
};
confirmCode = () => {
const { codeInput, confirmResult } = this.state;
signOut = () => {
firebase.auth().signOut();
}
renderPhoneNumberInput() {
const { phoneNumber } = this.state;
renderMessage() {
const { message } = this.state;
return (
<Text style={{ padding: 5, backgroundColor: '#000', color: '#fff'
}}>{message}</Text>
);
}
renderVerificationCodeInput() {
const { codeInput } = this.state;
return (
<View style={{ marginTop: 25, padding: 25 }}>
<Text>Enter verification code below:</Text>
<TextInput
autoFocus
style={{ height: 40, marginTop: 15, marginBottom: 15 }}
onChangeText={value => this.setState({ codeInput: value })}
placeholder={'Code ... '}
value={codeInput}
/>
render() {
const { user, confirmResult } = this.state;
return (
<View style={{ flex: 1 }}>
{this.renderMessage()}
{user && (
<View
style={{
padding: 15,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#77dd77',
flex: 1,
}}
>
<Image source={{ uri: successImageUri }} style={{ width: 100, height:
100, marginBottom: 25 }} />
<Text style={{ fontSize: 25 }}>Signed In!</Text>
<Text>{JSON.stringify(user)}</Text>
<Button title="Sign Out" color="red" onPress={this.signOut} />
</View>
)}
</View>
);
}
}