* Start the auth flow and authorizes to Firebase. * @param{boolean} interactive True if the OAuth flow should request with an interactive mode.
(interactive)
| 53 | * @param{boolean} interactive True if the OAuth flow should request with an interactive mode. |
| 54 | */ |
| 55 | function startAuth(interactive) { |
| 56 | // Request an OAuth token from the Chrome Identity API. |
| 57 | chrome.identity.getAuthToken({interactive: !!interactive}, function(token) { |
| 58 | if (chrome.runtime.lastError && !interactive) { |
| 59 | console.log('It was not possible to get a token programmatically.'); |
| 60 | } else if(chrome.runtime.lastError) { |
| 61 | console.error(chrome.runtime.lastError); |
| 62 | } else if (token) { |
| 63 | // Authorize Firebase with the OAuth Access Token. |
| 64 | var credential = firebase.auth.GoogleAuthProvider.credential(null, token); |
| 65 | firebase.auth().signInWithCredential(credential).catch(function(error) { |
| 66 | // The OAuth token might have been invalidated. Lets' remove it from cache. |
| 67 | if (error.code === 'auth/invalid-credential') { |
| 68 | chrome.identity.removeCachedAuthToken({token: token}, function() { |
| 69 | startAuth(interactive); |
| 70 | }); |
| 71 | } |
| 72 | }); |
| 73 | } else { |
| 74 | console.error('The OAuth Token was null'); |
| 75 | } |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Starts the sign-in process. |