file based auth
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { Handler } from 'hono';
|
||||
import { officerdb, count, Users } from 'officerdb';
|
||||
import { getUserCount, createUser } from 'officerdb';
|
||||
import { sign } from '@@/jwt';
|
||||
import { USER_ROLES, USER_STATUSES } from 'definitions';
|
||||
import type { USER_ROLES, USER_STATUSES } from 'definitions';
|
||||
import * as errors from '@@/custom-errors';
|
||||
import { sendMail } from 'emailer';
|
||||
|
||||
@@ -13,19 +13,14 @@ export const signupHandler: Handler = async function (ctx) {
|
||||
throw errors.BAD_REQUEST('Invalid email address');
|
||||
}
|
||||
|
||||
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');
|
||||
|
||||
const newUser = {
|
||||
const dbUser = await createUser({
|
||||
email: body.email as string,
|
||||
status: 'Unverified' as (typeof USER_STATUSES)[number],
|
||||
role: 'Admin' as (typeof USER_ROLES)[number],
|
||||
};
|
||||
|
||||
const insertedUsers = await officerdb.insert(Users).values(newUser).returning();
|
||||
if (!insertedUsers || insertedUsers.length === 0) throw errors.INTERNAL_SERVER_ERROR('Failed to create user');
|
||||
const dbUser = insertedUsers[0]!;
|
||||
});
|
||||
|
||||
const verificationCode = await sign({ id: dbUser.id, email: dbUser.email }, '24h');
|
||||
const url = `${origin}/auth/verify?verificationCode=${verificationCode}`;
|
||||
|
||||
Reference in New Issue
Block a user