file based auth

This commit is contained in:
2026-02-23 09:58:08 +00:00
parent 0bea406dcd
commit 2779fe22c6
40 changed files with 456 additions and 1010 deletions
+8 -11
View File
@@ -1,4 +1,4 @@
import { officerdb, eq, Users } from 'officerdb';
import { getUsers, getUserByEmail, getUserById, createUser, deleteUser } from 'officerdb';
import { createRouter } from '@@/create-router';
import { sign } from '@@/jwt';
import { USER_ROLES } from 'definitions';
@@ -15,7 +15,7 @@ usersRouter.get('/', async (ctx) => {
const user = ctx.get('user');
if (user.role !== 'Super Admin') throw errors.FORBIDDEN();
const users = await officerdb.query.Users.findMany();
const users = getUsers();
const sanitized = users.map(({ password, ...rest }) => rest);
return ctx.json(sanitized);
@@ -39,17 +39,14 @@ usersRouter.post('/invite', async (ctx) => {
? (role as (typeof USER_ROLES)[number])
: ('Member' as const);
const existing = await officerdb.query.Users.findFirst({ where: eq(Users.email, email) });
const existing = getUserByEmail(email);
if (existing) throw errors.CONFLICT('A user with this email already exists');
const insertedUsers = await officerdb.insert(Users).values({
const dbUser = await createUser({
email,
role: assignedRole,
status: 'Invited',
}).returning();
if (!insertedUsers || insertedUsers.length === 0) throw errors.INTERNAL_SERVER_ERROR('Failed to create user');
const dbUser = insertedUsers[0]!;
});
const origin = ctx.get('origin');
const verificationCode = await sign({ id: dbUser.id, email: dbUser.email }, '24h');
@@ -74,7 +71,7 @@ usersRouter.post('/:id/resend-invite', async (ctx) => {
const id = Number(ctx.req.param('id'));
if (!id || isNaN(id)) throw errors.BAD_REQUEST('Invalid user ID');
const target = await officerdb.query.Users.findFirst({ where: eq(Users.id, id) });
const target = getUserById(id);
if (!target) throw errors.NOT_FOUND('User not found');
if (target.status !== 'Invited') throw errors.BAD_REQUEST('User is not in Invited status');
@@ -101,9 +98,9 @@ usersRouter.delete('/:id', async (ctx) => {
if (!id || isNaN(id)) throw errors.BAD_REQUEST('Invalid user ID');
if (id === reqUser.id) throw errors.BAD_REQUEST('Cannot delete yourself');
const target = await officerdb.query.Users.findFirst({ where: eq(Users.id, id) });
const target = getUserById(id);
if (!target) throw errors.NOT_FOUND('User not found');
await officerdb.delete(Users).where(eq(Users.id, id));
await deleteUser(id);
return ctx.json({ ok: true });
});