mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-24 04:54:48 +00:00
129 lines
3.3 KiB
TypeScript
129 lines
3.3 KiB
TypeScript
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';
|
|
import { InputsService } from '../inputs/inputs.service';
|
|
import { Request } from 'express';
|
|
|
|
@ApiTags('oauth')
|
|
@Controller('oauth')
|
|
export class OauthController {
|
|
frontendRedirectUri = '';
|
|
constructor(private inputService: InputsService) {
|
|
switch (process.env.ENV) {
|
|
case 'dev':
|
|
case 'stg':
|
|
this.frontendRedirectUri = `https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`;
|
|
break;
|
|
case 'prd':
|
|
this.frontendRedirectUri = `https://app.dadosfera.ai/coletix/auth-callback`;
|
|
break;
|
|
default:
|
|
this.frontendRedirectUri = `http://localhost:4200/coletix/auth-callback`;
|
|
}
|
|
}
|
|
@Get('hubspot')
|
|
@UseGuards(AuthGuard('hubspot'))
|
|
async oauthHubspot() {
|
|
return true;
|
|
}
|
|
|
|
@Get('hubspot/callback')
|
|
@UseGuards(AuthGuard('hubspot'))
|
|
@Redirect()
|
|
async oauthHubspotCallback(@Req() req) {
|
|
return await this.callback(req, 'hubspot');
|
|
}
|
|
|
|
@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')
|
|
@UseGuards(AuthGuard('facebook'))
|
|
async oauthFacebook() {
|
|
return true;
|
|
}
|
|
|
|
@Get('facebook/callback')
|
|
@UseGuards(AuthGuard('facebook'))
|
|
@Redirect()
|
|
async oauthFacebookCallback(@Req() req) {
|
|
return await this.callback(req, 'facebook');
|
|
}
|
|
|
|
@Get('mailchimp')
|
|
@UseGuards(AuthGuard('mailchimp'))
|
|
async oauthMailchimp() {
|
|
return true;
|
|
}
|
|
|
|
@Get('mailchimp/callback')
|
|
@UseGuards(AuthGuard('mailchimp'))
|
|
@Redirect()
|
|
async oauthMailchimpCallback(@Req() req) {
|
|
return await this.callback(req, 'mailchimp');
|
|
}
|
|
|
|
@Get('salesforce')
|
|
@UseGuards(AuthGuard('salesforce'))
|
|
async oauthSalesforce() {
|
|
return true;
|
|
}
|
|
|
|
@Get('salesforce/callback')
|
|
@UseGuards(AuthGuard('salesforce'))
|
|
@Redirect()
|
|
async oauthSalesforceCallback(@Req() req) {
|
|
return await this.callback(req, 'salesforce');
|
|
}
|
|
|
|
async callback(@Req() req: Request, plugin) {
|
|
const { error, state: customer_id } = req.query;
|
|
|
|
if (error)
|
|
return {
|
|
url: `${this.frontendRedirectUri}?error=${error}`,
|
|
};
|
|
const request: any = req;
|
|
const { authInfo } = request;
|
|
|
|
if (!authInfo)
|
|
return {
|
|
url: `${this.frontendRedirectUri}?error=invalid_credentials`,
|
|
};
|
|
|
|
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 },
|
|
});
|
|
|
|
return { url: `${this.frontendRedirectUri}?input_id=${response.input.id}` };
|
|
}
|
|
}
|