aboutsummaryrefslogblamecommitdiff
path: root/src/gapi.js
blob: 60e9b1ad0ffed043c79169c0203af09b96d550dd (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                   
                            

                                                           





                                                 
 
                          
                                                                                                                                     

 


                                             

                                     













































                                                                                           


                                     
                                                                                          




                                           



                                                                           

 


                                                                                                         

                                           
 
                                                                                                     




                                                                                                             

                    




                                             
                                                 
                                                     
                                                
              
                           














                                                               
                                                                                               

                           
                            








                                                          

                                          

                                               

     




                                                                       


                






                                      

     











                                                               
 


















                                                              



                                      
                           



                                      

                                          
                                      
                                              
                           
                           

                                             


                                                 



                    



                                                        



                                  
                         



                                                            


                                                                    
                                                          
                                                       





                       




                                                               






                                         
                                                                      




                                                                                                











                                                                                                           
                        
                                                   



                                    


                           






































                                                                                              
                                            







                                                                 

                                                         







                                                                                

     
/* global chrome */
import LRU from "lru-cache";
const gapi_base = 'https://www.googleapis.com/calendar/v3';

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('&');
}

let loggedIn = null;

function _getAuthToken(interactive = false) {
    return new Promise(resolver =>
        chrome.identity.getAuthToken(
            { 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 (