Best practice Authentication & Authorization implementation
- NestJs
- Prisma as ORM
- Zod for input validation
- JWT tokens(Access & Refresh tokens)
- Redis for auto-detection refresh token system
You need to install and run postgresql as database and redis for auto detection refresh token system.
if you are not installed and run them you can use docker-compose.yml
file which i provide it for you:
$ docker compose up -d
You can run separatly by below command too:
$ docker compose up -d redis $ docker compose up -d db
Run above command just if you do not have postgres & redis on your system.
$ git clone https://github.com/dinno7/auth_nestjs
$ cd auth_nestjs
$ npm install
# development watch mode
$ npm run start:dev
# development
$ npm run start
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
You can use @Auth(...AuthTypes)
decorator to define authentication system for special route's handler or controller,
The JWT Bearer token is active for all routes by default and you can deactive it by passing AuthTypes.None
to @Auth()
decorator:
// Some route handler in controller
@Auth(AuthTypes.None)
routeHandler(){
// Your codes...
}
Also you can append this @Auth
decorator to hole controller:
// Some controller
@Auth(AuthTypes.None)
@Controller('dinno')
export class DinnoController {
// ...
}
There is 3 type of Auth types:
AuthTypes.None;
AuthTypes.Bearer; // -> Appended to all routes by default
AuthTypes.ApiKey;
- ApiKey auth system
- Google OAuth2
- OTP login with email
- Add global exception filter
- Complete README.md
- Remove the forgot-pass .ejs file and replace it with simple template
Nest is MIT licensed.