nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
641 stars 99 forks source link

firebase-auth-sw.js is not getting installed & axios requests do not contain the firebase token in production && developement. #431

Closed cifuentesmx closed 3 years ago

cifuentesmx commented 3 years ago

Version

@nuxtjs/firebase: ^7.3.0 firebase: ^8.2.2 nuxt: ^2.14.6

Reproduction Link

https://github.com/robertocifuentes/firebase-nuxt-ssr-issue

Steps to reproduce

  1. Create a new nuxt project
  2. Add firebase, PWA, Axios modules
  3. Follow config instructions in Firebase module to get started with Auth and SSR: true in this tutorial
  4. Compile and start a server (production or development)

What is Expected?

When a user is logged in, all $axios requests will contain the firebase token (managed by the /firebase-auth-sw.js), so we can install a custom server which authenticates client requests trough Firebase admin SDK.

What is actually happening?

Apparently the firebase-auth-sw.js is not getting installed thus the axios requests do not contains the firebase token.

Is this an issue?

Even I've tried different workarounds, I didn't managed to solve it. I'm not sure this is an issue or a messed up configuration problem or just me not fully understanding some basic concept.

These are the headers of a request when running a production server, the cookies are empty:

imagen

lupas commented 3 years ago

Hey @robertocifuentes For me your code is working as expected. The service worker gets loaded and I can log out the authUser in the mutation:

 ON_AUTH_STATE_CHANGED_MUTATION(state, { authUser, claims }) {
    console.log(authUser)

Returns:

Screenshot 2021-01-17 at 12 34 49

Why do you expect axios to automatically include the Firebase token? I don't see where you add the Firebase token to the axios instance.

You can set authorization headers on Axios like so, I think you can do this on each request but also directly on the entire axios instance in ON_AUTH_STATE_CHANGED_MUTATION, so you only have to do it once.

But if there is an issue please refer to the axios repository.

Hope that helped,

br, Pascal

lupas commented 3 years ago

Made an example project for you here: https://github.com/lupas/nuxt-firebase-server-auth-demo

You can see the relevant code to add the authorization header to your axios instance here: https://github.com/lupas/nuxt-firebase-server-auth-demo/blob/master/packages/frontend/store/actions.js#L32