chore: Extract authentication logic of the apps to their own classes

This commit is contained in:
Faruk AYDIN
2021-11-18 12:54:56 +01:00
committed by Ömer Faruk Aydın
parent 6b1dee053f
commit 46321f19f4
23 changed files with 707 additions and 617 deletions

View File

@@ -0,0 +1,56 @@
import TwitterApi from 'twitter-api-v2';
import App from '../../models/app';
import Field from '../../types/field';
export default class Authentication {
client: any
connectionData: any
appData: any
constructor(connectionData: any) {
this.client = new TwitterApi({
appKey: connectionData.consumerKey,
appSecret: connectionData.consumerSecret,
accessToken: connectionData.accessToken,
accessSecret: connectionData.accessSecret
});
this.connectionData = connectionData;
this.appData = App.findOneByKey('twitter');
}
async createAuthData() {
const appFields = this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl')
const callbackUrl = appFields.value;
const authLink = await this.client.generateAuthLink(callbackUrl);
return {
url: authLink.url,
accessToken: authLink.oauth_token,
accessSecret: authLink.oauth_token_secret,
}
}
async verifyCredentials() {
const verifiedCredentials = await this.client.login(this.connectionData.oauthVerifier)
return {
consumerKey: this.connectionData.consumerKey,
consumerSecret: this.connectionData.consumerSecret,
accessToken: verifiedCredentials.accessToken,
accessSecret: verifiedCredentials.accessSecret,
userId: verifiedCredentials.userId,
screenName: verifiedCredentials.screenName
}
}
async isStillVerified() {
try {
await this.client.currentUser();
return true;
} catch {
return false
}
}
}