33 lines
956 B
TypeScript
33 lines
956 B
TypeScript
import { and, eq } from 'drizzle-orm';
|
|
import { sessions } from '~~/drizzle/schema';
|
|
import { db } from '~~/server/lib/db';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const sessionData = await protectRoute(event);
|
|
|
|
const id = getRouterParam(event, 'id')!;
|
|
|
|
// signing out the current device goes through the normal sign out, which
|
|
// also clears its cookies
|
|
if (id === sessionData.session.id) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Cannot revoke the current session',
|
|
});
|
|
}
|
|
|
|
const deleted = await db.delete(sessions)
|
|
.where(and(eq(sessions.id, id), eq(sessions.userId, sessionData.user.id)))
|
|
.returning({ id: sessions.id });
|
|
|
|
if (deleted.length === 0) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'Not Found',
|
|
message: 'Session not found',
|
|
});
|
|
}
|
|
|
|
return { ok: true };
|
|
});
|