import { AuthenticateOptionsGoogle, Profile, Strategy, StrategyOptions, } from 'passport-google-oauth20'; import { PassportStrategy } from '@nestjs/passport'; import { Inject, Injectable } from '@nestjs/common'; import { OauthSecrets } from 'src/utils/OauthSecrets'; import { Request } from 'express'; import jwt from 'jsonwebtoken'; @Injectable() export class GoogleLoginStrategy extends PassportStrategy( Strategy, 'google-login', ) { redirect_uri: string; constructor( @Inject('OAUTH_SECRETS') private readonly oauthSecrets: OauthSecrets, ) { const options: StrategyOptions = { clientID: oauthSecrets['google-login'].client_id, clientSecret: oauthSecrets['google-login'].client_secret, callbackURL: oauthSecrets['google-login'].redirect_uri, scope: ['email', 'profile', 'openid'], }; const verify = ( accessToken: string, refreshToken: string, profile: Profile, done, ) => { return done(null, profile, { accessToken, refreshToken }); }; super(options, verify); } authenticate(req: Request, options: AuthenticateOptionsGoogle) { const language = req.headers['dadosfera-lang'] || req.query.language; options.state = jwt.sign({ language }, process.env.JWT_PRIVATE_KEY); super.authenticate(req, options); } }