- Create Services/AuthStateService.cs with Login/Logout/GetPortalUrl - Register as singleton in Program.cs Co-authored-by: Velik <hongochai10@users.noreply.github.com>
39 lines
964 B
C#
39 lines
964 B
C#
namespace WebClientTpos.Client.Services;
|
|
|
|
public class AuthStateService
|
|
{
|
|
public bool IsAuthenticated { get; private set; }
|
|
public string? UserEmail { get; private set; }
|
|
public string? UserRole { get; private set; } // "owner", "staff", "customer", "branch"
|
|
public string? Token { get; private set; }
|
|
|
|
public event Action? OnChange;
|
|
|
|
public void Login(string email, string token, string role)
|
|
{
|
|
IsAuthenticated = true;
|
|
UserEmail = email;
|
|
Token = token;
|
|
UserRole = role;
|
|
OnChange?.Invoke();
|
|
}
|
|
|
|
public void Logout()
|
|
{
|
|
IsAuthenticated = false;
|
|
UserEmail = null;
|
|
Token = null;
|
|
UserRole = null;
|
|
OnChange?.Invoke();
|
|
}
|
|
|
|
public string GetPortalUrl() => UserRole switch
|
|
{
|
|
"owner" or "admin" => "/admin",
|
|
"staff" => "/pos/cafe",
|
|
"branch" => "/admin",
|
|
"customer" => "/app",
|
|
_ => "/auth/login"
|
|
};
|
|
}
|