Spaces:
Sleeping
Sleeping
Andrew
commited on
Commit
·
8967568
1
Parent(s):
94a9d15
feat(auth): Add user registration route
Browse files
src/routes/login/register/+server.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { json } from "@sveltejs/kit";
|
| 2 |
+
import { z } from "zod";
|
| 3 |
+
import { collections } from "$lib/server/database";
|
| 4 |
+
import { hashPassword, generateRecoveryKey } from "$lib/server/passwords";
|
| 5 |
+
import { updateUserSession } from "../callback/userSession";
|
| 6 |
+
|
| 7 |
+
function escapeRegExp(string: string) {
|
| 8 |
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
export async function POST({ request, locals, cookies, getClientAddress }) {
|
| 12 |
+
const body = await request.json();
|
| 13 |
+
|
| 14 |
+
const { username, password, email } = z
|
| 15 |
+
.object({
|
| 16 |
+
username: z.string().min(1).trim(),
|
| 17 |
+
password: z.string().min(8),
|
| 18 |
+
email: z
|
| 19 |
+
.union([z.string().email(), z.literal("")])
|
| 20 |
+
.optional()
|
| 21 |
+
.transform((e) => (e === "" ? undefined : e)),
|
| 22 |
+
})
|
| 23 |
+
.parse(body);
|
| 24 |
+
|
| 25 |
+
// Check if user already exists
|
| 26 |
+
const existingUser = await collections.users.findOne({
|
| 27 |
+
$or: [
|
| 28 |
+
{ username: { $regex: new RegExp(`^${escapeRegExp(username)}$`, "i") } },
|
| 29 |
+
...(email ? [{ email: { $regex: new RegExp(`^${escapeRegExp(email)}$`, "i") } }] : []),
|
| 30 |
+
],
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
if (existingUser) {
|
| 34 |
+
return json({ message: "User already exists" }, { status: 409 });
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
const recoveryKey = generateRecoveryKey();
|
| 38 |
+
const passwordHash = await hashPassword(password);
|
| 39 |
+
const recoveryKeyHash = await hashPassword(recoveryKey); // Hash recovery key same way as password
|
| 40 |
+
|
| 41 |
+
await updateUserSession({
|
| 42 |
+
userData: {
|
| 43 |
+
authProvider: "password",
|
| 44 |
+
authId: username,
|
| 45 |
+
username,
|
| 46 |
+
name: username,
|
| 47 |
+
email,
|
| 48 |
+
passwordHash,
|
| 49 |
+
recoveryKeyHash,
|
| 50 |
+
},
|
| 51 |
+
locals,
|
| 52 |
+
cookies,
|
| 53 |
+
userAgent: request.headers.get("user-agent") ?? undefined,
|
| 54 |
+
ip: getClientAddress(),
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
return json({ recoveryKey });
|
| 58 |
+
}
|