|
| 1 | +/** |
| 2 | + * @fileoverview Web worker for Firebase Auth test app application. The |
| 3 | + * web worker tries to run operations on the Auth instance for testing purposes. |
| 4 | + */ |
| 5 | + |
| 6 | +importScripts('/dist/firebase-app.js'); |
| 7 | +importScripts('/dist/firebase-auth.js'); |
| 8 | +importScripts('config.js'); |
| 9 | + |
| 10 | +// Initialize the Firebase app in the web worker. |
| 11 | +firebase.initializeApp(config); |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns a promise that resolves with an ID token if available. |
| 15 | + * @return {!Promise<?string>} The promise that resolves with an ID token if |
| 16 | + * available. Otherwise, the promise resolves with null. |
| 17 | + */ |
| 18 | +var getIdToken = function() { |
| 19 | + return new Promise(function(resolve, reject) { |
| 20 | + firebase.auth().onAuthStateChanged(function(user) { |
| 21 | + if (user) { |
| 22 | + user.getIdToken().then(function(idToken) { |
| 23 | + resolve(idToken); |
| 24 | + }, function(error) { |
| 25 | + resolve(null); |
| 26 | + }); |
| 27 | + } else { |
| 28 | + resolve(null); |
| 29 | + } |
| 30 | + }); |
| 31 | + }).catch(function(error) { |
| 32 | + console.log(error); |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Runs various Firebase Auth tests in a web worker environment and confirms the |
| 38 | + * expected behavior. This is useful for manual testing in different browsers. |
| 39 | + * @param {string} googleIdToken The Google ID token to sign in with. |
| 40 | + * @return {!Promise<void>} A promise that resolves when all tests run |
| 41 | + * successfully. |
| 42 | + */ |
| 43 | +var runWorkerTests = function(googleIdToken) { |
| 44 | + var inMemoryPersistence = firebase.auth.Auth.Persistence.NONE; |
| 45 | + var expectedDisplayName = 'Test User'; |
| 46 | + var oauthCredential = firebase.auth.GoogleAuthProvider.credential( |
| 47 | + googleIdToken); |
| 48 | + var provider = new firebase.auth.GoogleAuthProvider(); |
| 49 | + var OPERATION_NOT_SUPPORTED_CODE = |
| 50 | + 'auth/operation-not-supported-in-this-environment'; |
| 51 | + var email = 'user' + Math.floor(Math.random() * 10000000000).toString() + |
| 52 | + '@example.com'; |
| 53 | + var pass = 'password'; |
| 54 | + return firebase.auth().setPersistence(inMemoryPersistence) |
| 55 | + .then(function() { |
| 56 | + firebase.auth().useDeviceLanguage(); |
| 57 | + return firebase.auth().signInAnonymously(); |
| 58 | + }) |
| 59 | + .then(function(user) { |
| 60 | + if (!user.uid) { |
| 61 | + throw new Error('signInAnonymously unexpectedly failed!'); |
| 62 | + } |
| 63 | + return user.updateProfile({displayName: expectedDisplayName}); |
| 64 | + }) |
| 65 | + .then(function() { |
| 66 | + if (firebase.auth().currentUser.displayName != expectedDisplayName) { |
| 67 | + throw new Error('Profile update failed!'); |
| 68 | + } |
| 69 | + return firebase.auth().currentUser.delete(); |
| 70 | + }) |
| 71 | + .then(function() { |
| 72 | + if (firebase.auth().currentUser) { |
| 73 | + throw new Error('currentUser.delete unexpectedly failed!'); |
| 74 | + } |
| 75 | + return firebase.auth().createUserWithEmailAndPassword(email, pass); |
| 76 | + }) |
| 77 | + .then(function(user) { |
| 78 | + if (user.email != email) { |
| 79 | + throw new Error( |
| 80 | + 'createUserWithEmailAndPassword unexpectedly failed!'); |
| 81 | + } |
| 82 | + return firebase.auth().fetchProvidersForEmail(email); |
| 83 | + }).then(function(providers) { |
| 84 | + if (providers.length == 0 || providers[0] != 'password') { |
| 85 | + throw new Error('fetchProvidersForEmail failed!'); |
| 86 | + } |
| 87 | + return firebase.auth().signInWithEmailAndPassword(email, pass); |
| 88 | + }) |
| 89 | + .then(function(user) { |
| 90 | + if (user.email != email) { |
| 91 | + throw new Error('signInWithEmailAndPassword unexpectedly failed!'); |
| 92 | + } |
| 93 | + return user.delete(); |
| 94 | + }) |
| 95 | + .then(function() { |
| 96 | + return firebase.auth().signInWithPopup(provider) |
| 97 | + .catch(function(error) { |
| 98 | + if (error.code != OPERATION_NOT_SUPPORTED_CODE) { |
| 99 | + throw error; |
| 100 | + } |
| 101 | + }); |
| 102 | + }) |
| 103 | + .then(function() { |
| 104 | + return firebase.auth().signInWithRedirect(provider) |
| 105 | + .catch(function(error) { |
| 106 | + if (error.code != OPERATION_NOT_SUPPORTED_CODE) { |
| 107 | + throw error; |
| 108 | + } |
| 109 | + }); |
| 110 | + }) |
| 111 | + .then(function() { |
| 112 | + return Promise.resolve().then(function() { |
| 113 | + return new firebase.auth.RecaptchaVerifier('id'); |
| 114 | + }).then(function() { |
| 115 | + throw new Error( |
| 116 | + 'RecaptchaVerifer instantiation succeeded unexpectedly!'); |
| 117 | + }).catch(function(error) { |
| 118 | + if (error.code != OPERATION_NOT_SUPPORTED_CODE) { |
| 119 | + throw error; |
| 120 | + } |
| 121 | + }); |
| 122 | + }) |
| 123 | + .then(function() { |
| 124 | + return firebase.auth().signInAndRetrieveDataWithCredential( |
| 125 | + oauthCredential); |
| 126 | + }) |
| 127 | + .then(function(result) { |
| 128 | + if (!result.user || |
| 129 | + !result.user.uid || |
| 130 | + !result.credential || |
| 131 | + !result.additionalUserInfo) { |
| 132 | + throw new Error( |
| 133 | + 'signInAndRetrieveDataWithCredential unexpectedly failed!'); |
| 134 | + } |
| 135 | + return firebase.auth().signOut(); |
| 136 | + }) |
| 137 | + .then(function() { |
| 138 | + if (firebase.auth().currentUser) { |
| 139 | + throw new Error('signOut unexpectedly failed!'); |
| 140 | + } |
| 141 | + }); |
| 142 | +}; |
| 143 | + |
| 144 | +/** |
| 145 | + * Handles the incoming message from the main script. |
| 146 | + * @param {!Object} e The message event received. |
| 147 | + */ |
| 148 | +self.onmessage = function(e) { |
| 149 | + if (e.data && e.data.type) { |
| 150 | + var result = {type: e.data.type}; |
| 151 | + switch (e.data.type) { |
| 152 | + case 'GET_USER_INFO': |
| 153 | + getIdToken().then(function(idToken) { |
| 154 | + result.idToken = idToken; |
| 155 | + result.uid = firebase.auth().currentUser && |
| 156 | + firebase.auth().currentUser.uid; |
| 157 | + self.postMessage(result); |
| 158 | + }); |
| 159 | + break; |
| 160 | + case 'RUN_TESTS': |
| 161 | + runWorkerTests(e.data.googleIdToken).then(function() { |
| 162 | + result.status = 'success'; |
| 163 | + self.postMessage(result); |
| 164 | + }).catch(function(error) { |
| 165 | + result.status = 'failure'; |
| 166 | + result.error = error; |
| 167 | + self.postMessage(result); |
| 168 | + }); |
| 169 | + break; |
| 170 | + default: |
| 171 | + self.postMessage({}); |
| 172 | + } |
| 173 | + } |
| 174 | +}; |
0 commit comments