add yandex button
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
"use client";
|
||||
|
||||
import Script from 'next/script';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
YaAuthSuggest: any;
|
||||
}
|
||||
}
|
||||
|
||||
export default function YandexAuthWithScript() {
|
||||
const buttonRef = useRef<HTMLDivElement>(null);
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
|
||||
const handleLogin = (token: string) => {
|
||||
console.log('handleLogin');
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!isReady || !buttonRef.current || !window.YaAuthSuggest) return;
|
||||
|
||||
window.YaAuthSuggest.init(
|
||||
{
|
||||
client_id: 'b356a7ceac0f4c50a3b434220f86bce0',
|
||||
response_type: 'token',
|
||||
redirect_uri: `http://localhost:2999/ru/login`,
|
||||
},
|
||||
'http://localhost:2999',
|
||||
{
|
||||
parentId: buttonRef.current.id,
|
||||
view: 'button',
|
||||
buttonView: 'main',
|
||||
buttonTheme: 'light',
|
||||
buttonSize: 'm',
|
||||
buttonBorderRadius: 10
|
||||
}
|
||||
)
|
||||
.then((result: any) => {
|
||||
console.log('then');
|
||||
console.log(result);
|
||||
if (result?.access_token) {
|
||||
handleLogin(result.access_token);
|
||||
}
|
||||
})
|
||||
.catch((error: any) => {
|
||||
console.error('Yandex auth error:', error);
|
||||
});
|
||||
}, [isReady]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Script
|
||||
src="https://yastatic.net/s3/passport-sdk/autofill/v1/sdk-suggest-with-polyfills-latest.js"
|
||||
strategy="lazyOnload"
|
||||
onLoad={() => setIsReady(true)}
|
||||
onError={() => console.error('Failed to load Yandex script')}
|
||||
/>
|
||||
|
||||
<div
|
||||
ref={buttonRef}
|
||||
id="yandex-auth-container"
|
||||
className="flex justify-center"
|
||||
>
|
||||
{!isReady && <div>Загрузка Яндекс авторизации...</div>}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user