The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update password
Kiran Kd5d9b5bc8990
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.tsindex 4da4e24804..dc6461bbfc 100644--- a/apps/web/app/api/user/route.ts+++ b/apps/web/app/api/user/route.ts@@ -14,6 +14,17 @@ export const GET = withSession(async ({ session }) => {where: {id: session.user.id,},+ select: {+ id: true,+ name: true,+ email: true,+ image: true,+ subscribed: true,+ createdAt: true,+ defaultWorkspace: true,+ source: true,+ passwordHash: true,+ },}),prisma.account.findFirst({@@ -39,6 +50,7 @@ export const GET = withSession(async ({ session }) => {...user,migratedWorkspace,provider: account?.provider,+ hasPassword: user?.passwordHash !== null,});});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.tsxindex 9a2058f8e8..b10bf12d50 100644--- a/apps/web/app/app.dub.co/(dashboard)/settings/security/page.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/page.tsx@@ -1,9 +1,33 @@-import { SetPassword } from "./password";--export default function SecurityPage() {- return (- <>- <SetPassword />- </>- );+import { getSession } from "@/lib/auth";+import { prisma } from "@/lib/prisma";+import { RequestSetPassword } from "./request-set-password";+import { UpdatePassword } from "./update-password";++export const dynamic = "auto";++export default async function SecurityPage() {+ const user = await getUser();++ return <>{user.hasPassword ? <UpdatePassword /> : <RequestSetPassword />}</>;}++const getUser = async () => {+ const session = await getSession();++ if (!session) {+ throw new Error("Unauthorized");+ }++ const user = await prisma.user.findFirstOrThrow({+ where: {+ id: session.user.id,+ },+ select: {+ passwordHash: true,+ },+ });++ return {+ hasPassword: user.passwordHash !== null,+ };+};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.tsxdeleted file mode 100644index 33319cc051..0000000000--- a/apps/web/app/app.dub.co/(dashboard)/settings/security/password.tsx+++ /dev/null@@ -1,55 +0,0 @@-"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>- );-};diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/security/request-set-password.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/security/request-set-password.tsxnew file mode 100644index 0000000000..b8cc58e40a--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/request-set-password.tsx@@ -0,0 +1,66 @@+"use client";++import useUser from "@/lib/swr/use-user";+import { Button, LoadingSpinner } from "@dub/ui";+import { useState } from "react";+import { toast } from "sonner";++// Displayed when the user doesn't have a password set for their account+export const RequestSetPassword = () => {+ const { user, loading } = useUser();+ const [sending, setSending] = useState(false);++ // 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 ${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>+ {!loading ? (+ <>+ <p className="text-sm text-gray-500">+ Your account is managed by{" "}+ <span className="capitalize">{user?.provider}</span>. You can set+ a password to use with your Dub account.+ </p>+ <div className="flex">+ <div>+ <Button+ text="Create account password"+ onClick={sendPasswordSetRequest}+ loading={sending}+ disabled={sending}+ />+ </div>+ </div>+ </>+ ) : (+ <LoadingSpinner />+ )}+ </div>+ </div>+ );+};diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsxnew file mode 100644index 0000000000..f38c5225fd--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx@@ -0,0 +1,69 @@+"use client";++import useUser from "@/lib/swr/use-user";+import { Form } from "@dub/ui";+import { useState } from "react";+import { toast } from "sonner";++// Allow the user to update their existing password+export const UpdatePassword = () => {+ const { user, loading } = useUser();+ const [sending, setSending] = useState(false);++ // 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 ${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 (+ <Form+ title="Password"+ description="Manage your account password."+ inputAttrs={{+ name: "name",+ // defaultValue:+ // status === "loading" ? undefined : session?.user?.name || "",+ placeholder: "Steve Jobs",+ maxLength: 32,+ }}+ helpText="Password must be at least 8 characters long containing at least one number, one uppercase letter, and one lowercase letter."+ 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);+ }+ })+ }+ />+ );+};diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex d8130db1c1..7e513c5ecd 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -116,6 +116,8 @@ export interface UserProps {migratedWorkspace: string | null;defaultWorkspace?: string;isMachine: boolean;+ hasPassword: boolean;+ provider: string | null;}export interface WorkspaceUserProps extends UserProps {