@edirect/auth
The EDirectInsure Authentication module.
Installation
$ npm i --save @edirect/auth
Usage
Import and register AuthModule on AppModule (app.module.ts):
imports: [
AuthModule.register({
jwksUrl: "https://url.to.jkws.com",
tokenUrl: "https://url.to.token.com"
}),
OR
AuthModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
jwksUrl: configService.get<string>(Variables.AUTH_SERVICE_JWKS),
tokenUrl: configService.get<string>(Variables.AUTH_SERVICE_TOKEN),
}),
inject: [ConfigService],
}),
...
]
Use the exported decorators to secure paths:
// Adds Bearer Token validation and, optionally, User Roles and Permissions validation.
// Loads the user information into the context, if valid.
@Auth({ roles: [RolesEnum.ROLE_ADMIN], permissions: [PermissionsEnum.ENTITY_CREATE] })
// Adds only Roles or Permissions validation. Use after @Token() decorator.
// For example, use the @Token() decorator at the controller level and the @Roles() or @Permission()
// decorator at the operatopn level.
@Roles([RolesEnum.ROLE_ADMIN])
@Permissions([PermissionsEnum.ENTITY_CREATE])
// Adds only Bearer Token validation. Loads the user information into the context, if valid.
@Token()
You can access the user information after authentication using the @Usr() decorator:
@Get('/:id')
@Token()
async findOne(@Param('sid') sid: string, @Usr() userInfo: UserInfo): Promise<GetEventReportDto> {}