Last active
November 15, 2024 09:14
-
-
Save thisismydesign/a83efc23aa156d79216d9b97615639ac to your computer and use it in GitHub Desktop.
OAuth2 in NestJS for social login (Google, Facebook, Twitter, etc) /1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common'; | |
import { Request, Response } from 'express'; | |
import { GoogleOauthGuard } from './google-oauth.guard'; | |
@Controller('auth/google') | |
export class GoogleOauthController { | |
constructor(private jwtAuthService: JwtAuthService) {} | |
@Get() | |
@UseGuards(GoogleOauthGuard) | |
async googleAuth(@Req() _req) { | |
// Guard redirects | |
} | |
@Get('redirect') | |
@UseGuards(GoogleOauthGuard) | |
async googleAuthRedirect(@Req() req: Request, @Res() res: Response) { | |
// For now, we'll just show the user object | |
return req.user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
import { AuthGuard } from '@nestjs/passport'; | |
@Injectable() | |
export class GoogleOauthGuard extends AuthGuard('google') {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { GoogleOauthController } from './google-oauth.controller'; | |
import { GoogleOauthStrategy } from './google-oauth.strategy'; | |
@Module({ | |
imports: [], | |
controllers: [GoogleOauthController], | |
providers: [GoogleOauthStrategy], | |
}) | |
export class GoogleOauthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PassportStrategy } from '@nestjs/passport'; | |
import { Profile, Strategy } from 'passport-google-oauth20'; | |
import { Injectable } from '@nestjs/common'; | |
import { ConfigService } from '@nestjs/config'; | |
@Injectable() | |
export class GoogleOauthStrategy extends PassportStrategy(Strategy, 'google') { | |
constructor( | |
configService: ConfigService, | |
private readonly usersService: UsersService, | |
) { | |
super({ | |
// Put config in `.env` | |
clientID: configService.get<string>('OAUTH_GOOGLE_ID'), | |
clientSecret: configService.get<string>('OAUTH_GOOGLE_SECRET'), | |
callbackURL: configService.get<string>('OAUTH_GOOGLE_REDIRECT_URL'), | |
scope: ['email', 'profile'], | |
}); | |
} | |
async validate( | |
_accessToken: string, | |
_refreshToken: string, | |
profile: Profile, | |
) { | |
const { id, name, emails } = profile; | |
// Here a custom User object is returned. In the the repo I'm using a UsersService with repository pattern, learn more here: https://docs.nestjs.com/techniques/database | |
return { | |
provider: 'google', | |
providerId: id, | |
name: name.givenName, | |
username: emails[0].value, | |
}; | |
} | |
} |
@jabedzaman Here's an example for that: https://github.com/thisismydesign/nestjs-starter/tree/master/src/server/app/auth
@jabedzaman Here's an example for that: https://github.com/thisismydesign/nestjs-starter/tree/master/src/server/app/auth
thanks!!! was searching for this from so long 🫡
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to have multiple auth providers? like how shall i design the db schemas in that case?