This is an authorization module for NestJS withTypeORM using MySQL. The module includes:
- Roles controller
- Pagination DTO
- DB errors handler
@GetUser
decorator for use only in protected routes, returns all user properties- Basic CRUD for users
- Two methods for user creation:
- One for registration (without roles property)
- One for user creation by an admin (with roles options), only allowed with admin token
- User update method with the possibility of updating user roles, but only with admin token
- ConfigModule, docker-compose(MySQL server) file and
.env.template
with very basic options, needs to be replaced
To install this authorization module with roles, you can choose from two options:
Option A (Very fast):
-
Copy the entire project file and configure it for your purposes.
-
Rename and configure the
.env.template
file. -
Run the command:
$ npm install
-
Import AuthModule to your app.module.ts and any other relevant modules.
-
Enjoy! :)
Option B (Fast):
-
Install NestJS and configure TypeORM module for MySQL connection.
-
Copy
src/auth
andsrc/common
folders to your project. -
Run the following commands to install dependencies:
$ npm i bcrypt @nestjs/passport passport @nestjs/jwt passport-jwt $ npm i -D @types/bcrypt @types/passport-jwt
-
Import AuthModule to your app.module.ts and any other relevant modules.
-
Enjoy! :)
The available roles can be found in src/auth/enums/valid-roles.enum.ts
.
// Route available for all registered users
// Option 1:
@Patch('update/:id')
@Auth(ValidRoles.user)
updateUser(
@Param('id', ParseIntPipe) id: number,
@GetUser() user: User,
@Body() updateUserDto: UpdateUserDto
) { ... }
// Option 2: Without passing roles it's equal to passing ValidRoles.user.
@Patch('update/:id')
@Auth()
updateUser(
@Param('id', ParseIntPipe) id: number,
@GetUser() user: User,
@Body() updateUserDto: UpdateUserDto
) { ... }
// Both examples demonstrate the use of the `@GetUser` decorator.
I hope this helps! Let me know if you have any questions.