@mest-fe/nestjs-sso v0.2.0
NestJS SSO
Common authentication SDK for internal services (NestJS project).
Installation
- Install package:
yarn add @mest-fe/nestjs-sso
- Add
SSOModule.forRoot()
to your module imports.
Usage
Authentication
Add a decorator to any controller class or method, and the user will be authenticated before the method is called.
// controller
@MestAuth()
@Controller('/users')
export class UsersController {
}
// method
@MestAuth()
@Post('/login')
async login() {
return 'ok'
}
Scope
Specify roles for controller classes or methods:
// Require authenticated and admin role
@MestRoleAuth(MestRoles.ADMIN)
@Delete('/users')
async removeUsers() {
return 'ok'
}
Get current user
On any controller method, the user of the current session can be injected:
@Get('/users/self')
@MestAuth()
async getSelf(@MestUser() user: MestUserType){
return user
}
Errors
When a user without a session request an api that requires authentication, a 401
error will be returned in http;
If the user simply has a role that does not match, a 403
error will be thrown.
The server will not handle these errors and you should direct the user on the client side to the sso.mest.sh to log in.
9 months ago
9 months ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago