Unable to remove cookie
Unanswered
Common paper wasp posted this in #help-forum
Common paper waspOP
Hello everyone,
I have a function that called on every page in order to validate session in database, if it didn't found the session or session is expired (by any reason) then remove cookie. Also it has a method to refresh them. But it doesn't work. Nextjs doesn't remove/update them. They still there.
I have a function that called on every page in order to validate session in database, if it didn't found the session or session is expired (by any reason) then remove cookie. Also it has a method to refresh them. But it doesn't work. Nextjs doesn't remove/update them. They still there.
export const validateRequest = cache(async () => {
const sessionId = cookies().get('auth_session')?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
const result = await validateSession(sessionId);
try {
if (result.session && result.session.fresh) {
const sessionCookie = createSessionCookie(result.session.id);
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
// equal to
//Cookie {
//name: 'auth_session',
//value: '',
//attributes: {
//httpOnly: true,
//secure: false,
//sameSite: 'lax',
//path: '/',
//maxAge: 0
//}
//}
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
}
} catch {}
return result;
});21 Replies
actually that's not true. You are using lucia, correct?
You have to invalidate the session to remove it
@Alex actually that's not true. You are using lucia, correct?
Common paper waspOP
Yes, it's lucia. If validate method returns null then session doesn't exists, why you need invalidate? This is example from official examples for nextjs.
invalidate method only removes it from db not from cookies
export async function middleware(req: NextRequest) {
const sessionId = req.cookies.get('auth_session')?.value
if (sessionId) {
const result = await validateSession(sessionId);
try {
if (result.session && result.session.fresh) {
const sessionCookie = createSessionCookie(result.session.id);
return NextResponse.next({
headers: {
"Set-Cookie": sessionCookie.serialize(),
},
});
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
// equal to
//Cookie {
//name: 'auth_session',
//value: '',
//attributes: {
//httpOnly: true,
//secure: false,
//sameSite: 'lax',
//path: '/',
//maxAge: 0
//}
//}
return NextResponse.next({
headers: {
"Set-Cookie": sessionCookie.serialize(),
},
});
}
} catch {}
}
}Common paper waspOP
it's v3
same, I am also using lucia v3. I can send you my code. But why do you want the cookie removed anyway?
@Ray ts
export async function middleware(req: NextRequest) {
const sessionId = req.cookies.get('auth_session')?.value
if (sessionId) {
const result = await validateSession(sessionId);
try {
if (result.session && result.session.fresh) {
const sessionCookie = createSessionCookie(result.session.id);
return NextResponse.next({
headers: {
"Set-Cookie": sessionCookie.serialize(),
},
});
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
// equal to
//Cookie {
//name: 'auth_session',
//value: '',
//attributes: {
//httpOnly: true,
//secure: false,
//sameSite: 'lax',
//path: '/',
//maxAge: 0
//}
//}
return NextResponse.next({
headers: {
"Set-Cookie": sessionCookie.serialize(),
},
});
}
} catch {}
}
}
Common paper waspOP
perfect, I cannot run it on edge because my db is running locally
@Common paper wasp perfect, I cannot run it on edge because my db is running locally
middleware run on locally in dev
@Alex same, I am also using lucia v3. I can send you my code. But why do you want the cookie removed anyway?
Common paper waspOP
1. It's invalid
2. I have a simple check in middleware to pass users only with cookies in protected routes otherwise redirects to login. Having invalid cookie gives me infinity redirects
2. I have a simple check in middleware to pass users only with cookies in protected routes otherwise redirects to login. Having invalid cookie gives me infinity redirects
@Ray middleware run on locally in dev
Common paper waspOP
there is no driver or adapter for db that allow to run db locally using tcp because edge doesn't support it, so you got errors in middleware something like "your package A using nodejs environment but we only support edge for no reason"
I don't know what driver you use but
DrizzleMySQLAdapter work for me in middleware@Ray I don't know what driver you use but `DrizzleMySQLAdapter` work for me in middleware
Common paper waspOP
Can you please show how you initialize drizzle orm?
nothing special, I just initialise it like the doc does
const adapter = new DrizzleMySQLAdapter(db, sessions, users);
export const lucia = new Lucia(adapter, {
sessionCookie: {
// this sets cookies with super long expiration
// since Next.js doesn't allow Lucia to extend cookie expiration when rendering pages
expires: false,
attributes: {
// set to `true` when using HTTPS
secure: process.env.NODE_ENV === "production",
},
},
});@Ray nothing special, I just initialise it like the doc does
ts
const adapter = new DrizzleMySQLAdapter(db, sessions, users);
export const lucia = new Lucia(adapter, {
sessionCookie: {
// this sets cookies with super long expiration
// since Next.js doesn't allow Lucia to extend cookie expiration when rendering pages
expires: false,
attributes: {
// set to `true` when using HTTPS
secure: process.env.NODE_ENV === "production",
},
},
});
Common paper waspOP
No, I mean not adapter but drizzle db itself
Connection
Which driver you use?
planetscale
Common paper waspOP
And it connects to local db?
no, connect to planetscale