FIX: salesforce oauth tye

This commit is contained in:
Gabriel Rosa
2022-09-08 15:11:54 -03:00
parent 35c0520750
commit 311a4e4b93
3 changed files with 21 additions and 32 deletions
+19 -30
View File
@@ -8,7 +8,7 @@ import { PackTheMetadata } from 'src/utils/ PackTheMetadata';
@ApiTags('oauth')
@Controller('oauth')
export class OauthController {
redirectUrl: URL;
redirectUrl: string;
constructor(
private connectionService: ConnectionClientService,
@@ -17,19 +17,13 @@ export class OauthController {
switch (process.env.ENV) {
case 'dev':
case 'stg':
this.redirectUrl = new URL(
`https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`,
);
this.redirectUrl = `https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`;
break;
case 'prd':
this.redirectUrl = new URL(
`https://app.dadosfera.ai/coletix/auth-callback`,
);
this.redirectUrl = `https://app.dadosfera.ai/coletix/auth-callback`;
break;
default:
this.redirectUrl = new URL(
`http://localhost:4200/coletix/auth-callback`,
);
this.redirectUrl = `http://localhost:4200/coletix/auth-callback`;
}
}
@Get('hubspot')
@@ -112,27 +106,25 @@ export class OauthController {
async callback(@Req() req) {
const { error, state } = req.query;
const url = new URL(this.redirectUrl);
if (error) {
this.logger.error(error);
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set(
url.searchParams.set('error', 'Erro ao autorizar');
url.searchParams.set(
'error_description',
`Credenciais inválidas. Erro: [${error}]`,
);
return { url: this.redirectUrl.href };
return { url: url.href };
}
const { authInfo } = req;
if (!authInfo) {
this.logger.error('No authInfo', req);
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set(
'error_description',
`Credenciais inválidas.`,
);
return { url: this.redirectUrl.href };
url.searchParams.set('error', 'Erro ao autorizar');
url.searchParams.set('error_description', `Credenciais inválidas.`);
return { url: url.href };
}
const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
@@ -142,12 +134,12 @@ export class OauthController {
connectionInfo = jwt.verify(state as string, process.env.JWT_PRIVATE_KEY);
} catch (error) {
this.logger.error('Invalid state JWT', { state, req, error });
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set(
url.searchParams.set('error', 'Erro ao autorizar');
url.searchParams.set(
'error_description',
'Ocorreu um erro ao autorizá-lo. Por favor tente novamente',
);
return { url: this.redirectUrl.href };
return { url: url.href };
}
connectionInfo.properties.access_token = access_token;
@@ -160,19 +152,16 @@ export class OauthController {
connectionInfo,
PackTheMetadata({ customer_id, customer_name, user_id }),
);
this.redirectUrl.searchParams.set(
'connection_id',
response.connection.id,
);
return { url: this.redirectUrl.href };
url.searchParams.set('connection_id', response.connection.id);
return { url: url.href };
} catch (error) {
this.logger.error(error);
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set(
url.searchParams.set('error', 'Erro ao autorizar');
url.searchParams.set(
'error_description',
'Ocorreu um erro interno ao autorizá-lo. Por favor tente novamente mais tarde',
);
return { url: this.redirectUrl.href };
return { url: url.href };
}
}
}
@@ -45,7 +45,7 @@ export class SalesforceStrategy extends PassportStrategy(
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'file',
'application',
);
super.authenticate(req, options);
}