file based auth
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user