refactor: Use http client for slack authentication
This commit is contained in:
@@ -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) {
|
||||
|
Reference in New Issue
Block a user