npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

twinz-authenticator

v1.1.8

Published

twinz-authenticator is nestjs module to easily integrate authentication service

Downloads

26

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 type
type 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 .