Replies: 1 comment 1 reply
-
|
Without going too much into details 😅, the approach of storing the session data in the db might probably look something like this: type SessionData = {
id: string
};
export async function useUserSession() {
"use server";
const session = await useSession<SessionData>({
password: process.env.SESSION_SECRET as string,
name: "user",
});
if (!session.data.id) {
const result = db.sessions.create();
await session.update({
id: result.id
});
return result;
} else {
const result = db.sessions.get(session.data.id);
if (result.updatedAt + maxSessionAge < now) {
return
}
db.sessions.update(session.data.id, {
updatedAt: "now()"
})
return result
}
}The real complexity here comes from properly invalidating/clearing up old sessions in the db. It's recommended to regularely clean up the old sessions e.g. via a cronjob 😅. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The docs on sessions briefly touch on storing session data in a database:
...and that's it. There's no guidance on how to implement any of this. I've googled high and low, going so far as to poke around the Vinxi and H3 source code, and come up with nothing concrete. Can anybody give me any hints on accomplishing this?
Beta Was this translation helpful? Give feedback.
All reactions