Closed dosstx closed 3 years ago
I observe the same behavior where res.locals
is {}
on hard refresh.
I haven't tested it yet, but possible issue could be the following (mentioned in Nuxtjs docs):
Only the primary module (in store/index.js) will receive the NuxtServerInit action. You'll need to chain your module actions from there.
If that's the case, my nuxtServerInit is being called from store/user.js
... maybe that's why I have the problem. @yuyusalim-ms can you confirm yours?
So after moving user logic to store/index.js
and putting nuxtServerInit action there...
export const state = () => ({
currentUser: null,
userProfile: null
})
export const mutations = {
SET_AUTH_USER(state, payload) {
state.currentUser = payload
},
SET_USER_PROFILE(state, payload) {
state.userProfile = payload
}
}
export const actions = {
// Store action called nuxtServerInit:
async nuxtServerInit({ dispatch, commit }, { res }) {
if (res && res.locals && res.locals.user) {
console.log('nuxt server init: ', res.locals)
const { allClaims: claims, idToken: token, ...authUser } = res.locals.user
await dispatch('onAuthStateChangedAction', {
authUser,
claims
// token
})
}
},
...did not fix the problem for me. Still get null user object on hard refresh of page.
I haven't tested it yet, but possible issue could be the following (mentioned in Nuxtjs docs):
Only the primary module (in store/index.js) will receive the NuxtServerInit action. You'll need to chain your module actions from there.
If that's the case, my nuxtServerInit is being called from
store/user.js
... maybe that's why I have the problem. @yuyusalim-ms can you confirm yours?
This is interesting but my nuxtServerInit is in store/index.js
all along. Similar to your latest test, I’m getting null on hard refresh.
Hey @dosstx
If you have a look at https://github.com/lupas/nuxt-firebase-demo you can see that the user object can be accessed on the server in the middleware via store here. Maybe you can find your answer in that repo.
Did you follow Firebase Auth with SSR one-by-one? Do you have "firebase-admin" installed?
@lupas
Did you follow Firebase Auth with SSR one-by-one? Do you have "firebase-admin" installed?
I believe I did it correctly, but I am often wrong...so I will be sure to look carefully again.
I do have firebase admin installed.
Someone pointed this out to me about the service worker being bypassed when doing "shift reload" , aka "hard refresh":
Shift-reload If you force-reload the page (shift-reload) it bypasses the service worker entirely. It'll be uncontrolled. This feature is in the spec, so it works in other service-worker-supporting browsers.
https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#shift-reload
That seems to be what is happening in my case. So, you confirm that it is not happening for you?
UPDATE: Not sure what happened, but everything seems to be working now.....didn't change a thing. Perhaps a cache issue? I don't know...but all works now....I'll leave this up a few more days if you don't mind in case I have issues again.
Seems like all works now, so closing this for now. Let me know if you run into this error again.
This is interesting but my nuxtServerInit is in
store/index.js
all along. Similar to your latest test, I’m getting null on hard refresh.
@yuyusalim-ms I also have the same problem, did you manage to fix it?
I have a similar problem like this one: https://github.com/nuxt-community/firebase-module/issues/252
However, I tried the proposed solution but I am unsure if I did it correctly, thus posting here for help:
Here's my problem: When using middleware, the user store object is coming back as
null
if I try to access a page directly such as typing in the URL vs navigating to it.There is no problem when navigating, but the issue is in directly accessing a page by typing in the URL in the browser window or doing a hard refresh. My use case is trying to prevent a signed in user from accessing the login page (or sign up page), thus I thought I'd write a middleware like so but console logging it shows user is
null
on direct page access and hard refresh.middleware/auth
I've followed the Auth SSR tutorial, but perhaps I am making a mistake somewhere. Can someone help me get this configured properly?
nuxt.config.js
:store/user
:So, with above code, hard refresh (or direct navigation via browser URL) of my
login
page (when user is already logged in) shows this in console (note the console log ofuser?, null
from middleware)Thank you for any help!