Tutorial built with React 16.13

Other versions available:

In this tutorial we'll cover how to implement user registration and login functionality with React and Redux. The tutorial example is a React + Redux Boilerplate application that uses JWT authentication, it's based on the code from a real world secure web application I developed for a law firm in Sydney recently.

For an extended version that includes email verification, role based authorization and forgot password functionality see React Boilerplate - Email Sign Up with Verification, Authentication & Forgot Password.

The tutorial project is available on GitHub at https://github.com/cornflourblue/react-redux-registration-login-example.

Here it is in action: (See on StackBlitz at https://stackblitz.com/edit/react-redux-registration-login-example)

Update History:

31 Aug 2020 - Updated to React 16.13.1 as well as other package dependencies to fix npm audit warnings

as well as other package dependencies to fix npm audit warnings 20 Nov 2019 - Updated to React 16.12.0 and posted tutorial on how to deploy the React app to AWS with a Node.js backend

and posted tutorial on how to deploy the React app to AWS with a Node.js backend 19 Jul 2019 - Updated components to map action creators to props instead of calling dispatch directly

11 Jan 2019 - Updated tutorial to React 16.7.0 and Webpack 4.28

and 26 Jun 2018 - Created real API backend with ASP.NET Core 2.1 at ASP.NET Core 2.1 - Simple API for Authentication, Registration and User Management

14 Jun 2018 - Created real backend API with NodeJS and MongoDB at NodeJS + MongoDB - Simple API for Authentication, Registration and User Management

08 Jun 2018 - Updated tutorial to React 16.4.0 and added auto-logout on "401 Unauthorized" response from the server

and added auto-logout on "401 Unauthorized" response from the server 27 Sep 2017 - Updated tutorial to React 16.0.0 and Webpack 3.6

and 16 Sep 2017 - Built tutorial with React 15.4.2 and Webpack 2.2 - The code for this version of the react tutorial is available on GitHub at https://github.com/cornflourblue/react-redux-registration-login-example/releases/tag/v15.4.2



Running the React + Redux Tutorial Example Locally

Install NodeJS and NPM from https://nodejs.org/en/download/. Download or clone the project source code from https://github.com/cornflourblue/react-redux-registration-login-example Install all required npm packages by running npm install from the command line in the project root folder (where the package.json is located). Start the application by running npm start from the command line in the project root folder, this will launch a browser displaying the application.

For more info on setting up a React development environment see React - Setup Development Environment.



Running the Tutorial Example with a Real Backend API

The boilerplate code uses a fake / mock backend that uses browser local storage for managing application data, to switch to a real backend api you just have to remove a couple of lines of code from the main react entry file ( /src/index.jsx ).

You can build your own backend api or start with one of the below options:



Deploying the React App to AWS

This video shows how to setup a production ready web server from scratch on AWS, then deploy the example React + Redux app and configure it to run with a real Node.js + MongoDB backend api. The tutorial used in the video is available at React + Node.js on AWS - How to Deploy a MERN Stack App to Amazon EC2.



Deploying the React App to Microsoft Azure

For instructions on how to deploy the React app to Azure with a real backend api built with ASP.NET Core and SQL Server see React + ASP.NET Core on Azure with SQL Server - How to Deploy a Full Stack App to Microsoft Azure.



React + Redux Tutorial Project Structure

All source code for the React + Redux tutorial app is located in the /src folder. Inside the src folder there is a folder per feature (App, HomePage, LoginPage, RegisterPage) and a bunch of folders for non-feature code that can be shared across different parts of the app (_actions, _components, _constants, _helpers, _reducers, _services).

I prefixed non-feature folders with an underscore "_" to group them together and make it easy to distinguish between features and non-features, it also keeps the project folder structure shallow so it's quick to see everything at a glance from the top level and to navigate around the project.

The index.js files in each folder are barrel files that group all the exported modules together so they can be imported using the folder path instead of the full module path and to enable importing multiple modules in a single import (e.g. import { userActions, alertActions } from '../_actions' ).

Click any of the below links to jump down to a description of each file along with it's code:

Redux Actions Folder

Path: /src/_actions

The _actions folder contains all the Redux action creators for the project, I've organised the action creators into different files by action type (e.g. user actions, alert actions etc).

If you're not familiar with Redux actions or action creators you can learn about them at https://redux.js.org/basics/actions.

Redux Alert Action Creators

Path: /src/_actions/alert.actions.js

Contains Redux action creators for actions related to alerts / toaster notifications in the application. For example to display a success alert message with the text 'Registration Successful' you can call dispatch(alertActions.success('Registration successful')); .

I've wrapped the action methods in an alertActions object at the top of the file so it's easy to see all available actions at a glance and simplifies importing them into other files. The implementation details for each action creator are placed in the below functions.

import { alertConstants } from '../_constants'; export const alertActions = { success, error, clear }; function success(message) { return { type: alertConstants.SUCCESS, message }; } function error(message) { return { type: alertConstants.ERROR, message }; } function clear() { return { type: alertConstants.CLEAR }; }

Redux User Action Creators

Path: /src/_actions/user.actions.js

Contains Redux action creators for actions related to users. Public action creators are exposed via the userActions object at the top of the file and function implementations are located below, I like this structure because you can quickly see all of the actions that are available.

Most of the actions for users are async actions that are made up of multiple sub actions, this is because they have to make an http request and wait for the response before completing. Async actions typically dispatch a request action before performing an async task (e.g. an http request), and then dispatch a success or error action based on the result of the async task.

For example the login() action creator performs the following steps:

dispatches a LOGIN_REQUEST action with dispatch(request({ username })); calls the async task userService.login(username, password) dispatches a LOGIN_SUCCESS with dispatch(success(user)); if login was successful, or dispatches a LOGIN_FAILURE action with dispatch(failure(error)); if login failed

To keep the code tidy I've put sub action creators into nested functions within each async action creator function. For example the login() function contains 3 nested action creator functions for request() , success() and failure() that return the actions LOGIN_REQUEST , LOGIN_SUCCESS and LOGIN_FAILURE respectively. Putting the sub action creators into nested functions also allows me to give them standard names like request, success and failure without clashing with other function names because they only exist within the scope of the parent function.

import { userConstants } from '../_constants'; import { userService } from '../_services'; import { alertActions } from './'; import { history } from '../_helpers'; export const userActions = { login, logout, register, getAll, delete: _delete }; function login(username, password) { return dispatch => { dispatch(request({ username })); userService.login(username, password) .then( user => { dispatch(success(user)); history.push('/'); }, error => { dispatch(failure(error.toString())); dispatch(alertActions.error(error.toString())); } ); }; function request(user) { return { type: userConstants.LOGIN_REQUEST, user } } function success(user) { return { type: userConstants.LOGIN_SUCCESS, user } } function failure(error) { return { type: userConstants.LOGIN_FAILURE, error } } } function logout() { userService.logout(); return { type: userConstants.LOGOUT }; } function register(user) { return dispatch => { dispatch(request(user)); userService.register(user) .then( user => { dispatch(success()); history.push('/login'); dispatch(alertActions.success('Registration successful')); }, error => { dispatch(failure(error.toString())); dispatch(alertActions.error(error.toString())); } ); }; function request(user) { return { type: userConstants.REGISTER_REQUEST, user } } function success(user) { return { type: userConstants.REGISTER_SUCCESS, user } } function failure(error) { return { type: userConstants.REGISTER_FAILURE, error } } } function getAll() { return dispatch => { dispatch(request()); userService.getAll() .then( users => dispatch(success(users)), error => dispatch(failure(error.toString())) ); }; function request() { return { type: userConstants.GETALL_REQUEST } } function success(users) { return { type: userConstants.GETALL_SUCCESS, users } } function failure(error) { return { type: userConstants.GETALL_FAILURE, error } } } // prefixed function name with underscore because delete is a reserved word in javascript function _delete(id) { return dispatch => { dispatch(request(id)); userService.delete(id) .then( user => dispatch(success(id)), error => dispatch(failure(id, error.toString())) ); }; function request(id) { return { type: userConstants.DELETE_REQUEST, id } } function success(id) { return { type: userConstants.DELETE_SUCCESS, id } } function failure(id, error) { return { type: userConstants.DELETE_FAILURE, id, error } } }

React Components Folder

Path: /src/_components

The _components folder contains shared React components that can be used anywhere in the application.

React Private Route Component

Path: /src/_components/PrivateRoute.jsx

The react private route component renders a route component if the user is logged in, otherwise it redirects the user to the /login page.

The way it checks if the user is logged in is by checking that there is a user object in local storage. While it's possible to bypass this check by manually adding an object to local storage using browser dev tools, this would only give access to the client side component, it wouldn't give access to any real secure data from the server api because a valid authentication token (JWT) is required for this.

import React from 'react'; import { Route, Redirect } from 'react-router-dom'; export const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={props => ( localStorage.getItem('user') ? <Component {...props} /> : <Redirect to={{ pathname: '/login', state: { from: props.location } }} /> )} /> )

