The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add password reset form
Kiran K74819f8ec800
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.tsindex f41df1af4a..4da4e24804 100644--- a/apps/web/app/api/user/route.ts+++ b/apps/web/app/api/user/route.ts@@ -9,11 +9,22 @@ import { z } from "zod";// GET /api/user – get a specific userexport const GET = withSession(async ({ session }) => {- const user = await prisma.user.findUnique({- where: {- id: session.user.id,- },- });+ const [user, account] = await Promise.all([+ prisma.user.findUnique({+ where: {+ id: session.user.id,+ },+ }),++ prisma.account.findFirst({+ where: {+ userId: session.user.id,+ },+ select: {+ provider: true,+ },+ }),+ ]);const migratedWorkspace = await redis.hget("migrated_links_users",@@ -27,6 +38,7 @@ export const GET = withSession(async ({ session }) => {return NextResponse.json({...user,migratedWorkspace,+ provider: account?.provider,});});diff --git a/apps/web/app/api/user/set-password/route.ts b/apps/web/app/api/user/set-password/route.tsindex 7a2d56a56b..a33a11f00f 100644--- a/apps/web/app/api/user/set-password/route.ts+++ b/apps/web/app/api/user/set-password/route.ts@@ -36,6 +36,8 @@ export const POST = withSession(async ({ session }) => {},});+ console.log(`${process.env.NEXTAUTH_URL}/auth/reset-password/${token}`);+// Send email with password reset linkawait sendEmail({subject: `${process.env.NEXT_PUBLIC_APP_NAME}: Password reset instructions`,@@ -46,5 +48,5 @@ export const POST = withSession(async ({ session }) => {}),});- return NextResponse.json({}, { status: 204 });+ return NextResponse.json({});});diff --git a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsxnew file mode 100644index 0000000000..c21ab67f05--- /dev/null+++ b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx@@ -0,0 +1,74 @@+"use client";++import { Button } from "@dub/ui";+import { useSearchParams } from "next/navigation";+import { useForm } from "react-hook-form";++type FormData = {+ password: string;+ confirmPassword: string;+};++export const ResetPasswordForm = () => {+ const searchParams = useSearchParams();++ const {+ register,+ handleSubmit,+ formState: { isLoading, disabled, errors },+ } = useForm<FormData>();++ const onSubmit = async (data) => {+ console.log(data);+ };++ console.log(errors);++ return (+ <>+ <form+ className="flex flex-col space-y-3"+ onSubmit={handleSubmit(onSubmit)}+ >+ <input+ placeholder="********"+ type="password"+ {...register("password", { required: true, minLength: 8 })}+ aria-invalid={errors.password ? "true" : "false"}+ className="w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"+ />+ <input+ placeholder="********"+ type="password"+ {...register("confirmPassword", { required: true, minLength: 8 })}+ className="w-full max-w-md rounded-md border border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"+ />+ <Button+ text="Reset Password"+ type="submit"+ loading={isLoading}+ disabled={disabled}+ />+ </form>+ {/* {noSuchAccount ? (+ <p className="text-center text-sm text-red-500">+ No such account.{" "}+ <Link href="/register" className="font-semibold text-red-600">+ Sign up+ </Link>{" "}+ instead?+ </p>+ ) : (+ <p className="text-center text-sm text-gray-500">+ Don't have an account?{" "}+ <Link+ href="/register"+ className="font-semibold text-gray-500 transition-colors hover:text-black"+ >+ Sign up+ </Link>+ </p>+ )} */}+ </>+ );+};diff --git a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsx b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsxnew file mode 100644index 0000000000..802beaa858--- /dev/null+++ b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/page.tsx@@ -0,0 +1,22 @@+import { Logo } from "@dub/ui";+import { HOME_DOMAIN } from "@dub/utils";+import { ResetPasswordForm } from "./form";++export default function ResetPasswordPage() {+ return (+ <div className="relative z-10 mt-[calc(30vh)] h-fit w-full max-w-md overflow-hidden border-y border-gray-200 sm:rounded-2xl sm:border sm:shadow-xl">+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 bg-white px-4 py-6 pt-8 text-center sm:px-16">+ <a href={HOME_DOMAIN}>+ <Logo className="h-10 w-10" />+ </a>+ <h3 className="text-xl font-semibold">Reset your password</h3>+ <p className="text-sm text-gray-500">+ Enter new password for your account.+ </p>+ </div>+ <div className="flex flex-col space-y-3 bg-gray-50 px-4 py-8 sm:px-16">+ <ResetPasswordForm />+ </div>+ </div>+ );+}diff --git a/apps/web/app/app.dub.co/(auth)/auth/reset-password/page.tsx b/apps/web/app/app.dub.co/(auth)/auth/reset-password/page.tsxdeleted file mode 100644index e69de29bb2..0000000000diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/layout.tsxindex 05c6cb234a..20f940f9d8 100644--- a/apps/web/app/app.dub.co/(dashboard)/settings/layout.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/settings/layout.tsx@@ -1,5 +1,5 @@import SettingsLayout from "@/ui/layout/settings-layout";-import { Gear2, Key } from "@dub/ui";+import { Gear2, Key, ShieldCheck } from "@dub/ui";import { ReactNode } from "react";export default function PersonalSettingsLayout({@@ -13,6 +13,11 @@ export default function PersonalSettingsLayout({icon: Gear2,segment: null,},+ {+ name: "Security",+ icon: ShieldCheck,+ segment: "security",+ },{name: "API Keys",icon: Key,diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/security/page.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/security/page.tsxnew file mode 100644index 0000000000..9a2058f8e8--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/page.tsx@@ -0,0 +1,9 @@+import { SetPassword } from "./password";++export default function SecurityPage() {+ return (+ <>+ <SetPassword />+ </>+ );+}diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/security/password.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/security/password.tsxnew file mode 100644index 0000000000..0fa26a0ada--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/password.tsx@@ -0,0 +1,87 @@+"use client";++import { Button } from "@dub/ui";+import { useSession } from "next-auth/react";+import { useState } from "react";+import { toast } from "sonner";++export const SetPassword = () => {+ const [sending, setSending] = useState(false);+ const { data: session, status } = useSession();++ // Send an email to the user with instructions to set their password+ const sendPasswordSetRequest = async () => {+ try {+ setSending(true);++ const response = await fetch("/api/user/set-password", {+ method: "POST",+ });++ if (response.ok) {+ toast.success(+ `We've sent you an email to ${session?.user?.email} with instructions to set your password`,+ );+ return;+ }++ const { error } = await response.json();+ throw new Error(error.message);+ } catch (error) {+ toast.error(error.message);+ } finally {+ setSending(false);+ }+ };++ return (+ <div className="rounded-lg border border-gray-200 bg-white">+ <div className="flex flex-col space-y-3 p-5 sm:p-10">+ <h2 className="text-xl font-medium">Password</h2>+ <p className="text-sm text-gray-500">Manage your account password.</p>+ <div className="mt-1 max-w-md">+ <div>+ <Button+ text="Create account password"+ onClick={sendPasswordSetRequest}+ loading={sending}+ disabled={sending}+ />+ </div>+ </div>+ </div>+ </div>+ );++ // return (+ // <Form+ // title="Password"+ // description="Manage your account password."+ // inputAttrs={{+ // name: "password",+ // defaultValue:+ // status === "loading" ? undefined : session?.user?.name || "",+ // placeholder: "Steve Jobs",+ // maxLength: 32,+ // }}+ // helpText="Max 32 characters."+ // handleSubmit={(data) =>+ // fetch("/api/user", {+ // method: "PATCH",+ // headers: {+ // "Content-Type": "application/json",+ // },+ // body: JSON.stringify(data),+ // }).then(async (res) => {+ // if (res.status === 200) {+ // update();+ // toast.success("Successfully updated your name!");+ // } else {+ // const { error } = await res.json();+ // toast.error(error.message);+ // }+ // })+ // }+ // />+ // );+};