-
|
Hi. I'm not fully grasping the concept of sessions, but I do notice that when I invalidate the user's current session through Is there a built in function that Lucia has that would delete the session record from the database after it's invalidated? Or would I have to implement it manually? Thanks a lot. EDIT: A few were asking about the adapter I'm using. I'm using the Prisma adapter. import { Lucia } from "lucia";
import { dev } from "$app/environment";
import { PrismaAdapter } from "@lucia-auth/adapter-prisma";
import db from "$lib/prisma";
import { Prisma, UserRole, type School, type Admin, type Student } from "@prisma/client";
const adapter = new PrismaAdapter(db.session, db.user)
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: !dev
}
},
getUserAttributes: (attributes) => {
return {
// attributes has the type of DatabaseUserAttributes
...attributes
};
}
});
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}
interface DatabaseUserAttributes {
email: string;
firstName: string;
lastName: string;
role: UserRole;
school: School | null;
admin: Admin | null;
student: Student | null;
schoolId: number | null;
}Here is import type { PageServerLoad } from './$types'
import { redirect, type Actions } from '@sveltejs/kit';
import { lucia } from '$lib/server/auth';
export const load: PageServerLoad = async (event) => {
console.log(event.locals.user);
return {
username: event.locals?.user?.firstName + " " + event.locals?.user?.lastName,
isLoggedIn: event.locals.user ? true : false
};
};
export const actions = {
default: async (event) => {
const cookieHeader = event.cookies.get("auth_session") ?? "";
const sessionId = lucia.readSessionCookie(cookieHeader) ?? "";
await lucia.invalidateSession(sessionId);
event.cookies.delete("auth_session", {path:"/"});
event.locals.user = null;
event.locals.session = null;
redirect(302, "/");
}
} satisfies Actions;And here is my folder structure, if needed. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
|
I think that any control implementation over repeated logins/registers in short periods of time (typical DDOS countermeasures) would do the trick, furthermore, maybe you could use that sessions that are not immediately deleted to control it, even thought there are other ways to achieve it. |
Beta Was this translation helpful? Give feedback.
-
|
Sessions should be deleted while running Which database adapter are you using? |
Beta Was this translation helpful? Give feedback.
-
|
Yeah seems like an issue with the adapter you're using. |
Beta Was this translation helpful? Give feedback.
-
|
I've diagnosed the issue. It seems Lucia is not invalidating any session because |
Beta Was this translation helpful? Give feedback.
-
|
Tested it quickly with our internal app ( next14+lucia3+prisma ). Our Logout action is the following: "use server"
import { cookies } from "next/headers"
import { redirect } from "next/navigation"
import { auth, lucia } from "@acme/auth"
export async function logoutAction(): Promise<ActionResult> {
const { session } = await auth()
if (!session) {
return {
error: "Unauthorized",
}
}
await lucia.invalidateSession(session.id)
const sessionCookie = lucia.createBlankSessionCookie()
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
)
return redirect("/auth")
}The But based on your code, it seems you're doing the same |
Beta Was this translation helpful? Give feedback.
-
const cookieHeader = event.cookies.get("auth_session") ?? "";
const sessionId = lucia.readSessionCookie(cookieHeader) ?? "";
await lucia.invalidateSession(sessionId);I figured out the answer. It turns out that the cookie I was getting from Fixed code: const cookieHeader = event.cookies.get("auth_session") ?? "";
await lucia.invalidateSession(cookieHeader); // succeeds |
Beta Was this translation helpful? Give feedback.
I figured out the answer. It turns out that the cookie I was getting from
auth_sessionwas already the id thatinvalidateSession()needed.readSessionCookie()is not needed anymore, I can pass the cookie value directly intoinvalidateSession().Fixed code: