Google login is working
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
@using Microsoft.Extensions.Configuration
|
||||
@using Bimix.UI.Shared.Services
|
||||
@inject IJSRuntime JS
|
||||
@@ -41,10 +42,37 @@
|
||||
private bool _isLoading = false;
|
||||
private string _errorMessage = string.Empty;
|
||||
private static LoginCard? _instance;
|
||||
private bool _isInitialized = false;
|
||||
|
||||
protected override void OnInitialized()
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
_instance = this;
|
||||
if (firstRender)
|
||||
{
|
||||
_instance = this;
|
||||
await InitializeGoogleSignIn();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitializeGoogleSignIn()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_isInitialized) return;
|
||||
|
||||
var clientId = Configuration["GoogleAuth:ClientId"];
|
||||
if (string.IsNullOrEmpty(clientId))
|
||||
{
|
||||
throw new Exception("Google ClientId is not configured.");
|
||||
}
|
||||
|
||||
await JS.InvokeVoidAsync("initGoogleSignIn", clientId);
|
||||
_isInitialized = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_errorMessage = "Błąd inicjalizacji Google Sign-In.";
|
||||
Console.Error.WriteLine($"Google Sign-In initialization error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleGoogleSignIn()
|
||||
@@ -55,55 +83,53 @@
|
||||
_errorMessage = string.Empty;
|
||||
StateHasChanged();
|
||||
|
||||
var clientId = Configuration["GoogleAuth:ClientId"];
|
||||
|
||||
if (string.IsNullOrEmpty(clientId))
|
||||
{
|
||||
throw new Exception("Google ClientId is not configured.");
|
||||
}
|
||||
|
||||
|
||||
await JS.InvokeVoidAsync("initGoogleSignIn", clientId);
|
||||
await JS.InvokeVoidAsync("requestGoogleSignIn");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_errorMessage = "Błąd podczas logownia. Spróbuj ponownie";
|
||||
_errorMessage = "Błąd podczas logowania. Spróbuj ponownie";
|
||||
_isLoading = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public static async Task OnGoogleSignInSuccess(string idToken)
|
||||
public static async Task OnGoogleSignInSuccess(string accessToken, string fullName, string email, string avatarUrl)
|
||||
{
|
||||
Console.WriteLine($"Google ID Token: {idToken}");
|
||||
Console.WriteLine($"Google Sign-In Success: {email}");
|
||||
|
||||
if (_instance != null)
|
||||
{
|
||||
await _instance.AuthService.SetAuthenticationAsync(idToken);
|
||||
var userInfo = new UserInfo
|
||||
{
|
||||
FullName = fullName,
|
||||
Email = email,
|
||||
AvatarUrl = avatarUrl
|
||||
};
|
||||
|
||||
await _instance.AuthService.SetAuthenticationAsync(accessToken, userInfo);
|
||||
|
||||
_instance._isLoading = false;
|
||||
_instance._errorMessage = string.Empty;
|
||||
|
||||
_instance.NavigationManager.NavigateTo("/dashboard", replace:true);
|
||||
_instance.NavigationManager.NavigateTo("/dashboard", replace: true);
|
||||
|
||||
await _instance.InvokeAsync(() => _instance.StateHasChanged());
|
||||
}
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public static async Task OnGoggleSignInError(string error)
|
||||
public static async Task OnGoogleSignInError(string error)
|
||||
{
|
||||
Console.WriteLine($"Google SignIn Error: {error}");
|
||||
|
||||
if (_instance != null)
|
||||
{
|
||||
_instance._isLoading = false;
|
||||
_instance._errorMessage = "Błąd logowanie Google. Spróbuj ponownie";
|
||||
_instance._errorMessage = "Błąd logowania Google. Spróbuj ponownie";
|
||||
await _instance.InvokeAsync(() => _instance.StateHasChanged());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<style>
|
||||
@@ -113,9 +139,4 @@
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.google-signin-button {
|
||||
width: 100%;
|
||||
padding: 12px 24px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user