Redux Action Constants Folder

Path: /src/_constants

The _constants folder contains all of the redux action type constants used by redux action creators and reducers. It could be used for any other constants in the project as well, it doesn't have to be only for redux action types.

I decided to put redux action constants into their own files (rather than the same files as redux actions) to simplify my redux action files and keep them focused on one thing.

Redux Alert Action Constants

Path: /src/_constants/alert.constants.js

The alert constants object contains the redux alert action types used to display and clear alerts in the react application.

export const alertConstants = { SUCCESS: 'ALERT_SUCCESS', ERROR: 'ALERT_ERROR', CLEAR: 'ALERT_CLEAR' };

Redux User Action Constants

Path: /src/_constants/user.constants.js

The user constants object contains the redux user action types that can be dispatched in the react application, async actions that perform http requests involve a request followed by a success or error response, so each of these three steps is represented by a redux action.

export const userConstants = { REGISTER_REQUEST: 'USERS_REGISTER_REQUEST', REGISTER_SUCCESS: 'USERS_REGISTER_SUCCESS', REGISTER_FAILURE: 'USERS_REGISTER_FAILURE', LOGIN_REQUEST: 'USERS_LOGIN_REQUEST', LOGIN_SUCCESS: 'USERS_LOGIN_SUCCESS', LOGIN_FAILURE: 'USERS_LOGIN_FAILURE', LOGOUT: 'USERS_LOGOUT', GETALL_REQUEST: 'USERS_GETALL_REQUEST', GETALL_SUCCESS: 'USERS_GETALL_SUCCESS', GETALL_FAILURE: 'USERS_GETALL_FAILURE', DELETE_REQUEST: 'USERS_DELETE_REQUEST', DELETE_SUCCESS: 'USERS_DELETE_SUCCESS', DELETE_FAILURE: 'USERS_DELETE_FAILURE' };

