Saving oauth connections

This commit is contained in:
Gabriel Rosa
2022-08-23 15:42:33 -03:00
parent 0f079a7b22
commit 195fdae0e6
8 changed files with 120 additions and 28 deletions
+42 -16
View File
@@ -1,14 +1,19 @@
import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { InputsService } from '../inputs/inputs.service';
import { Request } from 'express';
import { ConnectionClientService } from '../connection/client.service';
import jwt from 'jsonwebtoken';
import DadosferaLogger from '@dadosfera/dadosfera-logs/dist';
@ApiTags('oauth')
@Controller('oauth')
export class OauthController {
frontendRedirectUri = '';
constructor(private inputService: InputsService) {
constructor(
private connectionService: ConnectionClientService,
private logger: DadosferaLogger,
) {
switch (process.env.ENV) {
case 'dev':
case 'stg':
@@ -31,7 +36,7 @@ export class OauthController {
@UseGuards(AuthGuard('hubspot'))
@Redirect()
async oauthHubspotCallback(@Req() req) {
return await this.callback(req, 'hubspot');
return await this.callback(req);
}
@Get('googleads')
@@ -44,7 +49,7 @@ export class OauthController {
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleAdsCallback(@Req() req) {
return await this.callback(req, 'googleads');
return await this.callback(req);
}
@Get('google-sheets')
@@ -57,7 +62,7 @@ export class OauthController {
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleSheetsCallback(@Req() req) {
return await this.callback(req, 'google-sheets');
return await this.callback(req);
}
@Get('facebook')
@@ -70,7 +75,7 @@ export class OauthController {
@UseGuards(AuthGuard('facebook'))
@Redirect()
async oauthFacebookCallback(@Req() req) {
return await this.callback(req, 'facebook');
return await this.callback(req);
}
@Get('mailchimp')
@@ -83,7 +88,7 @@ export class OauthController {
@UseGuards(AuthGuard('mailchimp'))
@Redirect()
async oauthMailchimpCallback(@Req() req) {
return await this.callback(req, 'mailchimp');
return await this.callback(req);
}
@Get('salesforce')
@@ -96,11 +101,11 @@ export class OauthController {
@UseGuards(AuthGuard('salesforce'))
@Redirect()
async oauthSalesforceCallback(@Req() req) {
return await this.callback(req, 'salesforce');
return await this.callback(req);
}
async callback(@Req() req: Request, plugin) {
const { error, state: customer_id } = req.query;
async callback(@Req() req: Request) {
const { error, state } = req.query;
if (error)
return {
@@ -116,12 +121,33 @@ export class OauthController {
const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
const response = await this.inputService.create({
info: { customer_id, customer: 'customer', user_id: 'empty' },
plugin,
credentials: { access_token, refresh_token },
});
let connectionInfo: any;
try {
connectionInfo = jwt.verify(state as string, process.env.JWT_PRIVATE_KEY);
} catch (error) {
return {
url: `${this.frontendRedirectUri}?error=invalid_state`,
};
}
return { url: `${this.frontendRedirectUri}?input_id=${response.input.id}` };
connectionInfo.properties.access_token = access_token;
connectionInfo.properties.refresh_token = refresh_token;
const { customer_id, customer_name, user_id } = connectionInfo;
try {
const response = await this.connectionService.createConnection(
connectionInfo,
{ customer_id, customer_name, user_id },
);
return {
url: `${this.frontendRedirectUri}?connection_id=${response.connection.id}`,
};
} catch (error) {
this.logger.error(error);
return {
url: `${this.frontendRedirectUri}?error=internal`,
};
}
}
}