Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nest-social-auth

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nest-social-auth

The social authentication library for your NestJS Applications

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

NestJS Social login

A mult-disk mult-driver social authentication manager for NestJS.

Table of Content

Introduction

This library provides functionality for validating social authentication credentials for Facebook, Google, and LinkedIn. It can be used in the backend of your application to ensure that the credentials provided by the user are valid and can be used to authenticate with the respective social media platforms.


Installation

#Using NPM
npm i nest-social-auth

#Using YARN
yarn i nest-social-auth

Getting Started

To register OauthModule with your app, import the module inside AppModule.

Static Registration

OauthModule is added to global scope by default.

import { Module } from '@nestjs/common';
import { OauthModule } from 'nest-social-auth'

@Module({
  imports: [
    OauthModule.register({
      isGlobal: true,
      default: 'facebook',
      clients: {
        facebook: {
            clientId:process.env.FACEBOOK_APP_ID,
            clientSecret:process.env.FACEBOOK_APP_SECRET
        },
        google: {
          clientId:process.env.GOOGLE_CLIENT_ID,
          clientSecret:process.env.GOOGLE_CLIENT_SECRET
      },
      linkedin: {
          clientId:process.env.LINKEDIN_CLIENT_ID,
          clientSecret:process.env.LINKEDIN_CLIENT_SECRET
      }
    })
  ],
  controllers: [],
  providers: [],
})
export class AppModule { }

Use ConfigModule provided by NestJS to load configurations. To learn about ConfigModule, click here.

#1. Create filesystem.ts file

import { registerAs } from "@nestjs/config";
import { OauthOptions } from "libs/oauth/src/interfaces";

export default registerAs(
  "oauth",
  () =>
    ({
      isGlobal: true,
      default: "facebook",
      clients: {
        facebook: {
          clientId: process.env.FACEBOOK_APP_ID,
          clientSecret: process.env.FACEBOOK_APP_SECRET,
        },
        google: {
          clientId: process.env.GOOGLE_CLIENT_ID,
          clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        },
        linkedin: {
          clientId: process.env.LINKEDIN_CLIENT_ID,
          clientSecret: process.env.LINKEDIN_CLIENT_SECRET,
        },
      },
    } as OauthOptions)
);

#2. Register ConfigModule

import { Module } from "@nestjs/common";
import filesystem from "@config/fileystem";
import { ConfigModule } from "@nestjs/config";

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      expandVariables: true,
      load: [filesystem],
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

#3. Register Async StorageModule Add following snippet to the imports array. ConfigService is importable from @nestjs/config module.

OauthModule.registerAsync({
  isGlobal: true,
  imports: [ConfigModule],
  useFactory: (config: ConfigService) => config.get("oauth"),
  inject: [ConfigService],
});

Driver Configuration

The best part about this package is the simplicity that it provides while working across different social login platforms. Every driver follow a simple and consistent API.

Currently the package supports login authentication for facebook,google and linkedin.

Driver Name: Facebook

Configuration:

{
    clientId:process.env.FACEBOOK_APP_ID,
    clientSecret:process.env.FACEBOOK_APP_SECRET
    }

facebook driver expects two parameters to authenticate a token. You can get the FACEBOOK_APP_ID, FACEBOOK_APP_SECRET by creating a developer account at facebook and enabling auth 2.0. Learn more about it here.

Driver Name: Google

Configuration:

{
    clientId:process.env.GOOGLE_APP_ID,
    clientSecret:process.env.GOOGLE_APP_SECRET
    }

google driver expects two parameters to authenticate a token. You can get the GOOGLE_APP_ID, GOOGLE_APP_SECRET by creating a developer account at google and enabling auth 2.0. Learn more about it here.

Driver Name: Linkedin

Configuration:

{
    clientId:process.env.LINKEDIN_APP_ID,
    clientSecret:process.env.LINKEDIN_APP_SECRET
    }

linkedin driver expects two parameters to authenticate a token. You can get the LINKEDIN_APP_ID, LINKEDIN_APP_SECRET by creating a developer account at linkedin and enabling auth 2.0. Learn more about it here.

To serve the file objects from your project, have a look at serve-static module by NestJS.


m

Usage

This package provides a single and uniform API for any type of operation across different drivers. You just have to Call the function Oauthorizer and pass the name of social platform and use thr methods provided.

Oauthorizer('google').getProfile({id_token:''})
Oauthorizer('facebook').getProfile({accessToken:''})
Oauthorizer('linkedin').getProfile({code:''})


Methods
  • getProfile(payload: CredentialsPayload): Get user profile from token.

  • getAccessToken(payload: ProfileParams): Get the access token for linkedin.

License

This library is licensed under the MIT License.

Keywords

FAQs

Package last updated on 05 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc