question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component setup runs before router middleware is resolved

See original GitHub issue

Environment


  • Operating System: Darwin
  • Node Version: v18.12.0
  • Nuxt Version: 3.0.0-rc.13
  • Nitro Version: 0.6.1
  • Package Manager: npm@8.19.2
  • Builder: vite
  • User Config: runtimeConfig, vite, modules, i18n
  • Runtime Modules: @pinia/nuxt@0.4.3, @nuxtjs/i18n@8.0.0-beta.1
  • Build Modules: -

Reproduction

// middleware/auth.global.js
export default defineNuxtRouteMiddleware(async (to)=> {
  if (process.client) return

  const app = useNuxtApp()
  const event = app.ssrContext.event
  const accessToken = getCookie(event, 'accessToken')
  
  // Authenticate if accessToken exists
  // ...

  const { isLoggedIn } = useUserStore()
  const publicPaths = [
    //.. some public paths not needing authentication
  ]
  if (!publicPaths.includes(to.path) && !isLoggedIn) {
    return navigateTo('/login')
  } else if (publicPaths.includes(to.path) && isLoggedIn) {
    return navigateTo('/')
  } 
})

// pages/index.vue
export default defineNuxtComponent({
  async setup() {
    const {data: someData} = await useAsyncData(()=> {
      return $fetch('/api/some_api_auth_required', {
        method: 'get',
        headers: useRequestHeaders(['cookie'])
      })
    })
    return {
      someData
    }
  }
})

Describe the bug

I’m seeing that setup block data fetching is being called before router middleware is resolved.

When I visit ‘/’ without authentication, the page redirects to ‘/login’ like expected.

But, on nuxt server console, I’ll see that '/api/some_api_auth_required' was called and failed because accesstoken doesnt exist (not authenticated).

Is this normal behavior? If so, how w

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BracketJohncommented, Dec 23, 2022

This issue also appears in nuxt-auth: https://github.com/sidebase/nuxt-auth/issues/100

1reaction
danielroecommented, Nov 15, 2022

your reproduction was not using nuxt edge (if you look in the package-lock.json) - likely a stackblitz issue.

the current edge release is unstable but you should be able to test in rc14 shortly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Controller is constructed before route middleware are ...
Version: Laravel 9 PHP: 8.1 The problem that I'm having is that Laravel constructs the controller object and it's entire dependency injected ...
Read more >
Route running before Middleware promise completes
I am having an issue with my code and some middleware. I have a function that gets a list of nav items for...
Read more >
Yes, this is how vue-router guards work & when to use them
Basically, Vue Router only switches components for you depending on the URL. ... This is also known as the middleware pattern.
Read more >
Understanding Vue middleware pipelines
Using the beforeEach hook, we can run our middleware. We first check to see if the current route being processed has a meta...
Read more >
Writing middleware for use in Express apps
Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found