Skip to content

Commit

Permalink
[fetch-forms-from-google-drive] Store/re-use sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Feb 28, 2018
1 parent 43cc208 commit 58b75bb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.11.4",
"version": "1.11.5",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 14 additions & 1 deletion src/lib/google-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ const readline = require('readline-sync');
const warn = require('../lib/log').warn;

const SECRETS_FILE = './.gdrive.secrets.json';
const TOKENS_FILE = './.gdrive.session.json';

module.exports = () => {
const client = oauthClient();

if(!fs.exists(TOKENS_FILE)) return newSessionFor(client);

client.setCredentials(fs.readJson(TOKENS_FILE));

if(client.credentials.expiry_date < Date.now()) return newSessionFor(client);

return Promise.resolve(client);
};

function newSessionFor(client) {
const authUrl = client.generateAuthUrl({
scope: 'https://www.googleapis.com/auth/drive.readonly'
});
Expand All @@ -21,12 +32,14 @@ module.exports = () => {
client.getToken(accessCode, function (err, tokens) {
if(err) return reject(err);

fs.writeJson(TOKENS_FILE, tokens);

client.setCredentials(tokens);

resolve(client);
});
});
};
}

function oauthClient() {
let configFile;
Expand Down

0 comments on commit 58b75bb

Please sign in to comment.