Skip to content

Nuxt Auth middleware not working on reload page #1586

Open
@kiketordera

Description

@kiketordera

Reproduction

Follow instructions with Latest version of nuxt, vuefire and nuxt-fire

Steps to reproduce the bug

This middleware won't work when you reload the page manually from the reload button of the browser of navigating to specific link:

// middleware/auth.ts
export default defineNuxtRouteMiddleware(async (to, from) => {
  const user = await getCurrentUser()

  // redirect the user to the login page
  if (!user) {
    return navigateTo({
      path: '/login',
      query: {
        redirect: to.fullPath,
      },
    })
  }
})

Expected behavior

When an user is logged in, it does not redirect to login page either navigating clicking either reloading or going to a specific URL directly

Actual behavior

When an user is logged in, it does not redirect to login page if we are navigating clicking
BUT IT DOES NOT WORK AS EXPECTED WHEN either reloading or going to a specific URL directly

Additional information

Problem is that the middleware does not wait to the user to be loaded from firebase, it executes before so the users is not loaded.

I have SSR Enabled in my nuxt.config.ts:

  routeRules: {
    "**": { prerender: true },
  },

If I change it and add the route I want to go a spa, the middleware actually works as expected in that specific route (in this case '/profile':

  routeRules: {
    "/profile": { prerender: false, ssr: false },
    "**": { prerender: true },
  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions