Skip to main content

All In One

Implement various social identity provider authentication in your NestJS application for e.g. Google, Facebook, Twitter and many more.


npm install @nestjs-hybrid-auth/all --save


yarn add @nestjs-hybrid-auth/all

How To Use?#

This package is an assembling of all the individual nestjs hybrid auth provider packages and exports one single dynamic nestjs module. The module takes configuration for all social providers both synchronously and asynchronously using forRoot and forRootAsync static methods.

Sync Module Configuration#

If you have all of your social providers credentials and configuration handy in an object, just pass them in .forRoot static method to register the module. The options object is type of HybridAuthModuleOptions.

import { HybridAuthModule } from '@nestjs-hybrid-auth/all';
@Module({  imports: [    HybridAuthModule.forRoot({      google: {        clientID: process.env.GOOGLE_CLIENT_ID,        clientSecret: process.env.GOOGLE_CLIENT_SECRET,        callbackURL: process.env.GOOGLE_CALLBACK_URL,      },      facebook: {        clientID: process.env.FACEBOOK_CLIENT_ID,        clientSecret: process.env.FACEBOOK_CLIENT_SECRET,        callbackURL: process.env.FACEBOOK_CALLBACK_URL,      },      // Rest of the providers    }),  ],  controllers: [AppController],  providers: [AppService],})export class AppModule {}

Async Module Configuration#

If you get the configurations for each identity provider asynchronously through any config service or so, use forRootAsync static method to load them. The options would be same but value for each provider would be their AsyncOptions equivalent. Please refer to below code:

Example useFactory#

It is same as this config on Facebook page.

import { Module } from '@nestjs/common';import { ConfigModule, ConfigService } from '@nestjs/config';import {  HybridAuthModule,  FacebookAuthModuleOptions,} from '@nestjs-hybrid-auth/all';
@Module({  imports: [    ConfigModule.forRoot({      isGlobal: true,      cache: true,      expandVariables: true,    }),    HybridAuthModule.forRootAsync({      facebook: {        useFactory: (          configService: ConfigService        ): FacebookAuthModuleOptions => ({          clientID: configService.get('FACEBOOK_CLIENT_ID'),          clientSecret: configService.get('FACEBOOK_CLIENT_SECRET'),          callbackURL: configService.get('FACEBOOK_CALLBACK_URL'),        }),        inject: [ConfigService],      },      // Rest of the providers    }),  ],})export class AllAsyncFactoryModule {}

Example useClass#

It is same as this config on Facebook page.

import { Module, Injectable } from '@nestjs/common';import { ConfigModule, ConfigService } from '@nestjs/config';import {  HybridAuthModule,  FacebookAuthModuleOptions,  FacebookAuthModuleOptionsFactory,} from '@nestjs-hybrid-auth/all';
// We create a config provider class for useClass@Injectable()export class FacebookAuthConfig implements FacebookAuthModuleOptionsFactory {  constructor(private configService: ConfigService) {}
  createModuleOptions(): FacebookAuthModuleOptions {    return {      clientID: this.configService.get('FACEBOOK_CLIENT_ID'),      clientSecret: this.configService.get('FACEBOOK_CLIENT_SECRET'),      callbackURL: this.configService.get('FACEBOOK_CALLBACK_URL'),    };  }}
// Actual app module@Module({  imports: [    ConfigModule.forRoot({      isGlobal: true,      cache: true,      expandVariables: true,    }),    HybridAuthModule.forRootAsync({      facebook: {        useClass: FacebookAuthConfig,      },      // Rest of the providers    }),  ],})export class AllAsyncClassModule {}

Controller Configuration#

Once you have setup the module properly in module file, its time to configure your route handlers to make the user properly redirected to appropriate identity provider's login page. @nestjs-hybrid-auth/all exports route handler guards for all identity providers it supports from @nestjs-hybrid-auth/<identity-provider> where identity-provider can be facebook/google/linkedin/twitter etc.

Each route will have two variants. One is to redirect to social login page and the other is to collect the response such as access/refresh tokens and user profile etc. The result will be attached to Request object's hybridAuthResult property as shown in the example below.

This controller examples shows facebook login using hybrid auth.#

import { UseFacebookAuth, FacebookAuthResult } from '@nestjs-hybrid-auth/all';
@Controller()export class AppController {  constructor(private readonly appService: AppService) {}
  @UseFacebookAuth()  @Get('auth/facebook')  loginWithFacebook() {    return 'Login with Facebook';  }
  @UseFacebookAuth()  @Get('auth/facebook-login/callback')  facebookCallback(@Request() req): Partial<FacebookAuthResult> {    const result: FacebookAuthResult = req.hybridAuthResult;    return {      accessToken: result.accessToken,      refreshToken: result.refreshToken,      profile: result.profile,    };  }}

Exports For Module File#

@nestjs-hybrid-auth/all exports various interfaces and methods for module registration.


This is the dynamic module which must be imported in your app's main module with forRoot or forRootAsync static methods whichever suits your need. Both will return a NestJS dynamic module.

interface HybridAuthModule {  forRoot(options: HybridAuthModuleOptions): DynamicModule;  forRootAsync(options: HybridAuthModuleAsyncOptions): DynamicModule;}


If you are configuring your module with forRoot static method, pass in the module options given below. The options object is just a container for various identity providers strategy options.

interface HybridAuthModuleOptions {  google: GoogleAuthModuleOptions;  facebook: FacebookAuthModuleOptions;  // List goes on for other providers}


If you want to provide the async configurations for each identity provider, use the following HybridAuthModuleAsyncOptions object.

The value of each provider is kind of this

interface HybridAuthModuleAsyncOptions {  google: GoogleAuthModuleAsyncOptions;  facebook: FacebookAuthModuleAsyncOptions;  // List goes on for other providers}

Exports For Controller File#

@nestjs-hybrid-auth/all just re-exports all the authentication route guards and result interfaces from all the individual identity providers. For more information, you can check the documentation for any provider for e.g.@nestjs-hybrid-auth/google.

Have Issues?#

If you still have trouble setting up the workflow properly, please file an issue at Issues page.


Manish Jangir