NestJS Hybrid Auth
NestJS hybrid auth is a dynamic nestjs module or assembling of individual modules written over passport authentication library which enables you to integrate social login in your nestjs application for various identity providers such as Facebook, Google, Instagram and many more.
Please visit https://nestjs-hybrid-auth.manishjangir.com for complete documentation
Prerequisites
The library requires you to install few peer dependencies
npm install @nestjs/passport passport reflect-metadata --save
OR
yarn add @nestjs/passport passport reflect-metadata
Install Hybrid Auth
You can install the library for all providers or install it separately for each identity provider.
For All Providers
npm install @nestjs-hybrid-auth/all --save
yarn add @nestjs-hybrid-auth/all
For Individual Identity
npm install @nestjs-hybrid-auth/google --save
yarn add @nestjs-hybrid-auth/google
How To Use?
Every individual package or the global all-in-one hybrid auth package exports a dynamic nestjs module and nest Guard
(controller method decorator) which sets up the login and callback workflow.
Note: This is just a usage snippet. Please read the actual documentation of an identity provider for its peer dependencies.
In *.module.ts file
If you are using individual package
import { GoogleAuthModule } from '@nestjs-hybrid-auth/google';
@Module({
import: [
GoogleAuthModule.forRoot({
clientID: 'xxxxxxxxxxxx',
clientSecret: 'xxxxxxxxxx',
requestCallbackURL: 'xxxxxxxxx',
}),
],
})
class AppModule {}
Or if you are using all-in-one package
import { HybridAuthModule } from '@nestjs-hybrid-auth/all';
@Module({
import: [
HybridAuthModule.forRoot({
google: {
clientID: 'xxxxxxxxxxxx',
clientSecret: 'xxxxxxxxxx',
requestCallbackURL: 'xxxxxxxxx',
},
}),
],
})
class AppModule {}
In *.controller.ts file
import { UseGoogleAuth } from '@nestjs-hybrid-auth/google';
// OR
import { UseGoogleAuth } from '@nestjs-hybrid-auth/all';
@Controller
class AuthController {
@UseGoogleAuth()
@Get('auth/google')
googleLogin() {}
@UseGoogleAuth()
@Get('auth/google-callback') // This is the route configured in your Google oauth app
googleLoginCallback(req: Request) {
console.log(req.hybridAuthResult.user | accessToken | refreshToken);
}
}
Note:
- As passport uses express under the hood, fastify applications may not work with this package.
- Please read the full documentation for an identity provider before using it because some providers may require few additional dependencies to be installed to work properly.
Supported Identity Providers (Many more are yet to come)
Related
Maintainers & Contributors
Credits
Gopendra Jangir (Banner Image)