twinz-authenticator
v1.1.8
Published
twinz-authenticator is nestjs module to easily integrate authentication service
Downloads
19
Maintainers
Readme
twinz-authenticator
twinz-authenticator is a nestJs module , it is useful when you want to check the authenticated users , it is easy to integrate with almost nestJs apps, it is not a database dependent so you can use it in combination with Relational Databases or No-Sql Databases , all you have to do is to follow the guide and with few lines of code you will be able to implement your own Authentication Service.
Install
npm install --save twinz-authenticator
npm install --save @nestjs/passport passport passport-local
npm install --save @nestjs/jwt passport-jwt
npm install --save-dev @types/passport-local
npm install --save-dev @types/passport-jwt
Usage
After Installing the package you need to import the module AuthenticatorModule in your app.module.ts as shown bellow :
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthenticatorModule } from 'twinz-authenticator/src';
@Module({
imports: [
AuthenticatorModule.forRoot({defaultValidationClass:AppService,secret:'YourSecret',
signOptions:{expiresIn:'3000s'}}
)],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
the AuthenticatorModule return a customizable module based on the configuration object passed into the forRoot() method :
the defaultValidationClass : the class that implements the getUser() method that returns the user from the database and the signPayload() that receives the signed token from the module.
the secret : the secret used to create the token .
the signOptions : the object that contain the expiration time of the token .
Guide
the following exemple is a complete guide on how to use the twinz-authenticator :
app.module.ts :
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthenticatorModule } from 'twinz-authenticator/src';
@Module({
imports: [
AuthenticatorModule.forRoot({defaultValidationClass:AppService,secret:'YourSecret',
signOptions:{expiresIn:'3000s'}}
)],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
app.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { SignPayloadInterface, UserModelInterface, UserValidationInterface } from 'twinz-authenticator/src/interfaces/localstrategy-Interfaces';
import { SignPayloadService } from 'twinz-authenticator/src//sign-payload/sign-payload.service';
import { LoginPayload } from 'twinz-authenticator/src//types/types';
@Injectable()
export class AppService implements UserValidationInterface, SignPayloadInterface {
constructor(@Inject(SignPayloadService) private _signPayloadService: SignPayloadInterface) {
}
async getUser(username: string, passport: string): Promise<UserModelInterface> {
let users = [{id:1, Username: 'testUser', Password: 'TestPassword', Role: 'client' }]
let user = users.find((user: UserModelInterface) => { return user.Username == username })
if (user) {
return await Promise.resolve(user);
}
return null
}
signPayload(user: UserModelInterface): LoginPayload & { Username: string } {
return {
...this._signPayloadService.signPayload(user),
Username: user.Username
};
}
}
app.controller.ts :
import { Controller, Get, Post, Req, SetMetadata, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { AuthenticatorGuard } from 'twinz-authenticator/src/authenticator.guard';
import { AuthorizationGuard } from 'twinz-authenticator/src/authorizer.guard';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) { }
@UseGuards(AuthenticatorGuard)
@Post()
async userLogin(@Req() req) {
return this.appService.signPayload(req.user);
}
@SetMetadata('roles', ['client'])
@UseGuards(AuthorizationGuard)
@Get()
gethello(@Req() req) {
console.log('logged User', req.user)
return 'hello'
}
}
The login Object should be an object of typetype Login = {username:string;password:string}
finally for each endpoint you can use the AuthorizationGuard to authorize users with specific role by setting
the roles metadata that contains the list of authorized user roles to use the api .