file based auth
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { Handler } from 'hono';
|
||||
import { sendMail } from 'emailer';
|
||||
import { officerdb, count, Users } from 'officerdb';
|
||||
import { getUserCount, createUser } from 'officerdb';
|
||||
import { sign, verify } from '@@/jwt';
|
||||
import argon2 from 'argon2';
|
||||
import * as errors from '@@/custom-errors';
|
||||
@@ -13,8 +13,7 @@ export const bootstrapHandler: Handler = async function (ctx) {
|
||||
const token = body.token as string;
|
||||
const email = body.email as string;
|
||||
|
||||
const result = await officerdb.select({ count: count() }).from(Users);
|
||||
const userCount = result[0]?.count ?? 0;
|
||||
const userCount = getUserCount();
|
||||
if (userCount > 0) throw errors.FORBIDDEN('Registration is closed');
|
||||
|
||||
if (!token) {
|
||||
@@ -35,7 +34,7 @@ export const bootstrapHandler: Handler = async function (ctx) {
|
||||
return ctx.json({ ok: true });
|
||||
}
|
||||
|
||||
const payload = await verify(token).catch(() => null) as { email: string } | null;
|
||||
const payload = ((await verify(token).catch(() => null)) as { email: string } | null);
|
||||
if (!payload?.email) throw errors.BAD_REQUEST('Invalid or expired token');
|
||||
|
||||
const name = body.name as string;
|
||||
@@ -50,16 +49,15 @@ export const bootstrapHandler: Handler = async function (ctx) {
|
||||
|
||||
const passwordHash = await argon2.hash(password);
|
||||
|
||||
const insertedUsers = await officerdb.insert(Users).values({
|
||||
await createUser({
|
||||
email: payload.email,
|
||||
password: passwordHash,
|
||||
name: name.trim(),
|
||||
username: username.trim(),
|
||||
role: 'Super Admin',
|
||||
status: 'Active',
|
||||
}).returning();
|
||||
});
|
||||
|
||||
if (!insertedUsers || insertedUsers.length === 0) throw errors.INTERNAL_SERVER_ERROR('Failed to create user');
|
||||
syncUserPiConfig(payload.email).catch(() => {});
|
||||
return ctx.json({ ok: true });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user