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.

How to use Session in Nuxt3 middleware

See original GitHub issue

Ask 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:closed
  • Created 10 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
re-mxpcommented, Nov 30, 2022

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.

if (process.server) {
    const event = useRequestEvent();
    const { session } = event.context;
   // Access values in session
   ....
}
1reaction
BracketJohncommented, Dec 2, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

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