Intro
Get Started
Tech Stack
Features
Authentication
Password Validation
Persisted Authentication
Protected Routed
Page not Found
This project is based off of Ben Awads JWT authentication tutorial.
Code: https://github.com/benawad/jwt-auth-example
My goal with this project was have a fully functioning authentication app that I can clone in the future to jump start new projects. Ben Awad's tutorial helped me learn how to assign and manage JWT tokens as well as gave me an introduction to graphql. I also implemented a nicer UI so I can start with an out of the box, good looking login and sign up page. Admittedly, I may have take some inspiration from Facebook's UI...
Feel free to use this project, provide me with suggestions or make a PR to improve this project.
- Clone the project
- cd into the root directory and run
yarn install
- Make sure you have postgres installed on your local machine
- Create a .env file in the server directory and add the following environment variables
ACCESS_TOKEN_SECRET="your access token secret"
REFRESH_TOKEN_SECRET="your refresh token secret"
DB_USERNAME="username of owner of database"
DB_PASSWORD="password of owner of database"
DB_NAME="name of database"
The JWTs are managed in a way that tries to minimize the risk of XSS attacks. Instead of storing access tokens in local storage or cookies, they are stored in memory. Since the token is stored inside a memory variable, any malicious code injected into the website will not be able to make dangerous requests to the server on behalf of the authenticated user. However, this method makes it slightly more difficult to persist authentication across multiple sessions as access tokens are lost whenever the user leaves the page. To persist authentication, we store the refresh token in an httpOnly cookie. Every time the user first lands enters the React app, we grab a new access token and store it in a variable inside a useEffect.
- Typescript
- React
- JWT
- Typeorm
- Apollo
- Graphql
- Postgres
- Automatic token refresh on start up if previously logged in
- Access token stored in memory to prevent against malicious attacks
- Password validator class for password validation
- Multiple levels of validation
- No restrictions
- Length requirement
- Contains lowercase letters
- Contains uppercase letters
- Contains digits
- Contains special characters
- Password confirmation to ensure no mistyped passwords
- Sign Up demo: