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

nest-social-auth

v0.1.1

Published

The social authentication library for your NestJS Applications

Downloads

11

Readme

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 { }

Recommended Way

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.