@poapper/nest-auth
v0.0.1-alpha.4
Published
<p align="center"> <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a> </p>
Downloads
2
Readme
Description
Poapper SSO client library for Nest
Installation
$ npm i --save @poapper/nest-auth
Usage
Import AuthModule
and apply AuthMiddleware
:
@Module({
imports: [
AuthModule.register({
validateApiUrl: 'http://sso.poapper.com/validate',
}),
],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('*');
}
}
The AuthMiddleware parse and validate the Bearer token from request's Authorization Header, and add User's information in request object if the token is valid.
Async Config
@Module({
imports: [
AuthModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
validateApiUrl: configService.get('validateApiUrl'),
}),
inject: [ConfigService]
}),
],
})
Mock API for development
If useMocking
option is true and the application accept a request contains a token with the value'user-one-token', the AuthMiddleware
find User from mockUsers
by using the token value('user-one-token') as key and put the User in request object.
@Module({
imports: [
AuthModule.register({
useMocking: true,
mockUsers: {
['user-one-token'] : {
id: 1,
name: 'user-one-name',
email: '[email protected]',
}
}
}),
],
})
if you have a file that contains information of mockUsers, then place the file on the project root directory and use the mockUsersFilePath
option to load it.
app.module.ts
@Module({
imports: [
AuthModule.register({
useMocking: true,
mockUsersFilePath: 'mock-users.json',
}),
],
})
mock-users.json
{
"user-one-token": {
"id": 1,
"name": "user-one-name",
"email": "[email protected]"
}
}