Component setup runs before router middleware is resolved
See original GitHub issueEnvironment
- 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:
- Created 10 months ago
- Comments:11 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This issue also appears in
nuxt-auth
: https://github.com/sidebase/nuxt-auth/issues/100your 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.