Skip to content

Commit

Permalink
Merge pull request #384 from nuxt-community/authEmulatorSsrIssue
Browse files Browse the repository at this point in the history
[auth-emulator] Fixed auth ssr service worker to use emulator if emulator is in use
  • Loading branch information
lupas authored Nov 14, 2020
2 parents 9da40d4 + ab108ce commit 168aeb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ function loadAuth (options) {

// Add Service-Worker
addServiceWorker.call(this, options, 'firebase-auth-sw.js', {
authOptions: options.services.auth,
ignorePaths: [
'/__webpack_hmr',
'/_loading',
Expand Down
17 changes: 16 additions & 1 deletion lib/sw-templates/firebase-auth-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ importScripts(
firebase.initializeApp(<%= serialize(options.config) %>)
<% } %>

// Initialize authService
const authService = firebase.auth()

<% /* Uses emulator, if emulatorPort is set. */ %>
<% const authOptions = options.authOptions %>
<% if (['string', 'number'].includes(typeof authOptions.emulatorPort)) { %>
<% const emulatorHost =
typeof authOptions.emulatorHost === 'string'
? authOptions.emulatorHost
: 'http://localhost'
%>
authService.useEmulator('<%= `${emulatorHost}` %>:<%= `${authOptions.emulatorPort}` %>')
<% } %>


/**
* Returns a promise that resolves with an ID token if available.
* @return {!Promise<?string>} The promise that resolves with an ID token if
* available. Otherwise, the promise resolves with null.
*/
const getIdToken = () => {
return new Promise((resolve) => {
const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
const unsubscribe = authService.onAuthStateChanged((user) => {
unsubscribe()
if (user) {
// force token refresh as it might be used to sign in server side
Expand Down

0 comments on commit 168aeb0

Please sign in to comment.