From 67527c2f52e76725ad78719d4b0307e702bd0da1 Mon Sep 17 00:00:00 2001 From: schererleander Date: Fri, 26 Dec 2025 16:24:36 +0100 Subject: feat(2fa): implement google authenticator 2fa - add otplib and qrcode dependencies - update user model with 2fa fields - add twoFactorCode to validation schema - implement api routes for setup, enable, disable - add 2fa verification in auth flow - add 2fa management ui in settings - implement 2fa challenge in login page --- src/app/login/page.tsx | 176 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 114 insertions(+), 62 deletions(-) (limited to 'src/app/login/page.tsx') diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index ae89e63..c8a80f1 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -22,11 +22,14 @@ export default function SignInPage() { const [error, setError] = useState("") const router = useRouter() + const [showTwoFactor, setShowTwoFactor] = useState(false) + const form = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "", + twoFactorCode: "", }, }) @@ -38,11 +41,17 @@ export default function SignInPage() { const result = await signIn("credentials", { email: data.email, password: data.password, + twoFactorCode: data.twoFactorCode, redirect: false, }) if (result?.error) { - setError("Invalid email or password") + if (result.error === "2FA_REQUIRED") { + setShowTwoFactor(true) + // Don't clear password here so user can just enter code + } else { + setError(result.error) + } } else if (result?.ok) { router.push("/") router.refresh() @@ -62,7 +71,9 @@ export default function SignInPage() { Sign In - Enter your email and password to access your account + {showTwoFactor + ? "Enter the code from your authenticator app" + : "Enter your email and password to access your account"} @@ -74,76 +85,117 @@ export default function SignInPage() {
- ( - - Email - - - - - - )} - /> - ( - - Password - -
+ {!showTwoFactor ? ( + <> + ( + + Email + + + + + + )} + /> + ( + + Password + +
+ + +
+
+ +
+ )} + /> + + ) : ( + ( + + Two-Factor Code + - -
-
- -
- )} - /> + + + + )} + /> + )} + -
- Don't have an account? - - Sign up - -
+ {!showTwoFactor && ( + <> +
+ Don't have an account? + + Sign up + +
-
- -
+
+ +
+ + )} + + {showTwoFactor && ( +
+ +
+ )}
-- cgit v1.3.1