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
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
You can install the library for all providers or install it separately for each identity provider.
npm install @nestjs-hybrid-auth/all --save
yarn add @nestjs-hybrid-auth/all
npm install @nestjs-hybrid-auth/google --save
yarn add @nestjs-hybrid-auth/google
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.
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 {}
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.
Gopendra Jangir (Banner Image)