How To Set Up User Authentication Using React, Redux, and Redux Saga
How To Set Up User Authentication Using React, Redux, and Redux Saga
• React
• Redux
• Redux-Saga
• React Router
This post assumes that you already know react and basic concepts
of Redux and Redux-Saga.
Getting started
Clone my previous blog repository. CD into its root folder and run npm
Now that our backend is up and running, it’s time to get into its client
side implementation.
create-react-app react-login
This will create a new project with the name react-login . Go ahead
and cd into that folder. Open your package.json file in your
favourite editor and add following dependencies:
We don’t need any additional properties in this package.json file. We
can simply remove them, but I’ll leave it as is and move forward so that
we get to interesting part in this blog.
npm install
Index file
To start with, open the index.js file and place the code below into
this file.
Store configuration
Create a new folder called store inside the src folder. Inside that
new folder, create a file called configureStore.js , and paste following
code into that file.
Now that is out of our way, go ahead and create the src/reducers
folder, create index.js file and paste the code below in this file.
This file is responsible for importing the rest of the reducers inside the
reducers folder, combining them, and export them so that they are
available to be used in configureStore.js . We will make changes to
this file when we add new reducers later in this blog.
Routing file
Time for the routes file. Go ahead and create the src/routes folder
and inside this folder create an index.js file. Now open it and paste in
the below code.
The main goal of this file is to handle routing in our project. The file
imports React , Route and IndexRoute . After that, we need a
container, in this case I am importing container/App , which we are
going to write soon. Next is RegisterPage , which is a component, and
we will write that as well.
In the parent Route , when the home path matches then we simply
render our App container. On IndexRoute users will see
RegisterPage which will be rendered inside the App container.
Container
Now it’s time for the container. Go ahead and make a new folder called
container . Inside this folder create a new file called App.js and
place the below code into this file.
Registration
Now it is time for registerPage . Create a new folder src/components
For now, this is a very simple component. We will edit this later to add a
registration form and put some functionality into it.
Output
After creating all the folders and files above, run npm start in your
project, and open http://localhost:3000 in your browser. You should
be able to see the below result.
Clicking on login here will not redirect to the login route which we will
fix next.
Making it work
Routing
For routing to work, first make a new component inside the
components folder. Name it loginPage.js and place the below code
inside this component.
Now open the routes.js file, which we already created above, and
make following changes.
Now refresh your browser and you should be able to see loginPage
first. When you click on the “Register here” link, registerPage should
be rendered.
Now we have the basic routes working.
There seems to be a lot of code in this file now, but it’s all simple. First
we are importing connect to connect our store with the
registerPage component. Then we import registerUserAction
with their data as parameters. We are going to write actions in the next
step.
Actions
Now it’s time to add actions. Go ahead and create the src/actions
folder. Create the index.js file and place the below code in it.
This code exports some constants that we will be using throughout our
project.
Sagas
Now we are at the stage where we can introduce our sagas in our
project. If you are new to Redux-Saga then I suggest you tread this
blog before proceeding.
If you already know about sagas then go ahead and create a src/sagas
folder. Create the index.js file, and place the below code into this
file.
Now that we have our sagas it is time to import them in our store. Open
store/configureStore.js and update its contents with the below
contents.
1 import { createStore, applyMiddleware } from 'redux';
2 import createSagaMiddleware from 'redux-saga';
3
4 import rootReducer from '../reducers';
5 import rootSaga from '../sagas';
6
7 const configureStore = () => {
8 const sagaMiddleware = createSagaMiddleware();
9
10 return {
Now it’s time to create the src/services folder and create a new first
service. Name it authenticationService.js and place the below code
into this service.
This file does a basic ajax request using fetch API with some parameters
and header. It is a pretty self-explanatory service.
Reducer
Now that we are making a request to the server, it is time to receive that
response in our component. To do this we need a reducer. Go ahead
and create a reducers/registerReducer.js file and place the below
code into it.
It is a simple reducer function that gets state and returns new state. It
checks for REGISTER_USER_SUCCESS and REGISTER_USER_ERROR actions,
and returns the new state to the component.
Now we are done with the registration process. It is time to refresh your
browser, go to the register route, and enter some data. If you enter an
existing email then you should see the below result.
If you enter a new email then you should be redirected to loginPage ,
which we are going to implement next.
Login
It is time for us to login the user after they are registered. Go ahead and
open components/loginPage.js file and update it with the following
contents.
Now go ahead and open the sagas/watchers.js file and update its
contents with the following.
We do not have loginSaga yet. For that reason go ahead and open the
sagas/authenticationSaga.js saga and update its contents with the
following.
Here I am importing an additional service — loginUserService , which
I will be implementing next — and then exporting the new generator
function named loginSaga , which does pretty much the same thing as
registerSaga .
Now open the reducers/index.js file and update its contents with the
code below.
After this, refresh your browser and enter an email that is not registered
yet. After pressing the login button you should see the below result.
Dashboard page
Now create the components/dashboardPage.js component and place
the below code into this component.
This is a very simple component — all it does is return the Dashboard
text.
Now open the routes/index.js route and update its contents with the
following.
Now when you refresh page in your browser, enter a registered email,
and press enter, you should see the below results.
So there it is, this is a complete login system using React, Redux and
Redux-Saga. If you would like to see the whole project then clone this
repository.