22 lines
608 B
TypeScript
22 lines
608 B
TypeScript
'use client';
|
|
|
|
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 as any).grecaptcha) {
|
|
resolve(null);
|
|
return;
|
|
}
|
|
try {
|
|
(window as any).grecaptcha.ready(() => {
|
|
(window as any).grecaptcha
|
|
.execute(siteKey, { action })
|
|
.then((token: string) => resolve(token))
|
|
.catch(() => resolve(null));
|
|
});
|
|
} catch {
|
|
resolve(null);
|
|
}
|
|
});
|
|
}
|