Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proof-of-concept app that demonstrates, Rails API only backend, React.js frontend and refresh token authentication.

Notifications You must be signed in to change notification settings

aarona/jwt-refresh-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT Refresh Token Demo Application

A full-stack demonstration of JWT authentication with automatic token refresh, built with Rails (backend) and React (frontend). This project showcases enterprise-level authentication patterns and modern development practices.

Table of Contents

Purpose

This project was inspired by my first React.js project that leveraged GraphQL and an Apollo library for automatic authentication renewal. Wanting to expand my Full-stack skills, I decided to write my own simple token renewal library but I wanted it to be framework agnostic (apollo-link-token-refresh could only be used with Apollo GQL libraries) and dead simple so it could be used with any framework or even vanilla Javascript.

This project is a proof-of-concept that ties a simple Authentication service written in Rails with a React.js frontend using simple-jwt-refresh as a way for the client to authenticate and manage sessions.

Architecture Overview

flowchart LR
  Frontend["<b>React.js Frontend</b><br/><br/>• Authentication Context API<br/>• Login/Registration<br/>• Protected Routing"]
  Backend["<b>Authentication Service</b><br/><br/>• Refresh/Access tokens<br/>• Session Management<br/>• CORS Config"]
  JWTLib["<b>simple-jwt-refresh</b><br/><br/>• Auto Refresh<br/>• Secure Token Storage<br/>• Error Handling"]

  Backend -->|JSON Response| Frontend
  JWTLib <--> Backend
  Frontend <--> JWTLib

  style Frontend fill:#33a,color:#fff,text-align:left
  style Backend fill:#a33,color:#fff,text-align:left
  style JWTLib fill:#cc6,color:#000,text-align:left
Loading

Components

Quick Start

Setup Instructions (Docker Deployment)

Clone the repository

git clone https://github.com/aarona/jwt-refresh-app
cd jwt-refresh-app

# Edit .env with your configuration
cp .env.example .env

Get Rails master key

# Move to the Authentication Service folder
# And build a new master.key and credentials file from scratch
cd auth-service
rm config/credentials.yml.enc
bundle exec rails credentials:edit

# Copy the master key from auth-service/config/master.key
# You may need to generate one. See the auth-service documentation
# for more details.
cat config/master.key

# Add this value to your .env file as RAILS_MASTER_KEY

Start the entire stack (with automatic database seeding)

From the project root directory:

docker compose up --build

Clean up Docker resources

# Stop and remove containers and networks for this project
docker compose down

# Stop and remove containers, networks, and volumes for this project
docker compose down -v

# Remove project-specific images and rebuild from scratch
docker compose down --rmi all

If you're having gem installation issues, you can force a rebuild with no cache:

docker compose build --no-cache

Docker Services

Expect the images to take about 2GB of disk space:

Service Description Port Approximate Size
jwt-refresh-app-auth-service Rails API server 3000 1.2GB
jwt-refresh-app-frontend React development server 3001 600MB+

Docker Volumes

Volume Name Purpose
jwt-refresh-app_auth-service-gems Persistent storage for gems
jwt-refresh-app_frontend-node-modules Persistent storage for node_modules

Setup Instructions (Local Deployment/Development)

Authentication Service local setup instructions

Front end Application local setup instructions

Access the applications

Demo credentials (automatically created)

Name Email Password
Demo User [email protected] password123
John Doe [email protected] password123
Jane Smith [email protected] password123

Authentication Flow

Register:

(Optional) Create a new user. Check the "Keep me signed in" box to test longer refresh token lifetimes.

Registration Page

Login:

User submits credentials. Check the "Keep me signed in" box to test longer refresh token lifetimes.

Login Page

Logging Panel:

For convenience, I built a logging panel that uses React Context API so you can watch authentication, routing and other logs easily without needing to open a developer console. The main purpose for this panel is to observe that the refresh token library I built automatically updates and manages access tokens with out the user needing to do anything.

Activity Logs

Auto Refresh:

On token expiry, library uses the refresh token to get new access tokens. You can observe this with the built-in logging panel mentioned above.

Refresh Token Logs

Sessions Management:

You can test session revocation by logging in with the same user using different browsers or even different devices on the network. To test this, revoke another device's session and the refresh the page from that device. You will then be logged out on that device as expected.

Sessions that were created with the "Keep me signed in" flag on with be marked as "extended".

You can test that your session is restored automatically by reloading the page.

Session Management

Logout:

Clears tokens and invalidates server session

About

Proof-of-concept app that demonstrates, Rails API only backend, React.js frontend and refresh token authentication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published