Skip to content

menaaziz27/Authentication-Authorization-NestJS

Repository files navigation

Nest Logo

Description

Authentication system built with Passportjs.

Requirements

  • Nodejs
  • MongoDB Local or MongoDB Atlas

Installation

npm install

How to run the app

  1. Clone the repo locally (should have git installed locally)

$ git clone <https://github.com/menaaziz27/auth-task.git>

  1. edit your mongodb connection string src/app.module.ts line 10.

  2. run any of the following ..

development

$ npm run start

watch mode

$ npm run start:dev

production mode

$ npm run start:prod

My Thniking & Design Process

  • As mentioned in the task, Clean code was expected since that I had to use NestJS Framework to make the code readable and more safety.

  • I started to split my services to its own and abstracted the logic from the controllers

  • Firstly, I focused to make the job done and lastly I started to refactor and re-structure.

  • Since the task was a RESTful API, I used passportjs with jwt strategy to authenticate the user.


Authentication System Workflow




What I learned

  • I learnt alot abot NestJS since I used to use ExpressJS.

  • I realized that I could write softwares from only a documentation.

  • I've learnt how to create custom roles and custom auth guards in NestJS.

  • How to add and manage roles and expect specific values rom the user.


The Api endpoints

/auth/register => signup

/auth/login          => login

/auth/refresh-tokens => refresh tokens

/auth/logout         => log user out (clear cookies)

/                    => protected route

/public              => public route

/admin               => require authentication and admin role only

/users               => require authentication and user role only

Notes

  • There's no test included however I've included validation and useful errors as a response.
  • I know I should use environment variables but I've consumed a little bit of my time to use them in NestJs so I left the DB strings and jwt secrets available since it's a simple task and not gonna be in the production.

Useful Links

article

NestJS Docs

About

NestJS Authentication system using passportjs jwt strategy and refresh token.& Authorization with role based access control(RBAC).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published