This commit is contained in:
113
BimAI.UI.Shared/Services/AuthService.cs
Normal file
113
BimAI.UI.Shared/Services/AuthService.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace BimAI.UI.Shared.Services;
|
||||
|
||||
public class UserInfo
|
||||
{
|
||||
public string FullName { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string AvatarUrl { get; set; } = string.Empty;
|
||||
}
|
||||
public class AuthService
|
||||
{
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
private bool? _isAuthenticated;
|
||||
private UserInfo? _userInfo = null;
|
||||
|
||||
public event Action<bool>? AuthenticationStateChanged;
|
||||
|
||||
public AuthService(IJSRuntime jsRuntime)
|
||||
{
|
||||
_jsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
public bool IsAuthenticated => _isAuthenticated ?? false;
|
||||
public UserInfo? CurrentUser => _userInfo;
|
||||
|
||||
public async Task<bool> CheckAuthenticationAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var token = await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", "google_token");
|
||||
var userInfoJson = await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", "user_info");
|
||||
|
||||
_isAuthenticated = !string.IsNullOrEmpty(token);
|
||||
|
||||
if (_isAuthenticated.Value && !string.IsNullOrEmpty(userInfoJson))
|
||||
{
|
||||
_userInfo = JsonSerializer.Deserialize<UserInfo>(userInfoJson);
|
||||
}
|
||||
|
||||
Console.WriteLine($"AuthService.CheckAuthentication: token={(!string.IsNullOrEmpty(token) ? "EXISTS" : "NULL")}, isAuth={_isAuthenticated}");
|
||||
|
||||
return _isAuthenticated.Value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"AuthService.CheckAuthentication ERROR: {ex.Message}");
|
||||
_isAuthenticated = false;
|
||||
_userInfo = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetAuthenticationAsync(string token, UserInfo? userInfo = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "google_token", token);
|
||||
|
||||
if (userInfo != null)
|
||||
{
|
||||
_userInfo = userInfo;
|
||||
var userInfoJson = JsonSerializer.Serialize(userInfo);
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "user_info", userInfoJson);
|
||||
}
|
||||
|
||||
_isAuthenticated = true;
|
||||
Console.WriteLine($"AuthService.SetAuthentication: token saved, user={_userInfo?.Email}");
|
||||
AuthenticationStateChanged?.Invoke(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"AuthService.SetAuthentication ERROR: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ClearAuthenticationAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", "google_token");
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", "user_info");
|
||||
_isAuthenticated = false;
|
||||
_userInfo = null;
|
||||
Console.WriteLine($"AuthService.ClearAuthentication: token and user ingfo removed");
|
||||
AuthenticationStateChanged?.Invoke(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"AuthService.ClearAuthentication ERROR: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> GetTokenAsync()
|
||||
{
|
||||
if (_isAuthenticated != true)
|
||||
{
|
||||
await CheckAuthenticationAsync();
|
||||
}
|
||||
|
||||
if (_isAuthenticated != true) return null;
|
||||
|
||||
try
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", "google_token");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
BimAI.UI.Shared/Services/GoogleAuthConfig.cs
Normal file
7
BimAI.UI.Shared/Services/GoogleAuthConfig.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace BimAI.UI.Shared.Services;
|
||||
|
||||
// TODO it's a good place for this file?
|
||||
public class GoogleAuthConfig
|
||||
{
|
||||
public string ClientId { get; set; } = string.Empty;
|
||||
}
|
||||
18
BimAI.UI.Shared/Services/NoOpScannerService.cs
Normal file
18
BimAI.UI.Shared/Services/NoOpScannerService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using BimAI.UI.Shared.Interfaces;
|
||||
|
||||
namespace BimAI.UI.Shared.Services;
|
||||
|
||||
public class NoOpScannerService : IScannerService
|
||||
{
|
||||
public bool IsAvailable => false;
|
||||
public Task<string?> ScanBarcodeAsync()
|
||||
{
|
||||
return Task.FromResult<string?>(null);
|
||||
}
|
||||
public Task<bool> RequestCameraPermissionsAsync()
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
52
BimAI.UI.Shared/Services/ProductService.cs
Normal file
52
BimAI.UI.Shared/Services/ProductService.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Text.Json;
|
||||
using BimAI.Application.DTOModels;
|
||||
using BimAI.Application.DTOModels.Common;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
|
||||
namespace BimAI.UI.Shared.Services;
|
||||
|
||||
public class ProductService(HttpClient httpClient)
|
||||
{
|
||||
private readonly HttpClient _httpClient = httpClient;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public async Task<PagedResult<ProductDto>> GetProductsAsync(ProductFilterRequest request)
|
||||
{
|
||||
var queryParams = new Dictionary<string, string?>
|
||||
{
|
||||
["page"] = request.Page.ToString(),
|
||||
["pageSize"] = request.PageSize.ToString(),
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||
{
|
||||
queryParams["search"] = request.Search;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||
{
|
||||
queryParams["name"] = request.Name;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(request.Code))
|
||||
{
|
||||
queryParams["code"] = request.Code;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Ean))
|
||||
{
|
||||
queryParams["ean"] = request.Ean;
|
||||
}
|
||||
|
||||
var uri = QueryHelpers.AddQueryString("api/products", queryParams);
|
||||
var response = await _httpClient.GetAsync(uri);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var result = JsonSerializer.Deserialize<PagedResult<ProductDto>>(json, _jsonOptions);
|
||||
|
||||
return result ?? new PagedResult<ProductDto>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user