Giter Club home page Giter Club logo

react-jwt-auth's Introduction

React JWT Authentication (without Redux) example

In this tutorial, we’re gonna build a React JWT Authentication example with LocalStorage, React Router, Axios and Bootstrap (without Redux). I will show you:

  • JWT Authentication Flow for User Signup & User Login
  • Project Structure for React JWT Authentication (without Redux) with LocalStorage, React Router & Axios
  • Creating React Components with Form Validation
  • React Components for accessing protected Resources (Authorization)
  • Dynamic Navigation Bar in React App

User Registration and User Login Flow

For JWT Authentication, we’re gonna call 2 endpoints:

  • POST api/auth/signup for User Registration
  • POST api/auth/signin for User Login

The following flow shows you an overview of Requests and Responses that React Client will make or receive. This React Client must add a JWT to HTTP Header before sending request to protected resources.

react-jwt-authentication-flow

Demo Video

This is full React + Node.js Express JWT Authentication & Authorization demo (with form validation, check signup username/email duplicates, test authorization with 3 roles: Admin, Moderator, User):

react-express-authentication-jwt-example-feature-image

Or React with Spring Boot Server:

Spring Boot Reactjs JWT Authentication example

React Component Diagram with Router, Axios & LocalStorage

react-jwt-authentication-project-overview

For more detail, please visit:

React (without Redux) JWT Authentication & Authorization example

React JWT Authentication & Authorization example with HttpOnly Cookie

React - How to Logout when Token is expired

React Redux JWT Authentication & Authorization example

React Hooks: JWT Authentication & Authorization example

React + Redux + Hooks: JWT Authentication & Authorization example

This project was bootstrapped with Create React App.

Set port

.env

PORT=8081

Note:

Open src/services/auth-header.js and modify return statement for appropriate back-end (found in the tutorial).

export default function authHeader() {
  const user = JSON.parse(localStorage.getItem('user'));

  if (user && user.accessToken) {
    // return { Authorization: 'Bearer ' + user.accessToken }; // for Spring Boot back-end
    return { 'x-access-token': user.accessToken };             // for Node.js Express back-end
  } else {
    return {};
  }
}

Project setup

In the project directory, you can run:

npm install
# or
yarn install

or

Compiles and hot-reloads for development

npm start
# or
yarn start

Open http://localhost:8081 to view it in the browser.

The page will reload if you make edits.

Related Posts

In-depth Introduction to JWT-JSON Web Token

React.js CRUD example to consume Web API

React Pagination example

React File Upload with Axios and Progress Bar to Rest API

Fullstack (JWT Authentication & Authorization example):

React + Spring Boot

React + Node.js Express

CRUD with Node.js Express:

React.js + Node.js Express + MySQL

React.js + Node.js Express + PostgreSQL

React.js + Node.js Express + MongoDB

CRUD with Spring Boot:

React.js + Spring Boot + MySQL

React.js + Spring Boot + PostgreSQL

React.js + Spring Boot + MongoDB

CRUD with Django:

React.js + Django Rest Framework

Serverless:

React Firebase CRUD App with Realtime Database

React Firestore CRUD App example | Firebase Cloud Firestore

Integration (run back-end & front-end on same server/port)

Integrate React with Spring Boot

Integrate React with Node.js Express

react-jwt-auth's People

Contributors

bezkoder avatar dima056359 avatar tienbku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-jwt-auth's Issues

Tables are not created + no communication between front-end & back-end

Hello, Congrats on making these apps available for the general public.
I downloaded both front-end and backend (Postgresql) but I have the following issues despite following instructions:

Setup for front and back:

  • changed autheader to" return { 'x-access-token': user.accessToken };"

  • bodyParcer is deprected, changed to app.use(express.json()); and app.use(express.urlencoded({extended: true}));

  • register form will not respond on Submit unless I changed he zero in (this.checkBtn.context._errors.length === 0) to 1. Browser link changed but no success message and form still showing.

  • in index.js, changed the FALSE in "operatorsAliases: 0" to ZERO, or removed completely.

  • last;y, added <React.StrictMode> to index.js in front-end, new error below from dev tools:

Warning: Legacy context API has been detected within a strict-mode tree.

The old API will be supported in all 16.x releases, but applications using it should migrate to the new version.

Please update the following components: Button(s), Control(s), Form(t), s

Learn more about this warning here: https://fb.me/react-legacy-context
in Form(t) (at register.component.js:134)
in div (at register.component.js:127)
in div (at register.component.js:126)
in Register (created by Context.Consumer)
in Route (at App.js:135)
in Switch (at App.js:132)
in div (at App.js:131)
in div (at App.js:64)
in App (at src/index.js:11)
in Router (created by BrowserRouter)
in BrowserRouter (at src/index.js:10)
in StrictMode (at src/index.js:9)

IMPORTANT:

The back-end does not create the database and table (db: MY-DATABASE-POSTGRES-NAME, with password)
The front-end shows up fine in the browser but does not communicate with my postgres database. On submit, the register from fields will clear, and ONLY the browser link changed with new data from fields, ONLY when I changed the ZERO in (this.checkBtn.context._errors.length === 0) to 1. which is not a solution.

Is the legacy API warning the root of the problem or should Sequalize create the database and tables regardless?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.