9951 explained code solutions for 126 technologies


reactjsHow do I implement JWT authentication with ReactJS?


In order to implement JWT authentication with ReactJS, you need to install the necessary libraries and set up the authentication flow.

  1. Install the necessary libraries:

    • jsonwebtoken: for generating and verifying JWT tokens
    • axios: for making HTTP requests
  2. Set up the authentication flow:

    • Create a login component to accept user credentials and make a POST request to the authentication API.
    • On successful authentication, the API will return a JWT token.
    • Store the token in the browser's local storage.
    • For subsequent requests, attach the token in the Authorization header.

Example code

//Login component
import React from 'react';
import axios from 'axios';

const Login = () => {
  const handleLogin = async (e) => {
    e.preventDefault();
    const { email, password } = e.target.elements;
    const response = await axios.post('/auth', {
      email: email.value,
      password: password.value
    });
    const { token } = response.data;
    localStorage.setItem('token', token);
  };
  return (
    <form onSubmit={handleLogin}>
      <input type="text" name="email" />
      <input type="password" name="password" />
      <button type="submit">Login</button>
    </form>
  );
};
export default Login;
  1. For subsequent requests, attach the token in the Authorization header:
const token = localStorage.getItem('token');
const response = await axios.get('/protected-route', {
  headers: {
    Authorization: `Bearer ${token}`
  }
});

Helpful links

Edit this code on GitHub