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
+3 -6
View File
@@ -1,5 +1,5 @@
import type { Handler } from 'hono';
import { officerdb, eq, Users } from 'officerdb';
import { getUserById, updateUser } from 'officerdb';
import argon2 from 'argon2';
import { sign } from '@@/jwt';
import * as errors from '@@/custom-errors';
@@ -13,10 +13,7 @@ export const changePasswordHandler: Handler = async function (ctx) {
if (isProduction) validatePassword(newPassword);
const reqUser = ctx.get('user');
const dbUser = await officerdb.query.Users.findFirst({
where: eq(Users.id, reqUser.id),
columns: { password: true, username: true },
});
const dbUser = getUserById(reqUser.id);
if (!dbUser) throw errors.UNAUTHORIZED();
@@ -28,7 +25,7 @@ export const changePasswordHandler: Handler = async function (ctx) {
const newPasswordHash = await argon2.hash(newPassword);
// Use floored seconds-to-ms so the token iat (also floored) is never behind
const passwordChangedAt = Math.floor(Date.now() / 1000) * 1000;
await officerdb.update(Users).set({ password: newPasswordHash, passwordChangedAt }).where(eq(Users.id, reqUser.id));
await updateUser(reqUser.id, { password: newPasswordHash, passwordChangedAt });
const { id, email, name, role } = reqUser;
const username = dbUser.username ?? reqUser.username;