terminate-all.js (1061B)
1 import { app } from '../../../app.js'; 2 import { authenticateAdminSession } from '../../../lib/admin-auth.js'; 3 import { HttpError } from '../../../lib/http/http-error.js'; 4 import { isSystemAdmin } from '../../../lib/policy.js'; 5 import { destroyUserSessions } from '../../../lib/session.js'; 6 import { findUserById } from '../../../lib/users.js'; 7 8 export default { 9 method: 'delete', 10 path: '/api/v1/admin/users/:userId/sessions', 11 name: 'admin.userSessionsGlobal.terminateAll', 12 async handler(req) { 13 const auth = await authenticateAdminSession(req); 14 if (!isSystemAdmin(auth.user)) { 15 throw new HttpError(403, 'forbidden', 'System administrator access is required'); 16 } 17 const user = await findUserById(auth.systemDb, req.params.userId); 18 if (!user) { 19 throw new HttpError(404, 'user_not_found', 'User not found'); 20 } 21 22 const result = await destroyUserSessions(auth.systemDb, { userId: user.id }); 23 return new app.HttpOk( 24 { ok: true, destroyed: result.destroyed }, 25 { 'cache-control': 'no-store' } 26 ); 27 }, 28 };