Skip to content

Commit f6005b8

Browse files
authored
[CAL-409] Prevents usernames with special characters (#668)
1 parent dd9f5fe commit f6005b8

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/slugify.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const slugify = (str: string) => {
2-
return str.replace(/\s+/g, "-").toLowerCase();
2+
return str.replace(/[^a-zA-Z0-9-]/g, "-").toLowerCase();
33
};
44

55
export default slugify;

pages/api/auth/signup.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import prisma from "../../../lib/prisma";
2-
import { hashPassword } from "../../../lib/auth";
1+
import { hashPassword } from "@lib/auth";
2+
import prisma from "@lib/prisma";
3+
import slugify from "@lib/slugify";
4+
import { NextApiRequest, NextApiResponse } from "next";
35

4-
export default async function handler(req, res) {
6+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
57
if (req.method !== "POST") {
68
return;
79
}
810

911
const data = req.body;
10-
const { username, email, password } = data;
12+
const { email, password } = data;
13+
const username = slugify(data.username);
1114

1215
if (!username) {
1316
res.status(422).json({ message: "Invalid username" });

0 commit comments

Comments
 (0)