Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/web/app/(auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ const SignInPage = () => {
event.preventDefault();
const form = new FormData(event.currentTarget);
setPending(true);
const { error } = await signIn.email({
const { data, error } = await signIn.email({
email: String(form.get("email")),
password: String(form.get("password")),
});
setPending(false);
if (error) {
setPending(false);
toast.error(error.message ?? "Sign in failed");
return;
}
// The two-factor client plugin is already sending us to /two-factor.
if (data && "twoFactorRedirect" in data && data.twoFactorRedirect) {
return;
}
setPending(false);
router.push(nextPath());
router.refresh();
};
Expand Down
131 changes: 131 additions & 0 deletions apps/web/app/(auth)/two-factor/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "sonner";

import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import {
InputOTP,
InputOTPGroup,
InputOTPSlot,
} from "@/components/ui/input-otp";
import { Label } from "@/components/ui/label";
import { authClient } from "@/lib/auth-client";

const nextPath = () => {
const next = new URLSearchParams(window.location.search).get("next");
return next?.startsWith("/") && !/^\/[/\\]/u.test(next) ? next : "/dashboard";
};

const TwoFactorPage = () => {
const router = useRouter();
const [useBackup, setUseBackup] = useState(false);
const [code, setCode] = useState("");
const [trustDevice, setTrustDevice] = useState(false);
const [pending, setPending] = useState(false);

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setPending(true);
const { error } = useBackup
? await authClient.twoFactor.verifyBackupCode({
code: code.trim(),
trustDevice,
})
: await authClient.twoFactor.verifyTotp({ code, trustDevice });
if (error) {
setPending(false);
setCode("");
toast.error(error.message ?? "Invalid code");
return;
}
router.push(nextPath());
router.refresh();
};

const toggleMode = () => {
setUseBackup((value) => !value);
setCode("");
};

return (
<div className="flex flex-col gap-6">
<div className="space-y-2">
<h1 className="text-3xl font-bold tracking-tight">
Two-factor authentication
</h1>
<p className="text-muted-foreground">
{useBackup
? "Enter one of your backup codes."
: "Enter the 6-digit code from your authenticator app."}
</p>
</div>
<form className="space-y-4" method="post" onSubmit={handleSubmit}>
{useBackup ? (
<div className="space-y-2">
<Label htmlFor="backup-code">Backup code</Label>
<Input
autoComplete="off"
autoFocus
disabled={pending}
id="backup-code"
onChange={(event) => setCode(event.target.value)}
required
value={code}
/>
</div>
) : (
<InputOTP
autoComplete="one-time-code"
autoFocus
disabled={pending}
maxLength={6}
onChange={setCode}
value={code}
>
<InputOTPGroup>
{Array.from({ length: 6 }, (_, index) => (
<InputOTPSlot index={index} key={index} />
))}
</InputOTPGroup>
</InputOTP>
)}
<div className="flex items-center gap-2">
<Checkbox
checked={trustDevice}
disabled={pending}
id="trust-device"
onCheckedChange={(checked) => setTrustDevice(checked === true)}
/>
<Label className="font-normal" htmlFor="trust-device">
Trust this device for 30 days
</Label>
</div>
<Button
className="h-11 w-full"
disabled={pending || (useBackup ? !code.trim() : code.length !== 6)}
type="submit"
>
{pending ? "Verifying…" : "Verify"}
</Button>
</form>
<div className="flex items-center justify-between text-sm">
<button
className="text-link hover:underline"
onClick={toggleMode}
type="button"
>
{useBackup ? "Use authenticator app" : "Use a backup code"}
</button>
<Link className="text-muted-foreground hover:underline" href="/sign-in">
Back to sign in
</Link>
</div>
</div>
);
};

export default TwoFactorPage;
4 changes: 2 additions & 2 deletions apps/web/app/dashboard/account/components/account-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
const TABS = [
{ href: "/dashboard/account", label: "Profile", value: "profile" },
{ href: "/dashboard/account/password", label: "Password", value: "password" },
{ href: "/dashboard/account/passkeys", label: "Passkeys", value: "passkeys" },
{ href: "/dashboard/account/security", label: "Security", value: "security" },
{ href: "/dashboard/account/billing", label: "Billing", value: "billing" },
] as const;

const SECURITY_TABS = new Set<string>(["password", "passkeys"]);
const SECURITY_TABS = new Set<string>(["password", "security"]);

export const AccountTabs = ({ showSecurity }: { showSecurity: boolean }) => {
const tabs = TABS.filter(
Expand Down
Loading