Skip to content

niallconaghan/nx-auth-nestjs-angular

Repository files navigation

NxAuthNestjsAngular

This example is to demonstrate JWT authentication and refresh token authentcaiton and should not be used in production as is.

Mono repo

Mono repo workspace using Nrwl Nx workspace.

Authention API

Made using Nestjs.

Client

Made using Angular 10

Database

MonogoDB hosted on mongodb.com

How to run

Clone the project

git clone https://github.com/niallconaghan/nx-auth-nestjs-angular.git

Install packages, from the root directory, run:

npm install

To run the client, from the root directory, run:

nx serve client

To run the API, run:

nx serve api

API

The api will serve on port 4500 by default.

Setting environment

The api gettings environment variables from a .env file.

This file needs to be created in:

apps/api/

.env settings

To change what port the api is served on:

PORT=<port number>

Add your monodb.com url:

MONGO_DB_URL=<mongodb url>

Add your JWT secret:

JWT_SECRET=<your secret>

Add your password salt:

HASH_SALT=<salt number>

Example .env

PORT=1337
MONGO_DB_URL=mongodb+srv://<username>:<password>@database.5555.mongodb.net/users?retryWrites=true&w=majority
JWT_SECRET=s0m3s3cret
HASH_SALT=10

Client

The client will run on port 4200 by default.

Register a user with a username and password

Login as an authenticated user

Request protected data

After one minute your JWT token will expire

Upon requesting protected data the client will make a request to get a new JWT token using the refresh_token received after logging in.

After 2 minutes of no action the refresh_token will expire.

Any attempts to fetch protected data will log the user out.

Route guards

Route guards are in place to block access to the content page if you are not authenticated and block access to the login page if you are authenticated

Examples

Register a user

register

Login with that registered user

login

Request protected data

request-successful

Expired JWT, request new token via refresh_token

refresh-token-success

Authenticated again after refresh

success-after-refresh

Expired refresh token, logging out

refresh-expired

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published