import { useState, useEffect } from "react"; import { userService } from "../api/userService"; import { oidcService } from "../api/oidcService"; import catIcon from "../assets/cat.png"; type LoginScreenProps = { setAuthenticated: (isAuth: boolean) => void; }; export const LoginScreen = ({ setAuthenticated }: LoginScreenProps) => { const [error, setError] = useState(""); const [isChecking, setIsChecking] = useState(true); const [isLoggingIn, setIsLoggingIn] = useState(false); useEffect(() => { const initAuth = async () => { // First, check for OIDC callback parameters const callbackParams = oidcService.getCallbackParamsFromURL(); if (callbackParams) { // Handle OIDC callback try { setIsLoggingIn(true); const result = await oidcService.handleCallback( callbackParams.code, callbackParams.state ); // Store tokens localStorage.setItem("access_token", result.access_token); localStorage.setItem("refresh_token", result.refresh_token); // Clear URL parameters oidcService.clearCallbackParams(); setAuthenticated(true); setIsChecking(false); return; } catch (err) { console.error("OIDC callback error:", err); setError("Login failed. Please try again."); oidcService.clearCallbackParams(); setIsLoggingIn(false); setIsChecking(false); return; } } // Check if user is already authenticated const isValid = await userService.validateToken(); if (isValid) { setAuthenticated(true); } setIsChecking(false); }; initAuth(); }, [setAuthenticated]); const handleOIDCLogin = async () => { try { setIsLoggingIn(true); setError(""); // Get authorization URL from backend const authUrl = await oidcService.initiateLogin(); // Redirect to Authelia window.location.href = authUrl; } catch (err) { setError("Failed to initiate login. Please try again."); console.error("OIDC login error:", err); setIsLoggingIn(false); } }; // Show loading state while checking authentication or processing callback if (isChecking || isLoggingIn) { return (
Simba

{isLoggingIn ? "letting you in..." : "checking credentials..."}

); } return (
{/* Decorative background texture */}
{/* Cat icon & branding */}
Simba

asksimba

your feline knowledge companion

{/* Login card */}
{error && (
{error}
)}

Sign in to start chatting with Simba

{/* Footer paw prints */}

~ meow ~

); };