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
+39 -14
View File
@@ -1,11 +1,4 @@
import {
BadRequestException,
Controller,
Get,
Redirect,
Req,
UseGuards,
} from '@nestjs/common';
import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { InputsClientService } from 'src/clients/inputs/client.service';
@@ -54,7 +47,32 @@ export class OauthController {
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleCallback(@Req() req) {
return await this.callback(req, 'google');
return await this.callback(req, 'googleads');
}
@Get('googleads')
@UseGuards(AuthGuard('google'))
async oauthGoogleAds() {
return true;
}
@Get('googleads/callback')
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleAdsCallback(@Req() req) {
return await this.callback(req, 'googleads');
}
@Get('google-sheets')
@UseGuards(AuthGuard('google'))
async oauthGoogleSheets() {
return true;
}
@Get('google-sheets/callback')
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleSheetsCallback(@Req() req) {
return await this.callback(req, 'google-sheets');
}
@Get('facebook')
@@ -70,15 +88,22 @@ export class OauthController {
return await this.callback(req, 'facebook');
}
async callback(@Req() req, plugin) {
const { authInfo } = req;
async callback(@Req() req: Request, plugin) {
const { error, state: customer_id } = req.query;
const request: Request = req;
if (error)
return {
url: `${this.frontendRedirectUri}?error=${error}`,
};
const request: any = req;
const { authInfo } = request;
if (!authInfo) throw new BadRequestException('Oauth tokens not found');
if (!authInfo)
return {
url: `${this.frontendRedirectUri}?error=invalid_credentials`,
};
const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
const { state: customer_id } = request.query;
const response = await this.inputService.create({
info: { customer_id, customer: 'customer', user_id: 'empty' },