Email Authentication with React native and Firebase
Tags: react, javascript, reactnative
Published: 2019-04-19 09:59:42

---
title: Email Authentication with React native and Firebase
published: true
tags: reactjs,javascript,react-native,react
canonical_url: https://medium.com/front-end-weekly/email-authentication-with-react-native-and-firebase-9670609a3557
---

#### In each and every app, you will see several kinds of authentication like login with Facebook, Google etc. But still, people prefer to entering email and password. In this tutorial, we are going to integrate Email authentication with React native and Firebase. Using firebase makes it easier to use. Let me tell you how?

![](https://cdn-images-1.medium.com/max/940/1*QjW6170L-FbtceBPQeB5kw.png)

Let’s get started. So we are going to start it from scratch. Begin with

### Creating new react native project with expo:

We are not going to use react-native-cli instead of that we will use Expo. This will make our task a little bit easier. Because with react-native-cli, we need to install XCode or Android studio to at least run and test an application.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F788447%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe>

**To make an app with Expo, you need to type in terminal or CMD:**

**expo init react-native-firebase**

![](https://cdn-images-1.medium.com/max/1017/1*exsdG3TQ42ezlqVhjhTMsQ.png)

Once you hit enter, installation started. Once the installation is completed open project with VS Code.

![](https://cdn-images-1.medium.com/max/1017/1*bk6EzrBbgqklwLQhvM_RAA.png)

To run the app you need go inside it using **cd app-folder-name** then run the command **yarn start.**

![](https://cdn-images-1.medium.com/max/1024/1*RzPQZqA07UH5TEyvQ58Meg.png)

Once you run the app, the expo will open a new browser window.

![](https://cdn-images-1.medium.com/max/1024/1*LYRaz_caBlP8j-4CrWR2sQ.png)

You can see a lot of options available to run emulators for Android and iOS as well as QR Code. Here we are using the iOS simulator to run this app. Then after opening that simulator or use your device for open expo app.

There you will see the screen like as shown below:

![](https://cdn-images-1.medium.com/max/1024/1*32KpAm8viRiNkBGzediHYg.png)

If everything works properly then we are ready to go. So let’s start the real coding:

### Bootstrap form quickly with a native base

To develop our interface quickly, we are using Nativebase for development.

![](https://cdn-images-1.medium.com/max/1024/1*iXoNtRcrpY5cYkt9nyV-2A.png)

**To install native base** , open a new terminal run the command given below:

**npm i native-base -s**

![](https://cdn-images-1.medium.com/max/1024/1*diK-LWKPpGi5jo4TYIGcVQ.png)

Don’t worry if you will some errors while native base installation takes place. **Stop expo and run**  **npm install again.**

![](https://cdn-images-1.medium.com/max/1024/1*89K8z6jqWYN6aus-k9mIzw.png)

and we will back for normal mode

### Setup form with Native base components

So now, we are ready to import necessary Nativebase component to to App.js

```
import { Container, Item, Form, Input, Button, Label } from "native-base";
```

and after importing we will construct a user interface for the frontend form.

```
render() {
**return** (
 \<Container\>
 \<Form\>
 \<Item floatingLabel\>
 \<Label\>Email\</Label\>
 \<Input autoCapitalize="none" autoCorrect={false} /\>
 \</Item\>
 \<Item floatingLabel\>
 \<Label\>Password\</Label\>
 \<Input
 secureTextEntry={true}
 autoCapitalize="none"
 autoCorrect={false}
 /\>
 \</Item\>
 \<Button full rounded\>
 \<Text\>SignIn\</Text\>
 \</Button\>
 \</Form\>
 \</Container\>
 );
 }
```

Once you save the code shown above and then you will be able to see the instant result on the screen. This concept is also known as Hot Reloading.

![](https://cdn-images-1.medium.com/max/465/1*XZgwmM0YZyrw04LR4pnYCg.png)

So let’s make the form align center. To do this, we are going to play stylesheet properties. Just look at the code below, you can easily understand that we are playing with attributes in the stylesheet with containers.

```
\<Container style={styles.container}\>
```

By just adding a comment to styling we able to make it center.

```
**const** styles = StyleSheet.create({
 container: {
 flex: 1,
 backgroundColor: "#fff",
 // alignItems: "center",
 justifyContent: "center"
 }
});
```

Now save the code and see the results. All components aligned center. Awesome let’s move on to next step:

![](https://cdn-images-1.medium.com/max/509/1*b8vk4ZHyHYG0hmJa7qVTfg.png)

### Add firebase to React native project

To add firebase, we need to run the command **npm i firebase --save** and the after import firebase on App.js as shown below:

```
import \* as firebase from "firebase";
```

Next step is to **create firebase project.** Go to **firebase console,** create a project find apikey and configuration as shown in the image below:

![](https://cdn-images-1.medium.com/max/1024/1*WtL--ZtXbX0F3_5Bg2oFsQ.png)

Grab config and paste to App.js as shown in the below:

```
import \* as firebase from "firebase";
import { Container, Item, Form, Input, Button, Label } from "native-base";

var config = {
 apiKey: "AIzaSyDFdsjQWG8IFLXmviNqSiVZMw\_ADFl5tpo",
 authDomain: "react-native-firebase-3bde9.firebaseapp.com",
 databaseURL: "https://react-native-firebase-3bde9.firebaseio.com",
 projectId: "react-native-firebase-3bde9",
 storageBucket: "react-native-firebase-3bde9.appspot.com",
 messagingSenderId: "269398778466"
};

firebase.initializeApp(config);
```

Finally, we have successfully added firebase tp our project.

#### Sign Up

For email authentication, we need to activate Email authentication on Firebase console. Go to firebase console and enable it.

![](https://cdn-images-1.medium.com/max/1024/1*HzPcNSS8XKUdzVtnAa97mA.png)

Again get back to VS Code and add a signup button. So that user can fill up the information.

```
\<Button full rounded success style={{ marginTop: 20 }}\> \<Text\>Signup\</Text\>
\</Button\>
```

After adding the button you will be able to see this result with a Signin and signup button.

![](https://cdn-images-1.medium.com/max/1024/1*ZfvkxKlUEi6ALFyz1sEQ9Q.png)

Let’s code a little for signup button. You can follow along with me with all the steps. If you do that, there are rare chances to do mistakes and see any kind of error.

```
export default class App extends React.Component {
 constructor(props) {
 super(props);
 this.state = {
 email: "",
 password: ""
 };
 }
 SignUp = (email, password) =\> {
 try {
 firebase.auth().createUserWithEmailAndPassword(email, password);
 } catch (error) {
 console.log(error.toString(error));
 }
 };
```

Now, We create a state which handles email and password from the form and creates SignUp function for handling firebase code.

The next step is to **add form value to state with onChangeText.**

```
\<Item floatingLabel\>
 \<Label\>Email\</Label\>
 \<Input
 autoCapitalize="none"
 autoCorrect={false}
 onChangeText={email =\> this.setState({ email })}
 /\>
 \</Item\>
 \<Item floatingLabel\>
 \<Label\>Password\</Label\>
 \<Input
 secureTextEntry={true}
 autoCapitalize="none"
 autoCorrect={false}
 onChangeText={password =\> this.setState({ password })}
 /\>
```

We will trigger **SignUp function** with the **onPress event** for Signup button.

```
onPress={() =\> this.SignUp(this.state.email, this.state.password)}
```

Let’s try to save and submit form to test that we going in the right direction.

![](https://cdn-images-1.medium.com/max/509/1*m6qmo8ogo712er_edLfwQg.png)

After entering and hitting the signup, goto firebase console. **Hola, we have done it.** See the firebase database, there you can see the value we have entered.

![](https://cdn-images-1.medium.com/max/1024/1*96IDGlljZ8vR0oRwFMZccQ.png)

Now we have added successfully a new user to Firebase.

#### Sign In

Let’s code for sign in button. We use code from sign up method and change a little bit in firebase function. So that we can achieve what we want.

```
SignIn = (email, password) =\> {
**try** {
 firebase.auth().signInWithEmailAndPassword(email, password);
 firebase.auth().onAuthStateChanged(user =\> {
 alert(user.email);
 })

} **catch** (error) {
 console.log(error.toString(error));
 }
 };
```

Next, use **onAuthStateChanged** to getting user’s data from firebase. So, if we press the button then an onPress even call through to **SignIn** Buton which checks for right email and password.

```
onPress={() =\> this.SignIn(this.state.email, this.state.password)}
```

So let’s try to sign in.

![](https://cdn-images-1.medium.com/max/509/1*AP6IOJ_cnfDdNpSl6sZP0g.png)

It works. Finally, we did it. You can see everything works absolutely fine.

#### Conclusion

We have used Expo, Nativebase, Firebase. I hope you have learned. If you got stuck somewhere the following [GitHub](https://github.com/krissnawat/react-native-firebase-tutorial) repo. There you will get the whole code. If you fee its worth, keep sharing.

#### Learn More

- [Master React Native Animations | Udemy](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1331625&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fmaster-react-native-animations%2F)
- [React Native: Advanced Concepts | Udemy](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1172996&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Freact-native-advanced%2F)
- [The Practical Guide to React Native: Build iOS/Android Apps](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1436092&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Freact-native-the-practical-guide%2F)
- [Multiplatform Mobile App Development with React Native | Coursera](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=467035.13079845234&type=2&murl=https%3A%2F%2Fwww.coursera.org%2Flearn%2Freact-native)

### Disclaimer

_This post contains affiliate links to products. We may receive a commission for purchases made through these links._

* * *