Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Handle pasted OTP, verify session, update callback

Richie McIlroy54f351cfbce7
diff --git a/apps/web/app/(org)/verify-otp/form.tsx b/apps/web/app/(org)/verify-otp/form.tsx
index 4ecdc0c02..d152273c0 100644
--- a/apps/web/app/(org)/verify-otp/form.tsx
+++ b/apps/web/app/(org)/verify-otp/form.tsx
@@ -49,7 +49,7 @@ export function VerifyOTPForm({
inputRefs.current[5]?.focus();
}
- if (index + value.length >= 5) handleVerify.mutate();
+ if (newCode.every((d) => d)) handleVerify.mutate(newCode.join(""));
} else {
const newCode = [...code];
newCode[index] = value;
@@ -73,15 +73,17 @@ export function VerifyOTPForm({
const normalizedEmail = email.toLowerCase();
const handleVerify = useMutation({
- mutationFn: async () => {
- const otpCode = code.join("");
+ mutationFn: async (pastedCode?: string) => {
+ const otpCode = pastedCode ?? code.join("");
if (otpCode.length !== 6) throw "Please enter a complete 6-digit code";
- const res = await fetch(
- `/api/auth/callback/email?email=${encodeURIComponent(normalizedEmail)}&token=${encodeURIComponent(otpCode)}&callbackUrl=${encodeURIComponent("/login-success")}`,
+ await fetch(
+ `/api/auth/callback/email?email=${encodeURIComponent(normalizedEmail)}&token=${encodeURIComponent(otpCode)}&callbackUrl=${encodeURIComponent(next || "/dashboard")}`,
);
- if (!res.url.includes("/login-success")) {
+ const sessionRes = await fetch("/api/auth/session");
+ const session = await sessionRes.json();
+ if (!session?.user) {
setCode(["", "", "", "", "", ""]);
inputRefs.current[0]?.focus();
throw "Invalid code. Please try again.";
@@ -202,7 +204,7 @@ export function VerifyOTPForm({
variant="primary"
className="w-full"
spinner={isVerifying}
- onClick={() => handleVerify.mutate()}
+ onClick={() => handleVerify.mutate(code.join(""))}
disabled={code.some((digit) => !digit) || isVerifying}
>
{isVerifying ? "Verifying..." : "Verify Code"}
diff --git a/apps/web/app/s/[videoId]/_components/OtpForm.tsx b/apps/web/app/s/[videoId]/_components/OtpForm.tsx
index ca359bd53..093f266e8 100644
--- a/apps/web/app/s/[videoId]/_components/OtpForm.tsx
+++ b/apps/web/app/s/[videoId]/_components/OtpForm.tsx
@@ -49,7 +49,7 @@ const OtpForm = ({
inputRefs.current[5]?.focus();
}
- if (index + value.length >= 5) handleVerify.mutate(undefined);
+ if (newCode.every((d) => d)) handleVerify.mutate(newCode.join(""));
} else {
const newCode = [...code];
newCode[index] = value;
@@ -73,15 +73,17 @@ const OtpForm = ({
const normalizedEmail = email.toLowerCase();
const handleVerify = useMutation({
- mutationFn: async () => {
- const otpCode = code.join("");
+ mutationFn: async (pastedCode?: string) => {
+ const otpCode = pastedCode ?? code.join("");
if (otpCode.length !== 6) throw "Please enter a complete 6-digit code";
- const res = await fetch(
- `/api/auth/callback/email?email=${encodeURIComponent(normalizedEmail)}&token=${encodeURIComponent(otpCode)}&callbackUrl=${encodeURIComponent("/login-success")}`,
+ await fetch(
+ `/api/auth/callback/email?email=${encodeURIComponent(normalizedEmail)}&token=${encodeURIComponent(otpCode)}&callbackUrl=${encodeURIComponent("/dashboard")}`,
);
- if (!res.url.includes("/login-success")) {
+ const sessionRes = await fetch("/api/auth/session");
+ const session = await sessionRes.json();
+ if (!session?.user) {
setCode(["", "", "", "", "", ""]);
inputRefs.current[0]?.focus();
throw "Invalid code. Please try again.";
@@ -177,7 +179,7 @@ const OtpForm = ({
className="w-full"
spinner={isVerifying}
onClick={() => {
- handleVerify.mutate(undefined);
+ handleVerify.mutate(code.join(""));
}}
disabled={code.some((digit) => !digit) || isVerifying}
>