From 341154e928d772df5337686ed46a250e61fc404d Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Mon, 13 May 2024 00:27:29 -0400 Subject: [PATCH] auto assign username upon registration --- components/ModalContent/NewUserModal.tsx | 21 ++++-- layouts/AuthRedirect.tsx | 13 ---- lib/api/controllers/users/postUser.ts | 9 ++- pages/api/v1/auth/[...nextauth].ts | 1 - pages/choose-username.tsx | 93 ------------------------ 5 files changed, 20 insertions(+), 117 deletions(-) delete mode 100644 pages/choose-username.tsx diff --git a/components/ModalContent/NewUserModal.tsx b/components/ModalContent/NewUserModal.tsx index cf506cf..2e4290f 100644 --- a/components/ModalContent/NewUserModal.tsx +++ b/components/ModalContent/NewUserModal.tsx @@ -88,23 +88,28 @@ export default function NewUserModal({ onClose }: Props) { {emailEnabled ? (
-

Username

+

Email

setForm({ ...form, username: e.target.value })} - value={form.username} + onChange={(e) => setForm({ ...form, email: e.target.value })} + value={form.email} />
) : undefined}
-

Email

+

+ Username{" "} + {emailEnabled && ( + (Optional) + )} +

setForm({ ...form, email: e.target.value })} - value={form.email} + onChange={(e) => setForm({ ...form, username: e.target.value })} + value={form.username} />
diff --git a/layouts/AuthRedirect.tsx b/layouts/AuthRedirect.tsx index da1f6fc..9c35f6e 100644 --- a/layouts/AuthRedirect.tsx +++ b/layouts/AuthRedirect.tsx @@ -32,19 +32,6 @@ export default function AuthRedirect({ children }: Props) { router.push("/subscribe").then(() => { setRedirect(false); }); - } - // Redirect to "/choose-username" if user is authenticated and is either a subscriber OR subscription is undefiend, and doesn't have a username - else if ( - emailEnabled && - status === "authenticated" && - account.subscription?.active && - stripeEnabled && - account.id && - !account.username - ) { - router.push("/choose-username").then(() => { - setRedirect(false); - }); } else if ( status === "authenticated" && account.id && diff --git a/lib/api/controllers/users/postUser.ts b/lib/api/controllers/users/postUser.ts index 6a85e5f..464760e 100644 --- a/lib/api/controllers/users/postUser.ts +++ b/lib/api/controllers/users/postUser.ts @@ -72,6 +72,9 @@ export default async function postUser( }); if (!checkIfUserExists) { + const autoGeneratedUsername = + "user" + Math.round(Math.random() * 1000000000); + const saltRounds = 10; const hashedPassword = bcrypt.hashSync(body.password, saltRounds); @@ -85,7 +88,9 @@ export default async function postUser( const user = await prisma.user.create({ data: { name: body.name, - username: (body.username as string).toLowerCase().trim(), + username: emailEnabled + ? autoGeneratedUsername + : (body.username as string).toLowerCase().trim(), email: emailEnabled ? body.email?.toLowerCase().trim() : undefined, password: hashedPassword, emailVerified: new Date(), @@ -121,7 +126,7 @@ export default async function postUser( data: { name: body.name, username: emailEnabled - ? undefined + ? autoGeneratedUsername : (body.username as string).toLowerCase().trim(), email: emailEnabled ? body.email?.toLowerCase().trim() : undefined, password: hashedPassword, diff --git a/pages/api/v1/auth/[...nextauth].ts b/pages/api/v1/auth/[...nextauth].ts index 79b6f08..c59a38a 100644 --- a/pages/api/v1/auth/[...nextauth].ts +++ b/pages/api/v1/auth/[...nextauth].ts @@ -1,7 +1,6 @@ import { prisma } from "@/lib/api/db"; import NextAuth from "next-auth/next"; import CredentialsProvider from "next-auth/providers/credentials"; -import { AuthOptions } from "next-auth"; import bcrypt from "bcrypt"; import EmailProvider from "next-auth/providers/email"; import { PrismaAdapter } from "@auth/prisma-adapter"; diff --git a/pages/choose-username.tsx b/pages/choose-username.tsx deleted file mode 100644 index e1e6b69..0000000 --- a/pages/choose-username.tsx +++ /dev/null @@ -1,93 +0,0 @@ -import SubmitButton from "@/components/SubmitButton"; -import { signOut } from "next-auth/react"; -import { FormEvent, useState } from "react"; -import { toast } from "react-hot-toast"; -import { useSession } from "next-auth/react"; -import useAccountStore from "@/store/account"; -import CenteredForm from "@/layouts/CenteredForm"; -import TextInput from "@/components/TextInput"; -import AccentSubmitButton from "@/components/AccentSubmitButton"; - -export default function ChooseUsername() { - const [submitLoader, setSubmitLoader] = useState(false); - const [inputedUsername, setInputedUsername] = useState(""); - - const { data, status, update } = useSession(); - - const { updateAccount, account } = useAccountStore(); - - async function submitUsername(event: FormEvent) { - event.preventDefault(); - - setSubmitLoader(true); - - const redirectionToast = toast.loading("Applying..."); - - const response = await updateAccount({ - ...account, - username: inputedUsername, - }); - - if (response.ok) { - toast.success("Username Applied!"); - - update({ - id: data?.user.id, - }); - } else toast.error(response.data as string); - toast.dismiss(redirectionToast); - setSubmitLoader(false); - } - - return ( - -
-
-

- Choose a Username -

- -
- -
-

Username

- - setInputedUsername(e.target.value)} - /> -
-
-

- Feel free to reach out to us at{" "} - - support@linkwarden.app - {" "} - in case of any issues. -

-
- - - -
signOut()} - className="w-fit mx-auto cursor-pointer text-neutral font-semibold " - > - Sign Out -
-
-
-
- ); -}