forked from elasticio/gspreadsheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifyCredentials.js
37 lines (32 loc) · 1.18 KB
/
verifyCredentials.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const logger = require('@elastic.io/component-logger')();
const { GoogleOauth2Client } = require('./lib/client');
async function verify(credentials) {
// for now sailor hasn't opportunity emit something from verify credentials context
const context = {
logger,
emit: (emitType) => {
logger.warn(`Can not call ${emitType} from verify credentials context.`);
},
};
// eslint-disable-next-line no-use-before-define
checkOauth2EnvarsPresence();
const googleOauth2Client = new GoogleOauth2Client(credentials, context);
try {
await googleOauth2Client.listOfSpreadsheets();
logger.info('Credentials was successfully verified');
} catch (e) {
logger.error('Credentials verification failed');
throw e;
}
}
function checkOauth2EnvarsPresence() {
if (!process.env.OAUTH_CLIENT_ID) {
if (!process.env.OAUTH_CLIENT_SECRET) {
throw new Error('Environment variables are missed: OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET');
}
throw new Error('Environment variables are missed: OAUTH_CLIENT_ID');
} else if (!process.env.OAUTH_CLIENT_SECRET) {
throw new Error('Environment variables are missed: OAUTH_CLIENT_SECRET');
}
}
module.exports = verify;