@aequum/nestjs-authn
v0.27.41
Published
aequum NestJS authentication module
Readme
aequum NestJS authentication module
The module is designed to provide easy authentication services for the application, it uses JWT for authentication via passport-jwt strategy and provides user registration, login and password hashing for user credential.
Use
Install the module
To use the authentication module, you must install the package
@aequum/nestjs-authn:
pnpm install @aequum/nestjs-authnCreate the user defined components
To implement the authentication module, you must implement some classes in order to provide the authentication functionality for the app.
We provide a boilerplate with ready to use files, in the following list you can find the files that you must implement in your application.
User defined components are:
- User model
- User service
- Authentication service
- Passport strategy (JWT)
- Authentication controller
- Authentication NestJS module
Add the guard in the application
To use the authorization module, you must add the JWTGuard in your
main application module.
See the exapmle in our boilerplate
Configure
By default the services reads configuration from ConfigService with
the prefix authentication. it means root property authentication in
the configuration object, implementing the AuthnConfiguration
interface, this can be configured in AuthnService by the static property configPrefix.
See the configuration in our boilerplate, to get an example.
Components
Controllers
AuthController: Abstract class to implmement and handle login and token refresh.
Decorators
Can be used in the whole controller class or in a endpoint method.
@Public(): Marks controller or method as public, meaning it does not require authentication.@RequireAuthentication(): Marks controller or method as requiring authentication, meaning it requires a valid JWT token.
DTOs
class-validator models to
validate the inputs/outputs.
Exceptions
Via @aequum/exceptions package,
Guards
Interfaces
AuthCompliantUsersService: Interface for user service compliant with authentication module.AuthRefreshTokenCompliantUsersService: Extends fromAuthCompliantUsersServiceand adds methods for refresh token management.AuthnConfiguration: Interface for authentication module configuration.
Models
JwtPayload:class-validatormodel representing the payload of a JWT token, used byJwtStrategyto validate the tokens.
Passport
passportworkspacestrategiesworkspaceJwtStrategy: Abstract class to implmement JWT strategy in app and add it to the container.
Services
AuthnService: Abstract authentication service class to implmement in the application and add it to conatiner.
Types
ServiceUserFieldsMap: A map of fields from the user model to use in the authentication services.
