Skip to content

feat: add refresh-expired and refresh-timeout attributes to Turnstile #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
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
2 changes: 1 addition & 1 deletion app/create/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe("CreateGistPage", () => {
render(<CreateGistPage />);

const pinInput = screen.getByPlaceholderText(
"Set a PIN to protect edits"
"Leave empty for no protection"
);
expect(pinInput).toBeInTheDocument();

Expand Down
10 changes: 1 addition & 9 deletions app/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ export default function CreateGistPage() {
setIsTurnstileReady(false);
}, []);

const handleTurnstileExpire = useCallback(() => {
setTurnstileToken(null);
setIsTurnstileReady(false);
setError(
"⏰ Security verification expired. Please refresh the page to continue."
);
}, []);

const handleFilesChange = useCallback((newFiles: FileData[]) => {
setFiles(newFiles);
// Don't clear errors on file change - let them persist
Expand Down Expand Up @@ -391,9 +383,9 @@ export default function CreateGistPage() {
<Turnstile
sitekey={turnstileSiteKey}
action="create_gist"
refreshExpired="auto"
onSuccess={handleTurnstileSuccess}
onError={handleTurnstileError}
onExpire={handleTurnstileExpire}
appearance="interaction-only"
/>
)}
Expand Down
15 changes: 15 additions & 0 deletions components/ui/turnstile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ interface TurnstileProps {
onSuccess?: (token: string) => void;
onExpire?: () => void;
onError?: () => void;
onTimeout?: () => void;
theme?: "light" | "dark" | "auto";
action?: string;
size?: "normal" | "flexible" | "compact";
appearance?: "always" | "execute" | "interaction-only";
execution?: "render" | "execute";
language?: string;
refreshExpired?: "auto" | "manual" | "never";
refreshTimeout?: "auto" | "manual" | "never";
}

declare global {
Expand All @@ -25,12 +28,15 @@ declare global {
callback?: (token: string) => void;
"error-callback"?: () => void;
"expired-callback"?: () => void;
"timeout-callback"?: () => void;
theme?: "light" | "dark" | "auto";
action?: string;
size?: "normal" | "flexible" | "compact";
appearance?: "always" | "execute" | "interaction-only";
execution?: "render" | "execute";
language?: string;
"refresh-expired"?: "auto" | "manual" | "never";
"refresh-timeout"?: "auto" | "manual" | "never";
}
) => string;
reset: (widgetId: string) => void;
Expand All @@ -46,12 +52,15 @@ const Turnstile: React.FC<TurnstileProps> = ({
onSuccess,
onExpire,
onError,
onTimeout,
theme = "auto",
action,
size = "normal",
appearance = "interaction-only",
execution = "render",
language = "auto",
refreshExpired = "auto",
refreshTimeout = "auto",
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const widgetIdRef = useRef<string | null>(null);
Expand All @@ -67,9 +76,12 @@ const Turnstile: React.FC<TurnstileProps> = ({
appearance,
execution,
language,
"refresh-expired": refreshExpired,
"refresh-timeout": refreshTimeout,
callback: onSuccess,
"error-callback": onError,
"expired-callback": onExpire,
"timeout-callback": onTimeout,
});
}
};
Expand Down Expand Up @@ -110,12 +122,15 @@ const Turnstile: React.FC<TurnstileProps> = ({
onSuccess,
onError,
onExpire,
onTimeout,
theme,
action,
size,
appearance,
execution,
language,
refreshExpired,
refreshTimeout,
]);

return <div ref={containerRef} />;
Expand Down