The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
wip email signup
Kiran K55ba51ce353b
diff --git a/apps/web/app/app.dub.co/(auth)/login/page.tsx b/apps/web/app/app.dub.co/(auth)/login/page.tsxindex ce7ed1b84d..fe6afa3131 100644--- a/apps/web/app/app.dub.co/(auth)/login/page.tsx+++ b/apps/web/app/app.dub.co/(auth)/login/page.tsx@@ -22,9 +22,17 @@ export default function LoginPage() {Start creating short links with superpowers</p></div>- <div className="flex flex-col bg-gray-50 px-4 py-8 sm:px-16">+ <div className="flex flex-col gap-5 bg-gray-50 px-4 py-8 sm:px-16"><SignInWithEmailPassword />- <div className="my-8 border-t border-gray-300 bg-red-300" />++ <div className="relative flex items-center">+ <div className="border-subtle flex-grow border-t" />+ <span className="text-subtle leadning-none mx-2 flex-shrink text-sm font-normal text-gray-700">+ Or continue with+ </span>+ <div className="border-subtle flex-grow border-t" />+ </div>+<div className="space-y-3"><Suspensefallback={diff --git a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsxindex 6d82cd086c..18a4afd5bf 100644--- a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx+++ b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx@@ -87,7 +87,7 @@ export const SignInWithEmailPassword = () => {{...register("password")}/><Button- text="Sign in"+ text="Sign In"type="submit"loading={isSubmitting}disabled={disabled}diff --git a/apps/web/app/app.dub.co/(auth)/register/form.tsx b/apps/web/app/app.dub.co/(auth)/register/form.tsxindex 4e6c515b01..22bffa4d31 100644--- a/apps/web/app/app.dub.co/(auth)/register/form.tsx+++ b/apps/web/app/app.dub.co/(auth)/register/form.tsx@@ -35,6 +35,7 @@ export default function RegisterForm() {icon={<Google className="h-4 w-4" />}/><Button+ variant="secondary"text="Continue with GitHub"onClick={() => {setClickedGithub(true);diff --git a/apps/web/app/app.dub.co/(auth)/register/page.tsx b/apps/web/app/app.dub.co/(auth)/register/page.tsxindex b63ffb7b59..4354d6f38d 100644--- a/apps/web/app/app.dub.co/(auth)/register/page.tsx+++ b/apps/web/app/app.dub.co/(auth)/register/page.tsx@@ -2,6 +2,7 @@ import { BlurImage, Button, Logo } from "@dub/ui";import { HOME_DOMAIN, constructMetadata } from "@dub/utils";import { Suspense } from "react";import RegisterForm from "./form";+import { SignUpWithEmailPassword } from "./signup-email-password";export const metadata = constructMetadata({title: `Create your ${process.env.NEXT_PUBLIC_APP_NAME} account`,@@ -22,6 +23,8 @@ const logos = ["cal",];+export const runtime = "nodejs";+export default function RegisterPage() {return (<div className="grid w-full grid-cols-1 md:grid-cols-5">@@ -53,17 +56,29 @@ export default function RegisterPage() {</a></p></div>- <div className="flex flex-col space-y-3 bg-gray-50 px-4 py-8 sm:px-16">- <Suspense- fallback={- <>- <Button disabled={true} text="" variant="secondary" />- <div className="mx-auto h-5 w-3/4 rounded-lg bg-gray-100" />- </>- }- >- <RegisterForm />- </Suspense>+ <div className="flex flex-col gap-5 bg-gray-50 px-4 py-8 sm:px-16">+ <SignUpWithEmailPassword />++ <div className="relative flex items-center">+ <div className="border-subtle flex-grow border-t" />+ <span className="text-subtle leadning-none mx-2 flex-shrink text-sm font-normal text-gray-700">+ Or continue with+ </span>+ <div className="border-subtle flex-grow border-t" />+ </div>++ <div className="space-y-3">+ <Suspense+ fallback={+ <>+ <Button disabled={true} text="" variant="secondary" />+ <div className="mx-auto h-5 w-3/4 rounded-lg bg-gray-100" />+ </>+ }+ >+ <RegisterForm />+ </Suspense>+ </div></div></div></div>diff --git a/apps/web/app/app.dub.co/(auth)/register/signup-email-password.tsx b/apps/web/app/app.dub.co/(auth)/register/signup-email-password.tsxnew file mode 100644index 0000000000..e121b05b2b--- /dev/null+++ b/apps/web/app/app.dub.co/(auth)/register/signup-email-password.tsx@@ -0,0 +1,85 @@+"use client";++import { createNewAccount } from "@/lib/actions/create-new-account";+import z from "@/lib/zod";+import { signUpSchema } from "@/lib/zod/schemas/auth";+import { Button, Input } from "@dub/ui";+import { zodResolver } from "@hookform/resolvers/zod";+import { useAction } from "next-safe-action/hooks";+import { useRouter } from "next/navigation";+import { useForm } from "react-hook-form";+import { toast } from "sonner";++export const SignUpWithEmailPassword = () => {+ const router = useRouter();+ const { executeAsync, result, status, isExecuting } =+ useAction(createNewAccount);++ const {+ register,+ handleSubmit,+ reset,+ formState: { errors, isDirty },+ } = useForm<z.infer<typeof signUpSchema>>({+ // resolver: zodResolver(signUpSchema),+ defaultValues: {+ email: "[email protected]",+ password: "1234aB#",+ },+ });++ const onSubmit = handleSubmit(async (data) => {+ await executeAsync(data);++ if (status === "hasSucceeded") {+ toast.success("account created");+ reset();+ }+ });++ return (+ <>+ {result.serverError && (+ <div className="rounded-md bg-red-100 p-3 text-red-900 dark:bg-red-900 dark:text-red-200">+ <div className="relative flex md:flex-row">+ <div className="flex flex-grow flex-col sm:flex-row">+ <div className="ltr:ml-3 rtl:mr-3">+ <h3 className="text-sm font-medium">+ {result.serverError.serverError}+ </h3>+ </div>+ </div>+ </div>+ </div>+ )}++ <form onSubmit={onSubmit}>+ <div className="flex flex-col space-y-4">+ <Input+ type="email"+ placeholder="Email address"+ autoComplete="email"+ required+ {...register("email")}+ error={result.validationErrors?.email?.[0] || errors.email?.message}+ />+ <Input+ type="password"+ placeholder="Password"+ required+ {...register("password")}+ error={+ result.validationErrors?.password?.[0] || errors.password?.message+ }+ />+ <Button+ text={status === "executing" ? "Signing Up..." : "Sign Up"}+ type="submit"+ loading={isExecuting}+ disabled={!isDirty}+ />+ </div>+ </form>+ </>+ );+};diff --git a/apps/web/lib/actions/create-new-account.ts b/apps/web/lib/actions/create-new-account.tsnew file mode 100644index 0000000000..44be346bd7--- /dev/null+++ b/apps/web/lib/actions/create-new-account.ts@@ -0,0 +1,50 @@+"use server";++import { prisma } from "@/lib/prisma";+import { flattenValidationErrors } from "next-safe-action";+import { hashPassword } from "../auth/password";+import { signUpSchema } from "../zod/schemas/auth";+import { actionClient } from "./safe-action";++// Sign up a new user using email and password+export const createNewAccount = actionClient+ .schema(signUpSchema, {+ handleValidationErrorsShape: (ve) =>+ flattenValidationErrors(ve).fieldErrors,+ })+ .action(async ({ parsedInput }) => {+ const { email, password } = parsedInput;++ // Check user with email exists+ const user = await prisma.user.findUnique({+ where: {+ email,+ },+ });++ if (user) {+ throw new Error("An user with this email already exists.");+ }++ // Create an account+ const newUser = await prisma.user.create({+ data: {+ email,+ passwordHash: await hashPassword(password),+ },+ });++ if (!newUser) {+ throw new Error("Failed to create an account. Please try again.");+ }++ // TODO:+ // Send verification email++ return {+ user: {+ id: newUser.id,+ email: newUser.email,+ },+ };+ });diff --git a/apps/web/lib/zod/schemas/auth.ts b/apps/web/lib/zod/schemas/auth.tsindex 989e4e5673..6fe48e2fe6 100644--- a/apps/web/lib/zod/schemas/auth.ts+++ b/apps/web/lib/zod/schemas/auth.ts@@ -33,3 +33,8 @@ export const signInSchema = z.object({email: z.string().email().min(1),password: z.string().min(1),});++export const signUpSchema = z.object({+ email: z.string().email(),+ password: passwordSchema,+});diff --git a/packages/ui/src/input.tsx b/packages/ui/src/input.tsxindex 2b1c2adbb8..1e3fabdd33 100644--- a/packages/ui/src/input.tsx+++ b/packages/ui/src/input.tsx@@ -48,7 +48,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>({props.error && (<span- className="block text-sm text-red-500"+ className="block text-sm text-red-500 mt-2"role="alert"aria-live="assertive">