Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix password. again.

Steven Tey4a0044f656ee
diff --git a/apps/web/app/password/[domain]/[key]/action.ts b/apps/web/app/password/[domain]/[key]/action.ts
index 1cb939aa18..7915645d65 100644
--- a/apps/web/app/password/[domain]/[key]/action.ts
+++ b/apps/web/app/password/[domain]/[key]/action.ts
@@ -1,7 +1,7 @@
"use server";
import { getLinkViaEdge } from "@/lib/planetscale";
-import { redirect } from "next/navigation";
+import { linkConstructor } from "@dub/utils";
export async function verifyPassword(_prevState: any, data: FormData) {
const domain = data.get("domain") as string;
@@ -19,12 +19,20 @@ export async function verifyPassword(_prevState: any, data: FormData) {
if (validPassword) {
// if the password is valid, redirect to the link with the password in the query string
- await redirectHelper(domain, key, password);
+ return {
+ redirect: `${linkConstructor({
+ domain,
+ key: rawKey,
+ })}?pw=${password}`,
+ };
+ // TODO: Fix this once this actually works: https://nextjs.org/docs/app/api-reference/functions/redirect#client-component
+ // redirect(
+ // `${linkConstructor({
+ // domain,
+ // key: rawKey,
+ // })}?pw=${password}`,
+ // );
} else {
return { error: "Invalid password" };
}
}
-
-async function redirectHelper(domain: string, key: string, password: string) {
- redirect(`https://${domain}/${key}?pw=${password}`);
-}
diff --git a/apps/web/app/password/[domain]/[key]/form.tsx b/apps/web/app/password/[domain]/[key]/form.tsx
index 4d1ec0b692..e104cf64b4 100644
--- a/apps/web/app/password/[domain]/[key]/form.tsx
+++ b/apps/web/app/password/[domain]/[key]/form.tsx
@@ -2,11 +2,13 @@
import { AlertCircleFill } from "@/ui/shared/icons";
import { Button, useMediaQuery } from "@dub/ui";
-import { useParams } from "next/navigation";
+import { useParams, useRouter } from "next/navigation";
+import { useEffect } from "react";
import { useFormState, useFormStatus } from "react-dom";
import { verifyPassword } from "./action";
const initialState = {
+ redirect: null,
error: null,
};
@@ -17,6 +19,14 @@ export default function PasswordForm() {
};
const [state, formAction] = useFormState(verifyPassword, initialState);
+ const router = useRouter();
+
+ useEffect(() => {
+ if (state.redirect) {
+ router.push(state.redirect);
+ }
+ }, [state]);
+
const { isMobile } = useMediaQuery();
return (
@@ -39,12 +49,12 @@ export default function PasswordForm() {
autoFocus={!isMobile}
required
className={`${
- state?.error
+ state.error
? "border-red-300 pr-10 text-red-500 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
: "border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"
} block w-full rounded-md focus:outline-none sm:text-sm`}
/>
- {state?.error && (
+ {state.error && (
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">
<AlertCircleFill
className="h-5 w-5 text-red-500"
@@ -53,7 +63,7 @@ export default function PasswordForm() {
</div>
)}
</div>
- {state?.error && (
+ {state.error && (
<p className="mt-2 text-sm text-red-600" id="slug-error">
Incorrect password
</p>