Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- useEffect(() => {
- const checkAuthentication = async () => {
- setAuthChecking(true);
- try {
- const hasAuthCookie = document.cookie.includes('authToken=');
- if (hasAuthCookie) {
- setIsAuthenticated(true);
- setAuthChecking(false);
- return;
- }
- if (telegramLoading) {
- console.log('Waiting for Telegram user data to load...');
- return;
- }
- if (telegramError) {
- console.error('Telegram authentication error:', telegramError);
- setAuthChecking(false);
- return;
- }
- if (!telegramUser) {
- console.error('No Telegram user data available');
- setAuthChecking(false);
- return;
- }
- const response = await sendTelegramDataToServer(
- `${process.env.NEXT_PUBLIC_API_URL}/api/auth/telegram`
- );
- if (response?.jwt) {
- const jwt = response.jwt;
- const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
- document.cookie = `authToken=${jwt}; expires=${expiryDate.toUTCString()}; path=/; SameSite=Strict`;
- localStorage.setItem('authToken', jwt);
- setIsAuthenticated(true);
- }
- } catch (error) {
- console.error('Authentication error:', error);
- } finally {
- setAuthChecking(false);
- }
- };
- checkAuthentication();
- }, [telegramLoading, telegramError, telegramUser]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement