blob: 3906d6558aaa95fd6e3072e5d26322524a838dc4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import { createUserSchema } from "@kioku/shared";
import * as argon2 from "argon2";
import { eq } from "drizzle-orm";
import { Hono } from "hono";
import { db, users } from "../db";
import { Errors } from "../middleware";
const auth = new Hono();
auth.post("/register", async (c) => {
const body = await c.req.json();
const parsed = createUserSchema.safeParse(body);
if (!parsed.success) {
throw Errors.validationError(parsed.error.issues[0]?.message);
}
const { username, password } = parsed.data;
// Check if username already exists
const existingUser = await db
.select({ id: users.id })
.from(users)
.where(eq(users.username, username))
.limit(1);
if (existingUser.length > 0) {
throw Errors.conflict("Username already exists", "USERNAME_EXISTS");
}
// Hash password with Argon2
const passwordHash = await argon2.hash(password);
// Create user
const [newUser] = await db
.insert(users)
.values({
username,
passwordHash,
})
.returning({
id: users.id,
username: users.username,
createdAt: users.createdAt,
});
return c.json({ user: newUser }, 201);
});
export { auth };
|