forked from ProtoSchool/protoschool.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
48 lines (41 loc) · 1.52 KB
/
Copy pathauth.js
File metadata and controls
48 lines (41 loc) · 1.52 KB
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
38
39
40
41
42
43
44
45
46
47
48
/*
Create auth object for connecting with the Google APIs.
All required information will be ready from env vars.
*/
const errorCode = require('err-code')
const { google } = require('googleapis')
if (!process.env.GOOGLE_CLIENT_ID) {
throw errorCode(
new Error('No config available. Add a .env file or make all the config available through env variables. Please check the docs at https://github.com/ProtoSchool/protoschool.github.io/tree/code/scripts'),
'NO_CONFIG'
)
}
const credentials = {
installed: {
client_id: process.env.GOOGLE_CLIENT_ID,
project_id: process.env.GOOGLE_PROJECT_ID,
auth_uri: process.env.GOOGLE_AUTH_URI,
token_uri: process.env.GOOGLE_TOKEN_URI,
auth_provider_x509_cert_url: process.env.GOOGLE_AUTH_PROVIDER_X509_CERT_URL,
client_secret: process.env.GOOGLE_CLIENT_SECRET,
redirect_uris: process.env.GOOGLE_REDIRECT_URIS.split(',')
}
}
if (!credentials.installed.client_id) {
throw new Error('Failed to read the config from .env file. Check that all the credentials are set.')
}
const googleAuth = new google.auth.OAuth2(
credentials.installed.client_id,
credentials.installed.client_secret,
credentials.installed.redirect_uris[0]
)
if (process.env.GOOGLE_ACCESS_TOKEN) {
googleAuth.setCredentials({
access_token: process.env.GOOGLE_ACCESS_TOKEN,
refresh_token: process.env.GOOGLE_REFRESH_TOKEN,
scope: process.env.GOOGLE_SCOPE,
token_type: process.env.GOOGLE_TOKEN_TYPE,
expiry_date: process.env.GOOGLE_EXPIRY_DATE
})
}
module.exports = googleAuth