refactor: Pass app data within the constructor of an app
This commit is contained in:
@@ -1,47 +1,50 @@
|
||||
import { google as GoogleApi } from 'googleapis';
|
||||
import App from '../../models/app';
|
||||
import Field from '../../types/field';
|
||||
|
||||
export default class Authentication {
|
||||
oauthClient: any
|
||||
connectionData: any
|
||||
appData: any
|
||||
oauthClient: any;
|
||||
connectionData: any;
|
||||
appData: any;
|
||||
|
||||
scopes: string[] = [
|
||||
'https://www.googleapis.com/auth/datastore',
|
||||
'https://www.googleapis.com/auth/firebase',
|
||||
'https://www.googleapis.com/auth/user.emails.read',
|
||||
'profile',
|
||||
]
|
||||
];
|
||||
|
||||
constructor(connectionData: any) {
|
||||
this.appData = App.findOneByKey('firebase');
|
||||
constructor(appData: any, connectionData: any) {
|
||||
this.appData = appData;
|
||||
this.connectionData = connectionData;
|
||||
|
||||
this.oauthClient = new GoogleApi.auth.OAuth2(
|
||||
connectionData.consumerKey,
|
||||
connectionData.consumerSecret,
|
||||
this.oauthRedirectUrl,
|
||||
this.oauthRedirectUrl
|
||||
);
|
||||
|
||||
GoogleApi.options({ auth: this.oauthClient });
|
||||
}
|
||||
|
||||
get oauthRedirectUrl() {
|
||||
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
|
||||
return this.appData.fields.find(
|
||||
(field: Field) => field.key == 'oAuthRedirectUrl'
|
||||
).value;
|
||||
}
|
||||
|
||||
async createAuthData() {
|
||||
const url = this.oauthClient.generateAuthUrl({
|
||||
access_type: 'offline',
|
||||
scope: this.scopes
|
||||
scope: this.scopes,
|
||||
});
|
||||
|
||||
return { url };
|
||||
}
|
||||
|
||||
async verifyCredentials() {
|
||||
const { tokens } = await this.oauthClient.getToken(this.connectionData.oauthVerifier);
|
||||
const { tokens } = await this.oauthClient.getToken(
|
||||
this.connectionData.oauthVerifier
|
||||
);
|
||||
this.oauthClient.setCredentials(tokens);
|
||||
|
||||
const people = GoogleApi.people('v1');
|
||||
@@ -52,7 +55,9 @@ export default class Authentication {
|
||||
});
|
||||
|
||||
const { emailAddresses, resourceName: userId } = data;
|
||||
const primaryEmailAddress = emailAddresses.find(emailAddress => emailAddress.metadata.primary);
|
||||
const primaryEmailAddress = emailAddresses.find(
|
||||
(emailAddress) => emailAddress.metadata.primary
|
||||
);
|
||||
|
||||
return {
|
||||
consumerKey: this.connectionData.consumerKey,
|
||||
@@ -64,7 +69,7 @@ export default class Authentication {
|
||||
scope: tokens.scope,
|
||||
screenName: primaryEmailAddress.value,
|
||||
userId,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async isStillVerified() {
|
||||
@@ -72,7 +77,7 @@ export default class Authentication {
|
||||
await this.oauthClient.getTokenInfo(this.connectionData.accessToken);
|
||||
return true;
|
||||
} catch {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user