Added authenication/authorization. Refactored api startup.
This commit is contained in:
28
JSMR.UI.Blazor/Services/SessionState.cs
Normal file
28
JSMR.UI.Blazor/Services/SessionState.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace JSMR.UI.Blazor.Services;
|
||||
|
||||
public sealed class SessionState(AuthenticationClient auth)
|
||||
{
|
||||
public AuthenticationClient.MeResponse? Me { get; private set; }
|
||||
public bool IsAuthenticated => Me is not null;
|
||||
|
||||
public event Action? Changed;
|
||||
|
||||
public async Task RefreshAsync(CancellationToken ct = default)
|
||||
{
|
||||
Me = await auth.GetMeAsync(ct);
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
public async Task<bool> LoginAsync(string username, string password, CancellationToken ct = default)
|
||||
{
|
||||
var ok = await auth.LoginAsync(username, password, ct);
|
||||
await RefreshAsync(ct);
|
||||
return ok;
|
||||
}
|
||||
|
||||
public async Task LogoutAsync(CancellationToken ct = default)
|
||||
{
|
||||
await auth.LogoutAsync(ct);
|
||||
await RefreshAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user