How to use Session in Nuxt3 middleware
See original GitHub issueAsk your question
When I refresh the web browser, wanted to read session value from a middleware. I tried like below but not getting any value. An example of how to use useSession in the serverside middleware would be great.
export default defineNuxtRouteMiddleware(async (to, from) => {
const { session } = await useSession()
console.log(session.value)
})
Additional information
No response
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Session middleware for h3 and Nuxt apps. - GitHub
Add session support in h3 and Nuxt apps using express-session. Installation. npm install h3-session ...
Read more >node.js - Nuxt : How to get the session values from express
I assume that you are using SPA mode. So you can use API to get that userId. router.get('/testGetUserid', function(req, ...
Read more >nuxt-session - npm
Add session support in Nuxt.js, accessible in server middleware. ... Start using nuxt-session in your project by running `npm i ...
Read more >The serverMiddleware Property - Nuxt
If you don't want middleware to register for all routes, use the Object form with a specific path. If you don't do this,...
Read more >NuxtJS Crash Course: Middleware - Laracasts
We can make use of middleware in Nuxt to achieve this. ... But wouldn't the local storage also remain if the session expires?...
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
Route Middleware is executed on server and client side. Check if you are on the server with
process.server
and use following nuxt composable https://nuxt.com/docs/api/composables/use-request-event.Hey @smorcuend 👋
This sadly is a known problem in the way nitro / nuxt adds (module) middlewares. The session middleware currently runs after your middlewares, so there’s no access to the session in your middlewares.
We’ve opened an issue for this in the nuxt repo here: https://github.com/nuxt/framework/issues/9396
Once that is fixed / there is a documented way to resolve this we will roll it out here.