file based auth
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Handler } from 'hono';
|
||||
import { officerdb, eq, Users } from 'officerdb';
|
||||
import { updateUser } from 'officerdb';
|
||||
import * as errors from '@@/custom-errors';
|
||||
|
||||
export const updateUserHandler: Handler = async function (ctx) {
|
||||
@@ -8,10 +8,11 @@ export const updateUserHandler: Handler = async function (ctx) {
|
||||
|
||||
if (typeof name !== 'string') throw errors.BAD_REQUEST('Name is required');
|
||||
|
||||
await officerdb
|
||||
.update(Users)
|
||||
.set({ name, username: typeof username === 'string' ? username : undefined, avatar: avatar ?? null })
|
||||
.where(eq(Users.id, reqUser.id));
|
||||
await updateUser(reqUser.id, {
|
||||
name,
|
||||
username: typeof username === 'string' ? username : undefined,
|
||||
avatar: avatar ?? null,
|
||||
});
|
||||
|
||||
return ctx.json({ ok: true });
|
||||
};
|
||||
|
||||
@@ -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 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user