From 74a6eb8132eea563b6815c17eec47681988d3c1e Mon Sep 17 00:00:00 2001 From: Gabriel Rosa Date: Mon, 27 Jun 2022 15:51:24 -0300 Subject: [PATCH] FIX: google redirect uri fixed --- .../passport-strategies/google-strategy.ts | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/modules/oauth/passport-strategies/google-strategy.ts b/src/modules/oauth/passport-strategies/google-strategy.ts index 1affe26..7374ea7 100644 --- a/src/modules/oauth/passport-strategies/google-strategy.ts +++ b/src/modules/oauth/passport-strategies/google-strategy.ts @@ -5,6 +5,7 @@ import { OauthSecrets } from 'src/utils/OauthSecrets'; @Injectable() export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { + redirect_uri: string; constructor( @Inject('OAUTH_SECRETS') private readonly oauthSecrets: OauthSecrets, @@ -25,24 +26,42 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { }; super(options, verify); + switch (process.env.ENV) { + case 'dev': + case 'stg': + this.redirect_uri = `https://maestro.${process.env.ENV}.dadosfera.ai/oauth`; + break; + case 'prd': + this.redirect_uri = `https://maestro.dadosfera.ai/oauth`; + break; + default: + this.redirect_uri = `/oauth`; + break; + } } authenticate(req, options) { - const path: string = req.route.path; + if (req.route.path.includes('callback')) { + return super.authenticate(req, options); + } + + const plugin: string = req.route.path.replace('/oauth/', ''); + const { customer_id } = req.query; - const scope: string[] = ['https://www.googleapis.com/auth/userinfo.email']; - if (path.includes('googleads')) { - scope.push('https://www.googleapis.com/auth/adwords'); - options.callbackURL = '/oauth/googleads/callback'; + options.scope = ['https://www.googleapis.com/auth/userinfo.email']; + options.callbackURL = `${this.redirect_uri}/${plugin}/callback`; + + switch (plugin) { + case 'googleads': + options.scope.push('https://www.googleapis.com/auth/adwords'); + break; + case 'google-sheets': + options.scope.push( + 'https://www.googleapis.com/auth/drive.metadata.readonly', + 'https://www.googleapis.com/auth/spreadsheets.readonly', + ); + break; } - if (path.includes('google-sheets')) { - scope.push( - 'https://www.googleapis.com/auth/drive.metadata.readonly', - 'https://www.googleapis.com/auth/spreadsheets.readonly', - ); - options.callbackURL = '/oauth/google-sheets/callback'; - } - options.scope = scope; options.state = customer_id; options.accessType = 'offline'; options.prompt = 'consent';