From e9ffb7ef8299efec8486e16c9abde92df4f19962 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sun, 17 Jul 2022 00:44:47 +0300 Subject: [PATCH] refactor: Use http client for slack authentication --- .../backend/src/apps/slack/authentication.ts | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/apps/slack/authentication.ts b/packages/backend/src/apps/slack/authentication.ts index 906b8db4..8b5e2c42 100644 --- a/packages/backend/src/apps/slack/authentication.ts +++ b/packages/backend/src/apps/slack/authentication.ts @@ -1,22 +1,38 @@ import type { IAuthentication, IApp, IJSONObject } from '@automatisch/types'; -import { WebClient } from '@slack/web-api'; +import HttpClient from '../../helpers/http-client'; +import qs from 'qs'; export default class Authentication implements IAuthentication { appData: IApp; connectionData: IJSONObject; - client: WebClient; + client: HttpClient; + static requestOptions: IJSONObject = { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }; constructor(appData: IApp, connectionData: IJSONObject) { - this.client = new WebClient(); + this.client = new HttpClient({ baseURL: 'https://slack.com/api' }); this.connectionData = connectionData; this.appData = appData; } async verifyCredentials() { - const { bot_id: botId, user: screenName } = await this.client.auth.test({ - token: this.connectionData.accessToken as string, - }); + const response = await this.client.post( + '/auth.test', + qs.stringify({ token: this.connectionData.accessToken }), + Authentication.requestOptions + ); + + if (response.data.ok === false) { + throw new Error( + `Error occured while verifying credentials: ${response.data.error}.(More info: https://api.slack.com/methods/auth.test#errors)` + ); + } + + const { bot_id: botId, user: screenName } = response.data; return { botId, @@ -27,9 +43,11 @@ export default class Authentication implements IAuthentication { async isStillVerified() { try { - await this.client.auth.test({ - token: this.connectionData.accessToken as string, - }); + await this.client.post( + '/auth.test', + qs.stringify({ token: this.connectionData.accessToken }), + Authentication.requestOptions + ); return true; } catch (error) {