Advertisement
Wonkiest29

Untitled

May 7th, 2025
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   useEffect(() => {
  2.     const checkAuthentication = async () => {
  3.       setAuthChecking(true);
  4.  
  5.       try {
  6.         const hasAuthCookie = document.cookie.includes('authToken=');
  7.  
  8.         if (hasAuthCookie) {
  9.           setIsAuthenticated(true);
  10.           setAuthChecking(false);
  11.           return;
  12.         }
  13.  
  14.         if (telegramLoading) {
  15.           console.log('Waiting for Telegram user data to load...');
  16.           return;
  17.         }
  18.  
  19.         if (telegramError) {
  20.           console.error('Telegram authentication error:', telegramError);
  21.           setAuthChecking(false);
  22.           return;
  23.         }
  24.  
  25.         if (!telegramUser) {
  26.           console.error('No Telegram user data available');
  27.           setAuthChecking(false);
  28.           return;
  29.         }
  30.  
  31.         const response = await sendTelegramDataToServer(
  32.           `${process.env.NEXT_PUBLIC_API_URL}/api/auth/telegram`
  33.         );
  34.  
  35.         if (response?.jwt) {
  36.           const jwt = response.jwt;
  37.           const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
  38.           document.cookie = `authToken=${jwt}; expires=${expiryDate.toUTCString()}; path=/; SameSite=Strict`;
  39.           localStorage.setItem('authToken', jwt);
  40.           setIsAuthenticated(true);
  41.         }
  42.       } catch (error) {
  43.         console.error('Authentication error:', error);
  44.       } finally {
  45.         setAuthChecking(false);
  46.       }
  47.     };
  48.  
  49.     checkAuthentication();
  50.   }, [telegramLoading, telegramError, telegramUser]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement