FIX: google strategy custom scopes

This commit is contained in:
Gabriel Rosa
2022-06-27 14:02:51 -03:00
parent 06a0ed3299
commit 2f5d940b46
4 changed files with 55 additions and 30 deletions
@@ -1,6 +1,6 @@
import { Profile, Strategy, StrategyOptions } from 'passport-google-oauth20';
import { PassportStrategy } from '@nestjs/passport';
import { Inject, Injectable, Req } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { OauthSecrets } from 'src/utils/OauthSecrets';
@Injectable()
@@ -12,12 +12,7 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
const options: StrategyOptions = {
clientID: oauthSecrets.google.client_id,
clientSecret: oauthSecrets.google.client_secret,
scope: [
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/userinfo.email',
],
callbackURL: oauthSecrets.google.redirect_uri,
callbackURL: '/oauth/googleads/callback',
};
const verify = (
accessToken: string,
@@ -32,16 +27,21 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
super(options, verify);
}
authenticate(@Req() req, options) {
authenticate(req, options) {
const path: string = req.route.path;
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');
if (path.includes('google-sheets'))
scope.push(
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly',
);
options.scope = scope;
options.state = customer_id;
options.accessType = 'offline';
options.prompt = 'consent';
super.authenticate(req, options);
}
async validate(...args) {
console.log('validate');
console.log(args);
}
}