React + Redux Helpers Folder

Path: /src/_helpers

The helpers folder contains all the bits and pieces that don't fit into other folders but don't justify having a folder of their own.

React Auth Header

Path: /src/_helpers/auth-header.js

Auth header is a helper function that returns an HTTP Authorization header containing the Json Web Token (JWT) of the currently logged in user from local storage. If the user isn't logged in an empty object is returned.

The auth header is used to make authenticated HTTP requests to the server api using JWT authentication.

export function authHeader() { // return authorization header with jwt token let user = JSON.parse(localStorage.getItem('user')); if (user && user.token) { return { 'Authorization': 'Bearer ' + user.token }; } else { return {}; } }

React Fake / Mock Backend

Path: /src/_helpers/fake-backend.js

The fake backend is used for running the tutorial example without a server api (backend-less). It monkey patches the fetch() function to intercept certain api requests and mimic the behaviour of a real api by managing data in browser local storage. Any requests that aren't intercepted get passed through to the real fetch() function.

// array in local storage for registered users let users = JSON.parse(localStorage.getItem('users')) || []; export function configureFakeBackend() { let realFetch = window.fetch; window.fetch = function (url, opts) { return new Promise((resolve, reject) => { // wrap in timeout to simulate server api call setTimeout(() => { // authenticate if (url.endsWith('/users/authenticate') && opts.method === 'POST') { // get parameters from post request let params = JSON.parse(opts.body); // find if any user matches login credentials let filteredUsers = users.filter(user => { return user.username === params.username && user.password === params.password; }); if (filteredUsers.length) { // if login details are valid return user details and fake jwt token let user = filteredUsers[0]; let responseJson = { id: user.id, username: user.username, firstName: user.firstName, lastName: user.lastName, token: 'fake-jwt-token' }; resolve({ ok: true, text: () => Promise.resolve(JSON.stringify(responseJson)) }); } else { // else return error reject('Username or password is incorrect'); } return; } // get users if (url.endsWith('/users') && opts.method === 'GET') { // check for fake auth token in header and return users if valid, this security is implemented server side in a real application if (opts.headers && opts.headers.Authorization === 'Bearer fake-jwt-token') { resolve({ ok: true, text: () => Promise.resolve(JSON.stringify(users))}); } else { // return 401 not authorised if token is null or invalid reject('Unauthorised'); } return; } // get user by id if (url.match(/\/users\/\d+$/) && opts.method === 'GET') { // check for fake auth token in header and return user if valid, this security is implemented server side in a real application if (opts.headers && opts.headers.Authorization === 'Bearer fake-jwt-token') { // find user by id in users array let urlParts = url.split('/'); let id = parseInt(urlParts[urlParts.length - 1]); let matchedUsers = users.filter(user => { return user.id === id; }); let user = matchedUsers.length ? matchedUsers[0] : null; // respond 200 OK with user resolve({ ok: true, text: () => JSON.stringify(user)}); } else { // return 401 not authorised if token is null or invalid reject('Unauthorised'); } return; } // register user if (url.endsWith('/users/register') && opts.method === 'POST') { // get new user object from post body let newUser = JSON.parse(opts.body); // validation let duplicateUser = users.filter(user => { return user.username === newUser.username; }).length; if (duplicateUser) { reject('Username "' + newUser.username + '" is already taken'); return; } // save new user newUser.id = users.length ? Math.max(...users.map(user => user.id)) + 1 : 1; users.push(newUser); localStorage.setItem('users', JSON.stringify(users)); // respond 200 OK resolve({ ok: true, text: () => Promise.resolve() }); return; } // delete user if (url.match(/\/users\/\d+$/) && opts.method === 'DELETE') { // check for fake auth token in header and return user if valid, this security is implemented server side in a real application if (opts.headers && opts.headers.Authorization === 'Bearer fake-jwt-token') { // find user by id in users array let urlParts = url.split('/'); let id = parseInt(urlParts[urlParts.length - 1]); for (let i = 0; i < users.length; i++) { let user = users[i]; if (user.id === id) { // delete user users.splice(i, 1); localStorage.setItem('users', JSON.stringify(users)); break; } } // respond 200 OK resolve({ ok: true, text: () => Promise.resolve() }); } else { // return 401 not authorised if token is null or invalid reject('Unauthorised'); } return; } // pass through any requests not handled above realFetch(url, opts).then(response => resolve(response)); }, 500); }); } }

React Router History

Path: /src/_helpers/history.js

The history is a custom history object used by the React Router, the reason I used a custom history object instead of the built into React Router is to enable redirecting users from outside React components, for example from the user actions after successful login or registration.

import { createBrowserHistory } from 'history'; export const history = createBrowserHistory();

Redux Store

Path: /src/_helpers/store.js

The redux store helper calls createStore() to create the centralized redux state store for the entire react application.

import { createStore, applyMiddleware } from 'redux'; import thunkMiddleware from 'redux-thunk'; import { createLogger } from 'redux-logger'; import rootReducer from '../_reducers'; const loggerMiddleware = createLogger(); export const store = createStore( rootReducer, applyMiddleware( thunkMiddleware, loggerMiddleware ) );

Redux Reducers Folder

Path: /src/_reducers

The _reducers folder contains all the Redux reducers for the project, each reducer updates a different part of the application state in response to dispatched redux actions.

If you're not familiar with Redux reducers you can learn about them at https://redux.js.org/basics/reducers.

Redux Alert Reducer

Path: /src/_reducers/alert.reducer.js

The redux alert reducer manages the application state for alerts / toaster notifications, it updates state when an alert action is dispatched from anywhere in the application, for example when an alertConstants.SUCCESS action is dispatched, the reducer updates the alert state to an object with type: 'alert-success' and message: action.message .

import { alertConstants } from '../_constants'; export function alert(state = {}, action) { switch (action.type) { case alertConstants.SUCCESS: return { type: 'alert-success', message: action.message }; case alertConstants.ERROR: return { type: 'alert-danger', message: action.message }; case alertConstants.CLEAR: return {}; default: return state } }

Redux Authentication Reducer

Path: /src/_reducers/authentication.reducer.js

The redux authentication reducer manages the state related to login (and logout) actions, on successful login the current user object and a loggedIn flag are stored in the authentication section of the application state. On logout or login failure the authentication state is set to an empty object, and during login (between login request and success/failure) the authentication state has a loggingIn flag set to true and a user object with the details of the user that is attempting to login.

import { userConstants } from '../_constants'; let user = JSON.parse(localStorage.getItem('user')); const initialState = user ? { loggedIn: true, user } : {}; export function authentication(state = initialState, action) { switch (action.type) { case userConstants.LOGIN_REQUEST: return { loggingIn: true, user: action.user }; case userConstants.LOGIN_SUCCESS: return { loggedIn: true, user: action.user }; case userConstants.LOGIN_FAILURE: return {}; case userConstants.LOGOUT: return {}; default: return state } }

Redux Registration Reducer

Path: /src/_reducers/registration.reducer.js

The redux registration reducer manages the registration section of the application state, as you can see there isn't much to it, on registration request it just sets a registering flag set to true which the RegisterPage uses to show the loading spinner. On register success or failure it clears the registration state.

import { userConstants } from '../_constants'; export function registration(state = {}, action) { switch (action.type) { case userConstants.REGISTER_REQUEST: return { registering: true }; case userConstants.REGISTER_SUCCESS: return {}; case userConstants.REGISTER_FAILURE: return {}; default: return state } }

Redux Users Reducer

Path: /src/_reducers/users.reducer.js

The redux users reducer manages the users section of the application state which is used by the HomePage to display a list of users and enable deleting of users.

import { userConstants } from '../_constants'; export function users(state = {}, action) { switch (action.type) { case userConstants.GETALL_REQUEST: return { loading: true }; case userConstants.GETALL_SUCCESS: return { items: action.users }; case userConstants.GETALL_FAILURE: return { error: action.error }; case userConstants.DELETE_REQUEST: // add 'deleting:true' property to user being deleted return { ...state, items: state.items.map(user => user.id === action.id ? { ...user, deleting: true } : user ) }; case userConstants.DELETE_SUCCESS: // remove deleted user from state return { items: state.items.filter(user => user.id !== action.id) }; case userConstants.DELETE_FAILURE: // remove 'deleting:true' property and add 'deleteError:[error]' property to user return { ...state, items: state.items.map(user => { if (user.id === action.id) { // make copy of user without 'deleting:true' property const { deleting, ...userCopy } = user; // return copy of user with 'deleteError:[error]' property return { ...userCopy, deleteError: action.error }; } return user; }) }; default: return state } }

React + Redux Services Folder

Path: /src/_services

The _services layer handles all http communication with backend apis for the application, each service encapsulates the api calls for a content type (e.g. users) and exposes methods for performing various operations (e.g. CRUD operations). Services can also have methods that don't wrap http calls, for example the userService.logout() method just removes an item from local storage.

I like wrapping http calls and implementation details in a services layer, it provides a clean separation of concerns and simplifies the redux actions (and other modules) that use the services.

React + Redux User Service

Path: /src/_services/user.service.js

The user service encapsulates all backend api calls for performing CRUD operations on user data, as well as logging and out of the example application. The service methods are exported via the userService object at the top of the file, and the implementation of each method is located in the function declarations below.

In the handleResponse method the service checks if the http response from the api is 401 Unauthorized and automatically logs the user out. This handles if the JWT token expires or is no longer valid for any reason.

import config from 'config'; import { authHeader } from '../_helpers'; export const userService = { login, logout, register, getAll, getById, update, delete: _delete }; function login(username, password) { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }; return fetch(`${config.apiUrl}/users/authenticate`, requestOptions) .then(handleResponse) .then(user => { // store user details and jwt token in local storage to keep user logged in between page refreshes localStorage.setItem('user', JSON.stringify(user)); return user; }); } function logout() { // remove user from local storage to log user out localStorage.removeItem('user'); } function getAll() { const requestOptions = { method: 'GET', headers: authHeader() }; return fetch(`${config.apiUrl}/users`, requestOptions).then(handleResponse); } function getById(id) { const requestOptions = { method: 'GET', headers: authHeader() }; return fetch(`${config.apiUrl}/users/${id}`, requestOptions).then(handleResponse); } function register(user) { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(user) }; return fetch(`${config.apiUrl}/users/register`, requestOptions).then(handleResponse); } function update(user) { const requestOptions = { method: 'PUT', headers: { ...authHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify(user) }; return fetch(`${config.apiUrl}/users/${user.id}`, requestOptions).then(handleResponse);; } // prefixed function name with underscore because delete is a reserved word in javascript function _delete(id) { const requestOptions = { method: 'DELETE', headers: authHeader() }; return fetch(`${config.apiUrl}/users/${id}`, requestOptions).then(handleResponse); } function handleResponse(response) { return response.text().then(text => { const data = text && JSON.parse(text); if (!response.ok) { if (response.status === 401) { // auto logout if 401 response returned from api logout(); location.reload(true); } const error = (data && data.message) || response.statusText; return Promise.reject(error); } return data; }); }

React + Redux App Folder

Path: /src/App

The app folder is for react components and other code that is used only by the app component in the tutorial application.

React + Redux App Component

Path: /src/App/App.jsx

The app component is the root component for the react tutorial application, it contains the outer html, routes and global alert notification for the example app.

If the url path doesn't match any route there is a default redirect defined below the routes that redirects the user to the home page.

import React from 'react'; import { Router, Route, Switch, Redirect } from 'react-router-dom'; import { connect } from 'react-redux'; import { history } from '../_helpers'; import { alertActions } from '../_actions'; import { PrivateRoute } from '../_components'; import { HomePage } from '../HomePage'; import { LoginPage } from '../LoginPage'; import { RegisterPage } from '../RegisterPage'; class App extends React.Component { constructor(props) { super(props); history.listen((location, action) => { // clear alert on location change this.props.clearAlerts(); }); } render() { const { alert } = this.props; return ( <div className="jumbotron"> <div className="container"> <div className="col-sm-8 col-sm-offset-2"> {alert.message && <div className={`alert ${alert.type}`}>{alert.message}</div> } <Router history={history}> <Switch> <PrivateRoute exact path="/" component={HomePage} /> <Route path="/login" component={LoginPage} /> <Route path="/register" component={RegisterPage} /> <Redirect from="*" to="/" /> </Switch> </Router> </div> </div> </div> ); } } function mapState(state) { const { alert } = state; return { alert }; } const actionCreators = { clearAlerts: alertActions.clear }; const connectedApp = connect(mapState, actionCreators)(App); export { connectedApp as App };

React + Redux Home Page Folder

Path: /src/HomePage

The home page folder is for react components and other code that is used only by the home page component in the tutorial application.

React + Redux Home Page Component

Path: /src/HomePage/HomePage.jsx

The home page component is displayed after signing in to the application, it shows the signed in user's name plus a list of all registered users in the tutorial application. The users are loaded into redux state by calling this.props.getUsers() from the componentDidMount() react lifecycle hook, which dispatches the redux action userActions.getAll() .

Users can also be deleted from the user list, when the delete link is clicked it calls the this.props.deleteUser(id) function which dispatches the redux action userActions.delete(id) .

import React from 'react'; import { Link } from 'react-router-dom'; import { connect } from 'react-redux'; import { userActions } from '../_actions'; class HomePage extends React.Component { componentDidMount() { this.props.getUsers(); } handleDeleteUser(id) { return (e) => this.props.deleteUser(id); } render() { const { user, users } = this.props; return ( <div className="col-md-6 col-md-offset-3"> <h1>Hi {user.firstName}!</h1> <p>You're logged in with React!!</p> <h3>All registered users:</h3> {users.loading && <em>Loading users...</em>} {users.error && <span className="text-danger">ERROR: {users.error}</span>} {users.items && <ul> {users.items.map((user, index) => <li key={user.id}> {user.firstName + ' ' + user.lastName} { user.deleting ? <em> - Deleting...</em> : user.deleteError ? <span className="text-danger"> - ERROR: {user.deleteError}</span> : <span> - <a onClick={this.handleDeleteUser(user.id)}>Delete</a></span> } </li> )} </ul> } <p> <Link to="/login">Logout</Link> </p> </div> ); } } function mapState(state) { const { users, authentication } = state; const { user } = authentication; return { user, users }; } const actionCreators = { getUsers: userActions.getAll, deleteUser: userActions.delete } const connectedHomePage = connect(mapState, actionCreators)(HomePage); export { connectedHomePage as HomePage };

React + Redux Login Page Folder

Path: /src/LoginPage

The login page folder is for react components and other code that is used only by the login page component in the tutorial application.

React + Redux Login Page Component

Path: /src/LoginPage/LoginPage.jsx

The login page component renders a login form with username and password fields. It displays validation messages for invalid fields when the user attempts to submit the form. If the form is valid, submitting it causes the this.props.login(username, password) to be called, which dispatches the redux action userActions.login(username, password) .

The constructor() function calls this.props.logout() which dispatches the userActions.logout() redux action to log the user out if they're logged in, this enables the login page to also be used as the logout page.

import React from 'react'; import { Link } from 'react-router-dom'; import { connect } from 'react-redux'; import { userActions } from '../_actions'; class LoginPage extends React.Component { constructor(props) { super(props); // reset login status this.props.logout(); this.state = { username: '', password: '', submitted: false }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(e) { const { name, value } = e.target; this.setState({ [name]: value }); } handleSubmit(e) { e.preventDefault(); this.setState({ submitted: true }); const { username, password } = this.state; if (username && password) { this.props.login(username, password); } } render() { const { loggingIn } = this.props; const { username, password, submitted } = this.state; return ( <div className="col-md-6 col-md-offset-3"> <h2>Login</h2> <form name="form" onSubmit={this.handleSubmit}> <div className={'form-group' + (submitted && !username ? ' has-error' : '')}> <label htmlFor="username">Username</label> <input type="text" className="form-control" name="username" value={username} onChange={this.handleChange} /> {submitted && !username && <div className="help-block">Username is required</div> } </div> <div className={'form-group' + (submitted && !password ? ' has-error' : '')}> <label htmlFor="password">Password</label> <input type="password" className="form-control" name="password" value={password} onChange={this.handleChange} /> {submitted && !password && <div className="help-block">Password is required</div> } </div> <div className="form-group"> <button className="btn btn-primary">Login</button> {loggingIn && <img src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" /> } <Link to="/register" className="btn btn-link">Register</Link> </div> </form> </div> ); } } function mapState(state) { const { loggingIn } = state.authentication; return { loggingIn }; } const actionCreators = { login: userActions.login, logout: userActions.logout }; const connectedLoginPage = connect(mapState, actionCreators)(LoginPage); export { connectedLoginPage as LoginPage };

React + Redux Register Page Folder

Path: /src/RegisterPage

The register page folder is for react components and other code that is used only by the register page component in the tutorial application.

React + Redux Register Page Component

Path: /src/RegisterPage/RegisterPage.jsx

The register page component renders a simple registration form with fields for first name, last name, username and password. It displays validation messages for invalid fields when the user attempts to submit the form. If the form is valid, submitting it calls this.props.register(user) which dispatches the redux action userActions.register(user) .

import React from 'react'; import { Link } from 'react-router-dom'; import { connect } from 'react-redux'; import { userActions } from '../_actions'; class RegisterPage extends React.Component { constructor(props) { super(props); this.state = { user: { firstName: '', lastName: '', username: '', password: '' }, submitted: false }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { const { name, value } = event.target; const { user } = this.state; this.setState({ user: { ...user, [name]: value } }); } handleSubmit(event) { event.preventDefault(); this.setState({ submitted: true }); const { user } = this.state; if (user.firstName && user.lastName && user.username && user.password) { this.props.register(user); } } render() { const { registering } = this.props; const { user, submitted } = this.state; return ( <div className="col-md-6 col-md-offset-3"> <h2>Register</h2> <form name="form" onSubmit={this.handleSubmit}> <div className={'form-group' + (submitted && !user.firstName ? ' has-error' : '')}> <label htmlFor="firstName">First Name</label> <input type="text" className="form-control" name="firstName" value={user.firstName} onChange={this.handleChange} /> {submitted && !user.firstName && <div className="help-block">First Name is required</div> } </div> <div className={'form-group' + (submitted && !user.lastName ? ' has-error' : '')}> <label htmlFor="lastName">Last Name</label> <input type="text" className="form-control" name="lastName" value={user.lastName} onChange={this.handleChange} /> {submitted && !user.lastName && <div className="help-block">Last Name is required</div> } </div> <div className={'form-group' + (submitted && !user.username ? ' has-error' : '')}> <label htmlFor="username">Username</label> <input type="text" className="form-control" name="username" value={user.username} onChange={this.handleChange} /> {submitted && !user.username && <div className="help-block">Username is required</div> } </div> <div className={'form-group' + (submitted && !user.password ? ' has-error' : '')}> <label htmlFor="password">Password</label> <input type="password" className="form-control" name="password" value={user.password} onChange={this.handleChange} /> {submitted && !user.password && <div className="help-block">Password is required</div> } </div> <div className="form-group"> <button className="btn btn-primary">Register</button> {registering && <img src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" /> } <Link to="/login" className="btn btn-link">Cancel</Link> </div> </form> </div> ); } } function mapState(state) { const { registering } = state.registration; return { registering }; } const actionCreators = { register: userActions.register } const connectedRegisterPage = connect(mapState, actionCreators)(RegisterPage); export { connectedRegisterPage as RegisterPage };

Base Index HTML File

Path: /src/index.html

The base index html file contains the outer html for the whole tutorial application. When the app is started with npm start , Webpack bundles up all of the react + redux code into a single javascript file and injects it into the body of the page.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>React + Redux - User Registration and Login Example & Tutorial</title> <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> <style> a { cursor: pointer; } .help-block { font-size: 12px; } </style> </head> <body> <div id="app"></div> </body> </html>

Main React Entry File

Path: /src/index.jsx

The root index.jsx file bootstraps the react + redux tutorial application by rendering the App component (wrapped in a redux Provider ) into the app div element defined in the base index html file above.

The boilerplate application uses a fake / mock backend that stores data in browser local storage, to switch to a real backend api simply remove the fake backend code below the comment // setup fake backend .

import React from 'react'; import { render } from 'react-dom'; import { Provider } from 'react-redux'; import { store } from './_helpers'; import { App } from './App'; // setup fake backend import { configureFakeBackend } from './_helpers'; configureFakeBackend(); render( <Provider store={store}> <App /> </Provider>, document.getElementById('app') );

React Tutorial Babel RC (Run Commands)

Path: /.babelrc

The babel config file defines the presets used by babel to transpile the React and ES6 code. The babel transpiler is run by webpack via the babel-loader module configured in the webpack.config.js file below.

{ "presets": [ "@babel/preset-react", "@babel/preset-env" ] }

React Tutorial Package.json

Path: /package.json

The package.json file contains project configuration information including package dependencies which get installed when you run npm install . Full documentation is available on the npm docs website.

{ "name": "react-redux-registration-login-example", "version": "1.0.0", "repository": { "type": "git", "url": "https://github.com/cornflourblue/react-redux-registration-login-example.git" }, "license": "MIT", "scripts": { "build": "webpack --mode production", "start": "webpack-dev-server --open" }, "dependencies": { "history": "^4.10.1", "react": "^16.12.0", "react-dom": "^16.12.0", "react-redux": "^7.1.3", "react-router-dom": "^5.1.2", "redux": "^4.0.4", "redux-logger": "^3.0.6", "redux-thunk": "^2.3.0" }, "devDependencies": { "@babel/core": "^7.4.3", "@babel/preset-env": "^7.4.3", "@babel/preset-react": "^7.0.0", "babel-loader": "^8.0.5", "html-webpack-plugin": "^3.2.0", "path": "^0.12.7", "webpack": "^4.29.6", "webpack-cli": "^3.3.0", "webpack-dev-server": "^3.2.1" } }

React Webpack Config

Path: /webpack.config.js

Webpack is used to compile and bundle all the project files so they're ready to be loaded into a browser, it does this with the help of loaders and plugins that are configured in the webpack.config.js file. For more info about webpack check out the webpack docs.

The webpack config file also defines a global config object for the application using the externals property, you can also use this to define different config variables for your development and production environments.