Skip to content

[Google Cloud Run] SSR auth and page refresh issue in production #424

Open
@Flawe

Description

@Flawe

I'm seeing an issue when I'm signed in and refresh the page. Instead of staying signed in, I get redirected to the /signin page due to my middleware not detecting the user being logged in. It's almost as if nuxtServerInit isn't running, but only on refresh. Is that a thing? I have SSR set to true, which should be triggering this on the server side, but perhaps I'm missing something.

The only thing that seems to fix this is setting pwa.workbox.dev to true, but that's probably not a correct fix for production? I would also like to avoid doing a second redirect in beforeMount() if that's possible.

This is the setup I use:

store/index.js

export const actions = {
  nuxtServerInit({ dispatch, commit }, { res }) {
    if (res && res.locals && res.locals.user) {
      const { allClaims: claims, idToken: token, ...authUser } = res.locals.user
      commit('user/ON_AUTH_STATE_CHANGED_MUTATION', { authUser, claims, token })
    }
  },
}

store/user.js

export const getters = {
  isAuthenticated(state) {
    return state.uid !== null
  },
}
export const mutations = {
  ON_AUTH_STATE_CHANGED_MUTATION(state, { authUser, claims }) {
    if (!authUser) {
      state.uid = null
      return
    }

    const { uid } = authUser
    state.uid = uid
  },
}

And then in my middleware where I redirect to /signin

export default function ({ store, redirect }) {
  if (!store.getters['user/isAuthenticated']) {
    return redirect('/signin')
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionA general question about the module or Firebase.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions