JWT Authentication with React & Redux

Wiring up the Login Form

First, let's add a login() function to agent.js that'll actually fire off the HTTP request.

Add login function to agent.js
// ...

const requests = {
  get: url =>
  post: (url, body) =>`${API_ROOT}${url}`, body).then(responseBody)

const Articles = {
  // ...

const Auth = {
  login: (email, password) =>'/users/login', { user: { email, password } })

export default {

So now, the login component needs to call agent.Auth.login() with the username and password the user specifies. The Login component and auth reducer now need to be able to get the user's input and call the login() function. First, in the 'Login' component, we need to pull in all the state from the 'auth' reducer in mapStateToProps() and a mapDispatchToProps() function that's going to dispatch separate events:

Update the login component
import { Link } from 'react-router';
import ListErrors from './ListErrors';
import React from 'react';
import agent from '../agent';
import { connect } from 'react-redux';

const mapStateToProps = state => ({ ...state.auth });

const mapDispatchToProps = dispatch => ({
  onChangeEmail: value =>
    dispatch({ type: 'UPDATE_FIELD_AUTH', key: 'email', value }),
  onChangePassword: value =>
    dispatch({ type: 'UPDATE_FIELD_AUTH', key: 'password', value }),
  onSubmit: (email, password) =>
    dispatch({ type: 'LOGIN', payload: agent.Auth.login(email, password) })

class Login extends React.Component {
  constructor() {
    this.changeEmail = ev => this.props.onChangeEmail(;
    this.changePassword = ev => this.props.onChangePassword(;
    this.submitForm = (email, password) => ev => {
      this.props.onSubmit(email, password);

  render() {
    const email =;
    const password = this.props.password;
    return (
      <div className="auth-page">
        <div className="container page">
          <div className="row">

            <div className="col-md-6 offset-md-3 col-xs-12">
              <h1 className="text-xs-center">Sign In</h1>
              <p className="text-xs-center">
                <Link to="register">
                  Need an account?

              <ListErrors errors={this.props.errors} />

              <form onSubmit={this.submitForm(email, password)}>

                  <fieldset className="form-group">
                      className="form-control form-control-lg"
                      onChange={this.changeEmail} />

                  <fieldset className="form-group">
                      className="form-control form-control-lg"
                      onChange={this.changePassword} />

                    className="btn btn-lg btn-primary pull-xs-right"
                    Sign in



export default connect(mapStateToProps, mapDispatchToProps)(Login);

There's also a ListErrors component that's going to display any errors that occurred during login, like if the user entered the wrong password. Let's implement this component:

Create the ListErrors component
import React from 'react';

class ListErrors extends React.Component {
  render() {
    const errors = this.props.errors;
    if (errors) {
      return (
        <ul className="error-messages">
            Object.keys(errors).map(key => {
              return (
                <li key={key}>
                  {key} {errors[key]}
    } else {
      return null;

export default ListErrors;

The login component is going to dispatch 2 different actions: an 'UPDATE_FIELD_AUTH' action that's going to fire when the user changes an input field, and a 'LOGIN' action that's going to fire when the user submits the login form.

The input fields need to take the current value of email and password, and call the correct functions when they change. Also, if the state says there's an auth request in progress, we'll disable the submit button.