Build A MERN Stack App in 5 Days (Day 3: Setting up Redux & Firebase Login)
Tags: react, firebase, redux, javascript
Published: 2022-03-28 04:18:17

Welcome to Day 3! In the previous two parts, we have set up our backend and designed the Header, Sidebar & Widgets components in our UI. Before we design the Feed component, we need to implement User Authentication so that we can fetch our posts from the database. What we'll do today:
- Implement Firebase Google Auth
- Setup Redux
- Add Login & Logout functionalities 

<h2> Firebase Auth </h2>
Head over to https://console.firebase.google.com/u/1/ and 'Add project'. Name your project and Create a new 'Web' application and copy/paste the SDK configuration.

![sdk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68tc1sab97zdfnkmmshp.png)

<b>Go to the 'Authentication' tab and in the 'Sign-in methods',enable Google </b> . Go ahead and install Firebase in the 'client' directory by using 'npm i firebase'. Create a file named 'firebase.js' in the 'src' folder and copy paste the Firebase configuration alongwith some other imports as such:

![config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kge56gpsqh8bti9587la.png)

<h2> Redux </h2>
Go ahead and install 'react-redux' and '@reduxjs/toolkit'. Create a folder called 'features' in the src directory and create a new file called 'userSlice.js' in the features folder. This file will include the reducers and actions, namely login and logout for our user. In the userSlice.js:

![user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pky09n6pr2d93kbfdo0t.png)

Create a 'store.js' file in the src directory where we import our userSlice and initialize the global store, so that we can use it anywhere in our app and avoid prop drilling!

![store](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itoc9h7t9f0hnooy20og.png)

![index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k93wryn6yrws595j8vh8.png)

With our global Redux store & Firebase Auth set up,all that's left to do is create a Login page which will leverage the Firebase Auth to enable User Login and fill our global user object.

<h3> Login </h3>
In the 'pages' directory, create a 'Login.jsx' file. For now,it will be a simple page with a Logo and a Login Button. Feel free to customize it as per your needs and add more Authentication Providers!
Let's create a 'handleLogin' utility function which will do just what it's name suggests.

![handle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pezykffwywx2divsu87x.png)

![loginpage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqvcru6lmbb53pr397hu.png)

![logpage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kytjk1eahvgswq0r0zc8.png)

![modal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q237276nb31rp7mpsjnj.png)

Now,in our App.js let's render Login page/Home page depending upon the user's auth state. The user can visit the 'Home' page only after they have logged in.

![app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lp9tzx6zxk15uz1mzjw.png)

<h2> Logout </h2>
With the Login functionality done, lets add an option to Logout when the user clicks on their Avatar in the Header. Head over to 'Header.jsx' in the 'components' directory.

![loghand](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knqn8y1bdbmfpv5bxlcl.png)

![logout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu9ersr878t3fce3ptp8.png)

![hello](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqvd0hvk3zqsddlzx5xf.png)

![sure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzz3njytkhkdd3fb2o29.png)

With our login & logout functionalities implemented, in the next part we will add functionality to Add Question and Fetch Question from our database and render it in the Feed component, making our application fully functional.

<h3> Homework </h3>
- Add more Providers for Login (Github/Facebook)
- Customize the Login page 

<h3> References </h3>
[Code Repo](https://github.com/kishibe-rohan/kysymys-tutorial)
[Redux Toolkit](https://redux-toolkit.js.org/)
[Firebase](https://console.firebase.google.com/u/1/)

See you in the next part!