-
Notifications
You must be signed in to change notification settings - Fork 164
Description
Users might log into the website using multiple clients, such as mobile phones and computers. When a user updates their information on the mobile end, I want to check upon refreshing the page on the PC end if there have been updates and then reset the session data.
Therefore, in sessionHooks
, I query the user table and compare timestamps. If the session login time is earlier than the data update time, I use setUserSession
to update the session data. It successfully updates on the server side, but the user data obtained with useUserSession
on the client side does not reflect the updates.
I found a solution to this issue (#357) that suggests using session.user = getSessionData(user)
and it works correctly.
However, I’m concerned that this might be a bug rather than a feature and might not be supported in future versions. Is my concern unfounded?
export default defineNitroPlugin(() => {
sessionHooks.hook('fetch', async (session, ev) => {
const db = useDB(ev)
const { users } = tables
if (session.user) {
const [user] = await db
.select()
.from(users)
.where(eq(users.id, session.user.id))
if (user) {
if (user.status !== 'ACTIVE') {
await clearUserSession(ev)
throw createError({
statusCode: 401,
message: 'Invalid token'
})
}
console.log(session.user.lastLoginAt, user.updatedAt)
if (session.user.lastLoginAt < user.updatedAt) { // update user data on other client
// didn't work
// await setUserSession(ev, {
// user: getSessionData(user)
// })
// it's work
session.user = getSessionData(user)
}
}
}
})
})