Open tobiasdiez opened 1 month ago
Did you take a look at the fetch
hook (https://github.com/atinux/nuxt-auth-utils?tab=readme-ov-file#extend-session) to already implement this logic?
Yes, I did. But this is not run on server-routes (right?) and doesn't provide the sessionId.
What about creating a server util to wrap requireUserSession(event)
?
// server/utils/session.ts
export async function requireValidUserSession(event) {
const session = await requireUserSession(event)
// Do your logic here or throw createError(...)
// return the extended session
return session
})
What we can do is to expose the session.id
, or you can do you own with:
await setUserSession({ id: randomUUID(), user: { ... } })
Yes, the approach via requireValidUserSession
would work as well and I'm using something like this in https://github.com/JabRef/JabRefOnline/blob/main/server/middleware/validateSession.ts. I just thought that it is a very common pattern and thus would have liked the nuxt-auth module to expose such a hook.
it's tricky as you may want to add some local cache to avoid extending everytime based on the ID, at least you have a workaround :)
Often one needs to check if a session is still valid on the server-side. For example, one may want to present the user with an option to logout on all devices, which then should invalidate all existing sessions.
For this, one needs to query for every request the a sever-side session storage (and perhaps update it). Currently, this is relatively hard to implement. A few suggestions to make this easier:
useRawSession
method to directly access the h3 session (in other words make https://github.com/atinux/nuxt-auth-utils/blob/e0396255b2ebc6b3ef89b9dd62f0eb0fbf345389/src/runtime/server/utils/session.ts#L96 public) - alternatively, add thesessionId
to the public interface ofUserSession
sessionId
(and update timestamps like "last active" etc)Point two and three might look like: