31 lines
772 B
TypeScript
31 lines
772 B
TypeScript
'use client';
|
|
|
|
declare global {
|
|
interface Window {
|
|
grecaptcha: {
|
|
ready: (cb: () => void) => void;
|
|
execute: (siteKey: string, options: { action: string }) => Promise<string>;
|
|
};
|
|
}
|
|
}
|
|
|
|
export function getRecaptchaToken(action: string = 'submit'): Promise<string | null> {
|
|
return new Promise((resolve) => {
|
|
const siteKey = process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY;
|
|
if (typeof window === 'undefined' || !siteKey || !window.grecaptcha) {
|
|
resolve(null);
|
|
return;
|
|
}
|
|
try {
|
|
window.grecaptcha.ready(() => {
|
|
window.grecaptcha
|
|
.execute(siteKey, { action })
|
|
.then((token: string) => resolve(token))
|
|
.catch(() => resolve(null));
|
|
});
|
|
} catch {
|
|
resolve(null);
|
|
}
|
|
});
|
|
}
|