diff --git a/lib/slugify.ts b/lib/slugify.ts index a0a530ef..c6fba1f4 100644 --- a/lib/slugify.ts +++ b/lib/slugify.ts @@ -1,5 +1,5 @@ export const slugify = (str: string) => { - return str.replace(/\s+/g, "-").toLowerCase(); + return str.replace(/[^a-zA-Z0-9-]/g, "-").toLowerCase(); }; export default slugify; diff --git a/pages/api/auth/signup.ts b/pages/api/auth/signup.ts index c0d8ddcf..476bd60d 100644 --- a/pages/api/auth/signup.ts +++ b/pages/api/auth/signup.ts @@ -1,13 +1,16 @@ -import prisma from "../../../lib/prisma"; -import { hashPassword } from "../../../lib/auth"; +import { hashPassword } from "@lib/auth"; +import prisma from "@lib/prisma"; +import slugify from "@lib/slugify"; +import { NextApiRequest, NextApiResponse } from "next"; -export default async function handler(req, res) { +export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== "POST") { return; } const data = req.body; - const { username, email, password } = data; + const { email, password } = data; + const username = slugify(data.username); if (!username) { res.status(422).json({ message: "Invalid username" });