lamina-core
Advanced tools
Comparing version
@@ -25,25 +25,39 @@ const querystring = require('./querystring'); | ||
// request the auth token and save it in the storage backend | ||
return this.storageBackend | ||
const authWithGoogle = () => | ||
this.authBackend | ||
.auth(this.clientSecret) | ||
.then( | ||
authCode => this.accessRequest.exchange(this.clientSecret, authCode), | ||
) | ||
.then(this.__store.bind(this)) | ||
.then(this.__scheduleRefresh.bind(this)); | ||
const read = this.storageBackend | ||
.read() | ||
.catch(() => null) | ||
.then(state => { | ||
console.log(`read state ${JSON.stringify(state, 0, 2)}`); | ||
if (state == null) { | ||
// if the read fails (there is no stored state), re-auth | ||
return authWithGoogle(); | ||
} else { | ||
console.log(`read state ${JSON.stringify(state, 0, 2)}`); | ||
this.accessToken = state.access_token; | ||
this.refreshToken = state.refresh_token; | ||
this.accessToken = state.access_token; | ||
this.refreshToken = state.refresh_token; | ||
if (state.expires_at < new Date().getTime() + 500) { | ||
console.log('refreshing token loaded from state'); | ||
return this.__refreshAccessToken(state.refresh_token); | ||
// on reading a bad state, re-auth | ||
if (!this.refreshToken || !this.accessToken) { | ||
console.log(`read bad state, re-authenticating`); | ||
return authWithGoogle(); | ||
} | ||
// on reading an old state, re-auth | ||
if (state.expires_at < new Date().getTime() + 500) { | ||
console.log('refreshing token loaded from state'); | ||
return this.__refreshAccessToken(state.refresh_token); | ||
} | ||
} | ||
}) | ||
.catch( | ||
() => | ||
this.authBackend | ||
.auth(this.clientSecret) | ||
.then( | ||
authCode => | ||
this.accessRequest.exchange(this.clientSecret, authCode), | ||
) | ||
.then(this.__storeAndScheduleRefresh.bind(this)), | ||
); | ||
}); | ||
return read; | ||
} | ||
@@ -56,6 +70,7 @@ | ||
.refresh(this.clientSecret, refreshToken) | ||
.then(this.__storeAndScheduleRefresh.bind(this)); | ||
.then(this.__store.bind(this)) | ||
.then(this.__scheduleRefresh.bind(this)); | ||
} | ||
__storeAndScheduleRefresh(res) { | ||
__store(res) { | ||
// res an object of form | ||
@@ -67,5 +82,21 @@ // { | ||
// } | ||
console.log('storing and scheduling refresh', res); | ||
console.log('saving state from', res); | ||
this.accessToken = res.access_token || this.accessToken; | ||
this.refreshToken = res.refresh_token || this.refreshToken; | ||
// store to disk for getting new tokens in the future | ||
let toStore = { | ||
access_token: this.accessToken, | ||
refresh_token: this.refreshToken, | ||
expires_at: new Date().getTime() + res.expires_in, | ||
}; | ||
return this.storageBackend.write(toStore).then(() => res); | ||
} | ||
__scheduleRefresh(res) { | ||
// disable pending timeout | ||
console.log('scheduling refresh'); | ||
if (this.accessTokenRefreshTimeout !== null) { | ||
@@ -77,15 +108,7 @@ clearTimeout(this.accessTokenRefreshTimeout); | ||
this.accessTokenRefreshTimeout = setTimeout( | ||
() => this.__refreshAccessToken.bind(this, res.refresh_token), | ||
() => this.__refreshAccessToken.bind(this, res.refreshToken), | ||
res.expires_in / 2, | ||
); | ||
this.accessToken = res.access_token || this.accessToken; | ||
this.refreshToken = res.refresh_token || this.refreshToken; | ||
// store to disk for getting new tokens in the future | ||
return this.storageBackend.write({ | ||
access_token: this.accessToken, | ||
refresh_token: this.refreshToken, | ||
expires_at: new Date().getTime() + res.expires_in, | ||
}); | ||
return res; | ||
} | ||
@@ -92,0 +115,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"name": "lamina-core", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "simple dependency-free and platform-independent library for interfacing with google sheets", | ||
@@ -8,0 +8,0 @@ "main": "main.js", |
10695
5.93%291
6.2%