Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Implement Turnstile captcha verification in login and reset password pages

Bill Yang3b575a17ace2
diff --git a/client/src/app/login/page.tsx b/client/src/app/login/page.tsx
index d580b1dcc..95dc44b94 100644
--- a/client/src/app/login/page.tsx
+++ b/client/src/app/login/page.tsx
@@ -9,10 +9,12 @@ import { useSetPageTitle } from "../../hooks/useSetPageTitle";
import { authClient } from "../../lib/auth";
import { userStore } from "../../lib/userStore";
import { useConfigs } from "../../lib/configs";
+import { IS_CLOUD } from "../../lib/const";
import { AuthInput } from "@/components/auth/AuthInput";
import { AuthButton } from "@/components/auth/AuthButton";
import { AuthError } from "@/components/auth/AuthError";
import { SocialButtons } from "@/components/auth/SocialButtons";
+import { Turnstile } from "@/components/auth/Turnstile";
export default function Page() {
const { configs, isLoading: isLoadingConfigs } = useConfigs();
@@ -21,6 +23,7 @@ export default function Page() {
const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string>();
+ const [turnstileToken, setTurnstileToken] = useState<string>("");
const router = useRouter();
const handleSubmit = async (e: React.FormEvent) => {
@@ -28,11 +31,28 @@ export default function Page() {
setIsLoading(true);
setError("");
+
+ // Validate Turnstile token if in cloud mode
+ if (IS_CLOUD && !turnstileToken) {
+ setError("Please complete the captcha verification");
+ setIsLoading(false);
+ return;
+ }
+
try {
- const { data, error } = await authClient.signIn.email({
- email,
- password,
- });
+ const { data, error } = await authClient.signIn.email(
+ {
+ email,
+ password,
+ },
+ {
+ onRequest: context => {
+ if (IS_CLOUD && turnstileToken) {
+ context.headers.set("x-captcha-response", turnstileToken);
+ }
+ },
+ }
+ );
if (data?.user) {
userStore.setState({
user: data.user,
@@ -85,7 +105,20 @@ export default function Page() {
}
/>
- <AuthButton isLoading={isLoading} loadingText="Logging in...">
+ {IS_CLOUD && (
+ <Turnstile
+ onSuccess={token => setTurnstileToken(token)}
+ onError={() => setTurnstileToken("")}
+ onExpire={() => setTurnstileToken("")}
+ className="flex justify-center"
+ />
+ )}
+
+ <AuthButton
+ isLoading={isLoading}
+ loadingText="Logging in..."
+ disabled={IS_CLOUD ? !turnstileToken || isLoading : isLoading}
+ >
Login
</AuthButton>
diff --git a/client/src/app/reset-password/page.tsx b/client/src/app/reset-password/page.tsx
index 2115619cd..7874a2dd6 100644
--- a/client/src/app/reset-password/page.tsx
+++ b/client/src/app/reset-password/page.tsx
@@ -7,9 +7,11 @@ import { useRouter } from "next/navigation";
import { useState } from "react";
import { useSetPageTitle } from "../../hooks/useSetPageTitle";
import { authClient } from "../../lib/auth";
+import { IS_CLOUD } from "../../lib/const";
import { AuthInput } from "@/components/auth/AuthInput";
import { AuthButton } from "@/components/auth/AuthButton";
import { AuthError } from "@/components/auth/AuthError";
+import { Turnstile } from "@/components/auth/Turnstile";
export default function ResetPasswordPage() {
useSetPageTitle("Rybbit · Reset Password");
@@ -20,6 +22,7 @@ export default function ResetPasswordPage() {
const [otp, setOtp] = useState("");
const [newPassword, setNewPassword] = useState("");
const [resetSuccess, setResetSuccess] = useState(false);
+ const [turnstileToken, setTurnstileToken] = useState<string>("");
const router = useRouter();
const handleRequestOTP = async (e: React.FormEvent) => {
@@ -27,11 +30,27 @@ export default function ResetPasswordPage() {
setIsLoading(true);
setError("");
+ // Validate Turnstile token if in cloud mode
+ if (IS_CLOUD && !turnstileToken) {
+ setError("Please complete the captcha verification");
+ setIsLoading(false);
+ return;
+ }
+
try {
- const { data, error } = await authClient.emailOtp.sendVerificationOtp({
- email,
- type: "forget-password",
- });
+ const { data, error } = await authClient.emailOtp.sendVerificationOtp(
+ {
+ email,
+ type: "forget-password",
+ },
+ {
+ onRequest: context => {
+ if (IS_CLOUD && turnstileToken) {
+ context.headers.set("x-captcha-response", turnstileToken);
+ }
+ },
+ }
+ );
if (error) {
setError(error.message);
@@ -162,7 +181,20 @@ export default function ResetPasswordPage() {
onChange={e => setEmail(e.target.value)}
/>
- <AuthButton isLoading={isLoading} loadingText="Sending code...">
+ {IS_CLOUD && (
+ <Turnstile
+ onSuccess={token => setTurnstileToken(token)}
+ onError={() => setTurnstileToken("")}
+ onExpire={() => setTurnstileToken("")}
+ className="flex justify-center"
+ />
+ )}
+
+ <AuthButton
+ isLoading={isLoading}
+ loadingText="Sending code..."
+ disabled={IS_CLOUD ? !turnstileToken || isLoading : isLoading}
+ >
Send Verification Code
</AuthButton>