aboutsummaryrefslogtreecommitdiff
path: root/src/gapi.js
diff options
context:
space:
mode:
authorDeterminant <ted.sybil@gmail.com>2019-02-04 02:06:59 -0500
committerDeterminant <ted.sybil@gmail.com>2019-02-04 02:06:59 -0500
commitd3d4345a7ab82e9715b1cbf398d43206d43737f6 (patch)
treeb2006c6dcb774e0e6799ba6c43713be68a695412 /src/gapi.js
parent0ea5a1140c52d15ebb7f37862c1fabce5b80547b (diff)
add settings page
Diffstat (limited to 'src/gapi.js')
-rw-r--r--src/gapi.js66
1 files changed, 58 insertions, 8 deletions
diff --git a/src/gapi.js b/src/gapi.js
index 25d1cbf..60e9b1a 100644
--- a/src/gapi.js
+++ b/src/gapi.js
@@ -2,19 +2,68 @@
import LRU from "lru-cache";
const gapi_base = 'https://www.googleapis.com/calendar/v3';
-const GApiError = {
- invalidSyncToken: 1,
- otherError: 2,
-};
+const GApiError = Object.freeze({
+ invalidSyncToken: Symbol("invalidSyncToken"),
+ notLoggedIn: Symbol("notLoggedIn"),
+ notLoggedOut: Symbol("notLoggedOut"),
+ otherError: Symbol("otherError"),
+});
function to_params(dict) {
return Object.entries(dict).filter(([k, v]) => v).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
}
-export function getAuthToken() {
+let loggedIn = null;
+
+function _getAuthToken(interactive = false) {
return new Promise(resolver =>
chrome.identity.getAuthToken(
- {interactive: true}, token => resolver(token)));
+ { interactive }, token => resolver([token, !chrome.runtime.lastError])))
+ .then(([token, ok]) => {
+ if (ok) return token;
+ else throw GApiError.notLoggedIn;
+ });
+}
+
+function _removeCachedAuthToken(token) {
+ return new Promise(resolver =>
+ chrome.identity.removeCachedAuthToken({ token }, () => resolver()));
+}
+
+export function getLoggedIn() {
+ if (loggedIn === null)
+ {
+ return _getAuthToken(false)
+ .then(() => {loggedIn = true})
+ .catch(() => {loggedIn = false; console.log("here");})
+ .then(() => loggedIn);
+ }
+ else return Promise.resolve(loggedIn);
+}
+
+export function getAuthToken() {
+ return getLoggedIn().then(b => {
+ if (b) return _getAuthToken(false);
+ else throw GApiError.notLoggedIn;
+ });
+}
+
+export function login() {
+ return getLoggedIn().then(b => {
+ if (!b) return _getAuthToken(true).then(() => loggedIn = true);
+ else throw GApiError.notLoggedOut;
+ });
+}
+
+export function logout() {
+ return getAuthToken().then(token => {
+ return fetch(`https://accounts.google.com/o/oauth2/revoke?${to_params({ token })}`,
+ { method: 'GET', async: true }).then(response => {
+ if (response.status === 200)
+ return _removeCachedAuthToken(token);
+ else throw GApiError.otherError;
+ });
+ }).then(() => loggedIn = false);
}
export function getCalendars(token) {
@@ -51,7 +100,7 @@ function getEvents(calId, token, syncToken=null, timeMin=null, timeMax=null, res
return response.json();
else if (response.status === 410)
throw GApiError.invalidSyncToken;
- else throw GApiError.otherErrors;
+ else throw GApiError.otherError;
})
.then(data => {
results.push(...data.items);
@@ -72,7 +121,6 @@ export class GCalendar {
constructor(calId, name, options={maxCachedItems: 100, nDaysPerSlot: 10, largeQuery: 10}) {
this.calId = calId;
this.name = name;
- this.token = getAuthToken();
this.syncToken = '';
this.cache = new LRU({
max: options.maxCachedItems,
@@ -83,6 +131,8 @@ export class GCalendar {
this.divider = 8.64e7 * this.options.nDaysPerSlot;
}
+ get token() { return getAuthToken(); }
+
dateToCacheKey(date) {
return Math.floor(date / this.divider);
}