|
| 1 | +import firebase from '../plugins/firebase' |
| 2 | + |
| 3 | +/** |
| 4 | + * Returns a promise that resolves with an ID token if available. |
| 5 | + * @return {!Promise<?string>} The promise that resolves with an ID token if |
| 6 | + * available. Otherwise, the promise resolves with null. |
| 7 | + */ |
| 8 | +const getIdToken = () => { |
| 9 | + return new Promise((resolve) => { |
| 10 | + const unsubscribe = firebase.auth().onAuthStateChanged((user) => { |
| 11 | + unsubscribe() |
| 12 | + if (user) { |
| 13 | + user.getIdToken().then( |
| 14 | + (idToken) => { |
| 15 | + resolve(idToken) |
| 16 | + }, |
| 17 | + () => { |
| 18 | + resolve(null) |
| 19 | + } |
| 20 | + ) |
| 21 | + } else { |
| 22 | + resolve(null) |
| 23 | + } |
| 24 | + }) |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +const getOriginFromUrl = (url) => { |
| 29 | + // https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript |
| 30 | + const pathArray = url.split('/') |
| 31 | + const protocol = pathArray[0] |
| 32 | + const host = pathArray[2] |
| 33 | + return protocol + '//' + host |
| 34 | +} |
| 35 | + |
| 36 | +self.addEventListener('fetch', (event) => { |
| 37 | + if (event.request.url.includes('firebasestorage.googleapis.com')) { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const requestProcessor = (idToken) => { |
| 42 | + let req = event.request |
| 43 | + // For same origin https requests, append idToken to header. |
| 44 | + if ( |
| 45 | + self.location.origin === getOriginFromUrl(event.request.url) && |
| 46 | + (self.location.protocol === 'https:' || |
| 47 | + self.location.hostname === 'localhost') && |
| 48 | + idToken |
| 49 | + ) { |
| 50 | + // Clone headers as request headers are immutable. |
| 51 | + const headers = new Headers() |
| 52 | + for (const entry of req.headers.entries()) { |
| 53 | + headers.append(entry[0], entry[1]) |
| 54 | + } |
| 55 | + // Add ID token to header. |
| 56 | + headers.append('authorization', 'Bearer ' + idToken) |
| 57 | + try { |
| 58 | + req = new Request(req.url, { |
| 59 | + method: req.method, |
| 60 | + headers, |
| 61 | + mode: 'same-origin', |
| 62 | + credentials: req.credentials, |
| 63 | + cache: req.cache, |
| 64 | + redirect: req.redirect, |
| 65 | + referrer: req.referrer, |
| 66 | + body: req.body, |
| 67 | + bodyUsed: req.bodyUsed, |
| 68 | + context: req.context, |
| 69 | + }) |
| 70 | + } catch (e) { |
| 71 | + // This will fail for CORS requests. We just continue with the |
| 72 | + // fetch caching logic below and do not pass the ID token. |
| 73 | + } |
| 74 | + } |
| 75 | + return fetch(req) |
| 76 | + } |
| 77 | + // Fetch the resource after checking for the ID token. |
| 78 | + // This can also be integrated with existing logic to serve cached files |
| 79 | + // in offline mode. |
| 80 | + event.respondWith(getIdToken().then(requestProcessor, requestProcessor)) |
| 81 | +}) |
| 82 | + |
| 83 | +self.addEventListener('activate', (event) => { |
| 84 | + event.waitUntil(clients.claim()) |
| 85 | +}) |
0 commit comments