Skip to content

Commit be5fe22

Browse files
committed
Refactored username assignment to use fallback based on user email address
1 parent 9b9f386 commit be5fe22

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

app/api/webhook/clerk/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ export async function POST(req: Request) {
6060
const { id, email_addresses, image_url, username, first_name, last_name } =
6161
evt.data;
6262

63+
const parts = email_addresses[0].email_address.split("@");
64+
6365
// create a new user in database
6466
const mongoUser = await createUser({
6567
clerkId: id,
6668
name: `${first_name}${last_name ? ` ${last_name}` : ""}`,
67-
username: username || "",
69+
username: username || `${parts[0]}-${parts[1].split(".")[0]}`,
6870
email: email_addresses[0].email_address,
6971
picture: image_url,
7072
});
7173

74+
console.log(mongoUser);
75+
7276
return NextResponse.json({ message: "User created", user: mongoUser });
7377
}
7478

components/forms/Profile.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const Profile = ({ clerkId, user }: Props) => {
3232
const router = useRouter();
3333
const pathname = usePathname();
3434
const parsedUser = JSON.parse(user);
35+
3536
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
3637

3738
const form = useForm<z.infer<typeof ProfileValidation>>({
@@ -61,6 +62,7 @@ const Profile = ({ clerkId, user }: Props) => {
6162
},
6263
path: pathname,
6364
});
65+
6466
router.push("/");
6567
} catch (error) {
6668
toast({

0 commit comments

Comments
 (0)