@using DiunaBI.UI.Shared.Services @using Microsoft.Extensions.Configuration @inject IJSRuntime JS @inject IConfiguration Configuration @inject AuthService AuthService @inject NavigationManager NavigationManager @inject IGoogleAuthService? GoogleAuthService Welcome to DiunaBI Sign in using your Google account @if (_isLoading) { Verifying... } else { Sign in with Google } @if (!string.IsNullOrEmpty(_errorMessage)) { @_errorMessage } @code { private bool _isLoading = false; private string _errorMessage = string.Empty; private static LoginCard? _instance; private bool _isInitialized = false; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { _instance = this; // Initialize JavaScript Google SDK for web (both null service and WebGoogleAuthService) // Skip only for mobile platform-specific auth (MobileGoogleAuthService) var isMobileAuth = GoogleAuthService != null && GoogleAuthService.GetType().Name == "MobileGoogleAuthService"; if (!isMobileAuth) { await InitializeGoogleSignIn(); } else { Console.WriteLine("📱 Using platform-specific Google auth service"); _isInitialized = true; } } } private async Task InitializeGoogleSignIn() { try { if (_isInitialized) return; var clientId = Configuration["GoogleAuth:ClientId"]; Console.WriteLine($"🔍 Reading GoogleAuth:ClientId from configuration: '{clientId}'"); if (string.IsNullOrEmpty(clientId)) { _errorMessage = "Google ClientId is not configured in appsettings."; Console.Error.WriteLine("❌ Google ClientId is NULL or EMPTY in configuration!"); return; } Console.WriteLine($"✅ Calling initGoogleSignIn with clientId: {clientId}"); await JS.InvokeVoidAsync("initGoogleSignIn", clientId); _isInitialized = true; } catch (Exception ex) { _errorMessage = "Google Sign-In initialization error."; Console.Error.WriteLine($"❌ Google Sign-In initialization error: {ex.Message}"); Console.Error.WriteLine($"Stack trace: {ex.StackTrace}"); } } private async Task HandleGoogleSignIn() { try { _isLoading = true; _errorMessage = string.Empty; StateHasChanged(); // Use platform-specific auth if available (mobile), otherwise use JavaScript (web) if (GoogleAuthService != null) { Console.WriteLine("📱 Using mobile Google auth"); var success = await GoogleAuthService.SignInAsync(); if (success) { Console.WriteLine("✅ Mobile auth successful, navigating..."); NavigationManager.NavigateTo("/dashboard", replace: true); } else { _errorMessage = "Login failed. Please try again"; _isLoading = false; StateHasChanged(); } } else { Console.WriteLine("🌐 Using web JavaScript Google auth"); await JS.InvokeVoidAsync("requestGoogleSignIn"); } } catch (Exception ex) { Console.Error.WriteLine($"❌ HandleGoogleSignIn error: {ex.Message}"); _errorMessage = "Login error. Please try again"; _isLoading = false; StateHasChanged(); } } [JSInvokable] public static async Task OnGoogleSignInSuccess(string googleCredential, string fullName, string email, string avatarUrl) { Console.WriteLine($"=== OnGoogleSignInSuccess: {email} ==="); if (_instance != null) { try { // Waliduj użytkownika w backendzie DiunaBI var (success, errorMessage) = await _instance.AuthService.ValidateWithBackendAsync( googleCredential, fullName, email, avatarUrl); if (success) { Console.WriteLine("✅ User validated, navigating to dashboard"); _instance._isLoading = false; _instance._errorMessage = string.Empty; _instance.NavigationManager.NavigateTo("/dashboard", replace: true); } else { Console.WriteLine($"❌ Validation failed: {errorMessage}"); _instance._isLoading = false; _instance._errorMessage = errorMessage ?? "Login failed."; } await _instance.InvokeAsync(() => _instance.StateHasChanged()); } catch (Exception ex) { Console.Error.WriteLine($"❌ OnGoogleSignInSuccess error: {ex.Message}"); _instance._isLoading = false; _instance._errorMessage = "User verification error."; await _instance.InvokeAsync(() => _instance.StateHasChanged()); } } } [JSInvokable] public static async Task OnGoogleSignInError(string error) { Console.WriteLine($"Google SignIn Error: {error}"); if (_instance != null) { _instance._isLoading = false; _instance._errorMessage = "Google login error. Please try again"; await _instance.InvokeAsync(() => _instance.StateHasChanged()); } } }