Enhance authentication flow; add user registration completion check and update contract acceptance process
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Blazored.LocalStorage;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using MudBlazor;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
|
||||
namespace FrontOffice.Main.Utilities;
|
||||
|
||||
@@ -9,14 +10,16 @@ public class AuthService
|
||||
private readonly ILocalStorageService _localStorage;
|
||||
private readonly NavigationManager _navigation;
|
||||
private readonly ISnackbar _snackbar;
|
||||
private readonly UserAuthInfo _userAuthInfo;
|
||||
|
||||
private const string TokenStorageKey = "auth:token";
|
||||
|
||||
public AuthService(ILocalStorageService localStorage, NavigationManager navigation, ISnackbar snackbar)
|
||||
public AuthService(ILocalStorageService localStorage, NavigationManager navigation, ISnackbar snackbar, UserAuthInfo userAuthInfo)
|
||||
{
|
||||
_localStorage = localStorage;
|
||||
_navigation = navigation;
|
||||
_snackbar = snackbar;
|
||||
_userAuthInfo = userAuthInfo;
|
||||
}
|
||||
|
||||
public async Task<bool> IsAuthenticatedAsync()
|
||||
@@ -29,6 +32,54 @@ public class AuthService
|
||||
{
|
||||
return await _localStorage.GetItemAsync<string>(TokenStorageKey);
|
||||
}
|
||||
public async Task<bool> IsCompleteRegisterAsync()
|
||||
{
|
||||
await InitUserAuthInfo();
|
||||
if (_userAuthInfo.NationalCode ==null || _userAuthInfo.FirstName == null || _userAuthInfo.LastName == null || !_userAuthInfo.IsSignMainContract)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool IsCompleteRegister()
|
||||
{
|
||||
InitUserAuthInfo().GetAwaiter();
|
||||
if (_userAuthInfo.NationalCode ==null || _userAuthInfo.FirstName == null || _userAuthInfo.LastName == null || _userAuthInfo.IsSignMainContract)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public async Task<UserAuthInfo> GetUserAuthInfo()
|
||||
{
|
||||
await InitUserAuthInfo();
|
||||
return _userAuthInfo;
|
||||
}
|
||||
|
||||
public async Task InitUserAuthInfo()
|
||||
{
|
||||
var claims = await GetTokenClaimsAsync();
|
||||
_userAuthInfo.UserClaims= claims;
|
||||
var firstName = claims.FirstOrDefault(c => c.key == "FirstName").value;
|
||||
var lastName = claims.FirstOrDefault(c => c.key == "LastName").value;
|
||||
var nationalCode = claims.FirstOrDefault(c => c.key == "NationalCode").value;
|
||||
var mobileNumber = claims.FirstOrDefault(c => c.key == "MobileNumber").value;
|
||||
var isSignMainContractStr = claims.FirstOrDefault(c => c.key == "IsSignMainContract").value;
|
||||
_userAuthInfo.FirstName = firstName ?? string.Empty;
|
||||
_userAuthInfo.LastName = lastName ?? string.Empty;
|
||||
_userAuthInfo.NationalCode = nationalCode ?? string.Empty;
|
||||
_userAuthInfo.MobileNumber = mobileNumber ?? string.Empty;
|
||||
_userAuthInfo.IsSignMainContract = bool.TryParse(isSignMainContractStr, out var isSignMainContract) && isSignMainContract;
|
||||
}
|
||||
|
||||
private async Task<List<(string key, string value)>?> GetTokenClaimsAsync()
|
||||
{
|
||||
var token = await GetTokenAsync();
|
||||
if (string.IsNullOrEmpty(token)) return null;
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(token);
|
||||
return jwtToken.Claims.Select(c => (c.Type, c.Value)).ToList();
|
||||
}
|
||||
|
||||
public async Task LogoutAsync()
|
||||
{
|
||||
|
||||
11
src/FrontOffice.Main/Utilities/UserAuthInfo.cs
Normal file
11
src/FrontOffice.Main/Utilities/UserAuthInfo.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace FrontOffice.Main.Utilities;
|
||||
|
||||
public class UserAuthInfo
|
||||
{
|
||||
public List<(string key, string value)> UserClaims { get; set; }
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? NationalCode { get; set; }
|
||||
public string? MobileNumber { get; set; }
|
||||
public bool IsSignMainContract { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user