mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 18:13:26 +00:00
session management
This commit is contained in:
30
src/routes/auth/session.ts
Normal file
30
src/routes/auth/session.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Session } from '@/db/models/Session';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const sessionRouter = makeRouter((app) => {
|
||||
app.delete(
|
||||
'/auth/session/:sid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
sid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
auth.assert();
|
||||
|
||||
const targetedSession = await em.findOne(Session, { id: params.sid });
|
||||
if (!targetedSession) return true; // already deleted
|
||||
|
||||
if (targetedSession.user !== auth.user.id)
|
||||
throw new StatusError('Cant delete sessions you dont own', 401);
|
||||
|
||||
await em.removeAndFlush(targetedSession);
|
||||
return true;
|
||||
}),
|
||||
);
|
||||
});
|
Reference in New Issue
Block a user