Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8) by pckurdu

View this thread on steempeak.com
· @pckurdu ·
$36.48
Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)
![react-redux.fw.png](https://cdn.steemitimages.com/DQmRpmFiQxCwe4jswNSCADhUgLnyH8Ce249cszhzrYXLGvC/react-redux.fw.png)
# Repository

### React
https://github.com/facebook/react

### Material-ui
https://github.com/mui-org/material-ui

### My Github Address
https://github.com/pckurdu

### This Project Github Address
https://github.com/pckurdu/Build-A-Blog-Site-With-React-Redux-Firebase-And-MaterializeCSS-Part-8-

# What Will I Learn?

- You will learn how to perform firebase auth operations using the redux.
- You will learn the callback functions of signInWithEmailAndPassword ().
- You will learn to access redux action in react component.
- You will learn to process the reducer operations and store data to the props in the react component.
- You will learn to update the state's unknown object with the setState () function 

# Requirements

- text editor (I used visual studio code editor)
- Basic javascript information
- Basic react information

# Difficulty

- Basic

# Tutorial Contents

In this tutorial we will perform sign in and sign out operations using `redux`.

We will create a action for the user to login. In action, we will create the necessary control environment and communicate with the `reducer`.When exporting the reducer to the store, we will provide the login control from the firebase with the action.

As a result of these transactions we will have information that the user is logged in or not but first we need to activate the component in which the user can enter their information. We created the signIn component in the previous tutorials. In this tutorial we will learn how to access the user-entered inputs.

After getting the user's login information, we will communicate with reducer and action in the signIn component. We will examine which values are coming to the signIn component and we will complete our code according to the information.

We will perform the sign out process after performing the login process. For the sign out process, we will define another action and in this action we will perform the firebase exit with the firebase function. We will do the action control with the reducer and forward the sign out information to the store.

This tutorial is divided into headings as follows:

- Create Login Action
- Activate SignIn Component
- Access Actions From SignIn Component
- Access Reducer and Store From SignIn Component
- Control of Login Processes

Let’ start and enjoy.

### 1-Create Login Action

We created the projectActions.js file to add data to firestore. Since we will perform authentication, we create another file. Under the `actions` folder, we will create the `authActions.js` file.

We will define a function in this file and use this function as action on the reducer side. Since this function will check the user information, it should take the input information with a parameter.

#### In authActions.js
```
//An action called logIn
export const logIn=(information)=>{
    return (dispatch,getState,{getFirebase})=>{
        //We're accessing the firebase
        const firebase = getFirebase();

        //Email and Password method to check the login.
        firebase.auth().signInWithEmailAndPassword(
            information.email,
            information.password
        )
    }
}
```
<br>
We defined `getFirebase()` in `index.js`.Using `getFirebase()`, we can access the firebase and use the functions in it.

The `auth ()` function in the firebase contains functions to perform authentication operations. We'll use the `signInWithEmailAndPassword()` function because we're activating the Email / Password login method.
![react1.png](https://cdn.steemitimages.com/DQmeB5KpSezxEZbVkk9sYhqJa36GsrDqd2MMVhrmkT8j3NW/react1.png)
<br>
`signInWithEmailAndPassword ()` returns whether the user is logged on by email or password. If the operation is successful, we can catch it with the `then()` function. If the login process is wrong or if the error occurred during the connection to the firebase, we will be notified by the `catch()` function.

#### In authActions.js
```
//Email and Password method to check the login.
        firebase.auth().signInWithEmailAndPassword(
            information.email,
            information.password
            //if login is successful
        ).then(()=>{
            dispatch({type:'LOGIN_SUCCESS'});
            //if login fails
        }).catch((err)=>{
            dispatch({type:'LOGIN_ERROR',err});
        });
```
<br>
As we complete the logIn action, we can switch to the reducer control.

We have created the `authReducer.js` file before. In this reducer, we will check the type of action and keep a variable with the help of error.

#### In authReducer.js
```
//default data is used if no state data is assigned.
const initState={
    //error variable
    authError:null
}
```
<br>
We will do according to the action type of the state and authError variable.
```
//create reducer with state and action parameters
const authReducer=(state=initState,action)=>{
    switch (action.type){
        case 'LOGIN_ERROR':
        console.log('login error');
        //Importing authError in state
        return{
            ...state,
            authError:'login failed'
        }
        case 'LOGIN_SUCCESS':
        console.log('login success');
        return{
            ...state,
            authError:null
        }
        default:
        return state;

    }
   
}
```
<br>
The reducer does not know which action is running and controls all actions. We can use action according to the type of action.

Now the reducer knows the result of the firebase signIn process. Using the components reducer, we can learn the result of the signIn process and do the operations accordingly.

Then let's prepare to use this reducer in the signIn component.

### 2- Activate SignIn Component

We have previously designed this page.
![react2.png](https://cdn.steemitimages.com/DQmcga18bspMstXDegGHAjjfNrujrHUsCPifyJVZM45DEGk/react2.png)
<br>
Let's enter the values entered into the input and push the button into the state of this component.
For these operations, we must first define the `state`.This state must be created to keep the state `email` and `password`.

### In SignIn.js
```
class SignIn extends Component { 
  //for inputs
  state = {
    email: '',
    password: ''
  }
```
<br>
As input values change, we must update these state values. For this we must trigger the input `onChange` event and define a function to run when triggered.
```
//will work when the input is changed.
  handleChange = (e) => {
    this.setState({
      [e.target.id]: e.target.value
    })
  } 
…
   {/* for email */}
   <div className="input-field">
      <label htmlFor="email">Email</label>
      <input type="email" id='email' onChange={this.handleChange} />
   </div>
   {/* for password */}
    <div className="input-field">
       <label htmlFor="password">Password</label>
       <input type="password" id='password' onChange={this.handleChange} />
    </div>
```
<br>
We used the `setState()` function to perform the state update. On this page we have defined two inputs and we used `e.target.value` to detect which input has changed. Thus the values in the inputs will be equal to the state.

When the submit event of the form is triggered, it will be retaining the final state of the state inputs. Let's create a function to trigger when the submit event occurs.
```
//will work when the form is submit.
  handleSubmit = (e) => {
    e.preventDefault();
    console.log(this.state);
  }
…
<form className="white" onSubmit={this.handleSubmit}> 
```
<br>
So we can access the input values.
![react1.gif](https://cdn.steemitimages.com/DQmRqc4HLmpJTvafy2grM4SBmEiPNdMSX4K3Za1DHReXJ8J/react1.gif)
<br>
The input information appears on the console screen.
![react3.png](https://cdn.steemitimages.com/DQmV56ZMd8eeXoVagvMxw9d7iVHFcebtf1fyDws9UKo1HFk/react3.png)
<br>
Now signIn component reducer is ready for use.

### 3- Access Actions From SignIn Component

Since we are going to connect to the reducer and store within the signIn component, we must use the `connect` module.

We must access the `logIn action` we created at the moment of connection. So we can run this action and control the firebase auth.

#### In SignIn.js
```
//To access store and reducer
import {connect} from 'react-redux';
//To access action
import {logIn} from '../../store/actions/authActions';
```
<br>
Let's create a function to access action from component. We should use this function for the second parameter of the connect function. The first parameter of the connect module accesses the store and we access the action with the second parameter.
```
//Function to access action
const mapDispatchToProps=(dispatch)=>{
  return{
    signIn:(info)=>dispatch(logIn(info))
  }
}
export default connect(null,mapDispatchToProps)(SignIn)
```
<br>
The generated `sigIn` represents the logIn function in action. With this function we can access this signIn with component props.

Then when we submit it, we can run this action with the information in the state.
```
//will work when the form is submit.
  handleSubmit = (e) => {
    e.preventDefault();
    //action function
    this.props.signIn(this.state);
  } 
```
<br>
So the logIn function in action will work but the component does not know the result of the transaction.

### 4- Access Reducer and Store From SignIn Component

We have defined a variable called `authError` so that we can know the result of the process. We were keeping this variable in store. Then let's access the store in the signIn component and edit the page according to the authError variable.
```
//Function to access store
const mapStateToProps=(state)=>{
  return {
    authError:state.auth.authError
  }
}
export default connect(mapStateToProps,mapDispatchToProps)(SignIn)
```
<br>
With the help of such a function, we have received the authError data from the store on the props of the page.
```
render() {
    const {authError}=this.props;
  return (
```
<br>
We can now give a warning if we are making an incorrect entry at the bottom of the page.
```
{/* for submit */}
          <div className="input-field">
            <button className="btn green lighten-1 z-depth-0">Login</button>
            <div className="red-text center">
            {/* If there is data in authError, it means that it has received an error. */}
              {authError ? <p>{authError}</p>: null}
            </div>
          </div>
```
<br>
We can control the operations as we finished coding.

### 5- Control of Login Processes
![react2.gif](https://cdn.steemitimages.com/DQmbYnDPmjwnfJjbnFHQWb5Y1fErF5oU4Wjm2zBiTZJXUw5/react2.gif)
<br>
If you enter the wrong user name or password on the login screen, the `authError` value will appear on the screen.
![react4.png](https://cdn.steemitimages.com/DQmXancfNoFjDfHw4WDFn2c4e2VUYQqNh5uFtRcF6YUodBG/react4.png)
<br>
We will not include user information in the firebase value in the store that we wrote to the console.

If you type the user name and password correctly, the user information will be filled in the firebase value.
![react3.gif](https://cdn.steemitimages.com/DQmdgofGTshiUKj6xBGbMM52dTGR7Z1uWcuaDa1PqVMU4Gf/react3.gif)
<br>
and
![react5.png](https://cdn.steemitimages.com/DQmbWZQkN72sPN6oGGXufUEFmGUq7dexqqX2UA1t5AP7PNR/react5.png)
<br>
# Curriculum

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-1

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-2

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-3

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-4

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-5

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-6

https://steemit.com/utopian-io/@pckurdu/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-7

# Proof of Work Done

https://github.com/pckurdu/Build-A-Blog-Site-With-React-Redux-Firebase-And-MaterializeCSS-Part-8-
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 152 others
properties (23)
post_id72,377,983
authorpckurdu
permlinkbuild-a-blog-site-with-react-redux-firebase-and-materializecss-part-8
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","react","redux","firebase"],"image":["https:\/\/cdn.steemitimages.com\/DQmRpmFiQxCwe4jswNSCADhUgLnyH8Ce249cszhzrYXLGvC\/react-redux.fw.png","https:\/\/cdn.steemitimages.com\/DQmeB5KpSezxEZbVkk9sYhqJa36GsrDqd2MMVhrmkT8j3NW\/react1.png","https:\/\/cdn.steemitimages.com\/DQmcga18bspMstXDegGHAjjfNrujrHUsCPifyJVZM45DEGk\/react2.png","https:\/\/cdn.steemitimages.com\/DQmRqc4HLmpJTvafy2grM4SBmEiPNdMSX4K3Za1DHReXJ8J\/react1.gif","https:\/\/cdn.steemitimages.com\/DQmV56ZMd8eeXoVagvMxw9d7iVHFcebtf1fyDws9UKo1HFk\/react3.png","https:\/\/cdn.steemitimages.com\/DQmbYnDPmjwnfJjbnFHQWb5Y1fErF5oU4Wjm2zBiTZJXUw5\/react2.gif","https:\/\/cdn.steemitimages.com\/DQmXancfNoFjDfHw4WDFn2c4e2VUYQqNh5uFtRcF6YUodBG\/react4.png","https:\/\/cdn.steemitimages.com\/DQmdgofGTshiUKj6xBGbMM52dTGR7Z1uWcuaDa1PqVMU4Gf\/react3.gif","https:\/\/cdn.steemitimages.com\/DQmbWZQkN72sPN6oGGXufUEFmGUq7dexqqX2UA1t5AP7PNR\/react5.png"],"links":["https:\/\/github.com\/facebook\/react","https:\/\/github.com\/mui-org\/material-ui","https:\/\/github.com\/pckurdu","https:\/\/github.com\/pckurdu\/Build-A-Blog-Site-With-React-Redux-Firebase-And-MaterializeCSS-Part-8-","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-1","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-2","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-3","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-4","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-5","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-6","https:\/\/steemit.com\/utopian-io\/@pckurdu\/build-a-blog-site-with-react-redux-firebase-and-materializecss-part-7"],"app":"steemit\/0.1","format":"markdown"}
created2019-04-01 13:01:18
last_update2019-04-01 13:01:18
depth0
children5
net_rshares49,126,574,373,328
last_payout2019-04-08 13:01:18
cashout_time1969-12-31 23:59:59
total_payout_value27.704 SBD
curator_payout_value8.779 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length12,216
author_reputation23,382,389,405,576
root_title"Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (216)
@portugalcoin ·
$12.09
Thank you for your contribution @pckurdu.

- Your tutorials are getting better and better. Excellent work in structuring the tutorial and explaining the code, with several GIFs with the demonstration of their results throughout the contribution.

- The section of your curriculum begins to get big in the contribution, could put for example:
<b>Curriculum</b>: <a href="#">Part1</a>, <a href="#">Part2</a>...

Thank you for your work in developing this tutorial.
Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/2-1-1-1-1-3-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
👍  , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id72,398,233
authorportugalcoin
permlinkre-pckurdu-build-a-blog-site-with-react-redux-firebase-and-materializecss-part-8-20190401t212031255z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["pckurdu"],"links":["#","https:\/\/join.utopian.io\/guidelines","https:\/\/review.utopian.io\/result\/8\/2-1-1-1-1-3-1-3-","https:\/\/discord.gg\/uTyJkNm","https:\/\/join.utopian.io\/"],"app":"steemit\/0.1"}
created2019-04-01 21:20:30
last_update2019-04-01 21:20:30
depth1
children1
net_rshares16,472,160,166,956
last_payout2019-04-08 21:20:30
cashout_time1969-12-31 23:59:59
total_payout_value9.174 SBD
curator_payout_value2.917 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length958
author_reputation214,343,891,436,406
root_title"Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (20)
@utopian-io ·
Thank you for your review, @portugalcoin! Keep up the good work!
properties (22)
post_id72,532,338
authorutopian-io
permlinkre-re-pckurdu-build-a-blog-site-with-react-redux-firebase-and-materializecss-part-8-20190401t212031255z-20190404t132034z
categoryutopian-io
json_metadata{"app":"beem\/0.20.17"}
created2019-04-04 13:20:36
last_update2019-04-04 13:20:36
depth2
children0
net_rshares0
last_payout2019-04-11 13:20:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length64
author_reputation152,913,012,544,965
root_title"Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@steem-ua ·
#### Hi @pckurdu!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
post_id72,403,377
authorsteem-ua
permlinkre-build-a-blog-site-with-react-redux-firebase-and-materializecss-part-8-20190401t234443z
categoryutopian-io
json_metadata{"app":"beem\/0.20.19"}
created2019-04-01 23:44:45
last_update2019-04-01 23:44:45
depth1
children0
net_rshares0
last_payout2019-04-08 23:44:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length286
author_reputation23,203,609,903,979
root_title"Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@steemitboard ·
Congratulations @pckurdu! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@pckurdu/voted.png?201904012310</td><td>You received more than 3000 upvotes. Your next target is to reach 4000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@pckurdu) and compare to others on the [Steem Ranking](http://steemitboard.com/ranking/index.php?name=pckurdu)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
post_id72,403,558
authorsteemitboard
permlinksteemitboard-notify-pckurdu-20190401t234759000z
categoryutopian-io
json_metadata{"image":["https:\/\/steemitboard.com\/img\/notify.png"]}
created2019-04-01 23:48:00
last_update2019-04-01 23:48:00
depth1
children0
net_rshares0
last_payout2019-04-08 23:48:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length828
author_reputation38,705,954,145,809
root_title"Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@utopian-io ·
Hey, @pckurdu!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
post_id72,408,142
authorutopian-io
permlinkre-build-a-blog-site-with-react-redux-firebase-and-materializecss-part-8-20190402t014609z
categoryutopian-io
json_metadata{"app":"beem\/0.20.17"}
created2019-04-02 01:46:09
last_update2019-04-02 01:46:09
depth1
children0
net_rshares0
last_payout2019-04-09 01:46:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length589
author_reputation152,913,012,544,965
root_title"Build A Blog Site With React, Redux Firebase And MaterializeCSS (Part 8)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000