-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefaultUAuthStateManager.cs
More file actions
55 lines (45 loc) · 1.46 KB
/
DefaultUAuthStateManager.cs
File metadata and controls
55 lines (45 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using CodeBeam.UltimateAuth.Client.Runtime;
using CodeBeam.UltimateAuth.Core.Abstractions;
namespace CodeBeam.UltimateAuth.Client.Authentication
{
internal sealed class DefaultUAuthStateManager : IUAuthStateManager
{
private readonly IUAuthClient _client;
private readonly IClock _clock;
private readonly IUAuthClientBootstrapper _bootstrapper;
public UAuthState State { get; } = UAuthState.Anonymous();
public DefaultUAuthStateManager(IUAuthClient client, IClock clock, IUAuthClientBootstrapper bootstrapper)
{
_client = client;
_clock = clock;
_bootstrapper = bootstrapper;
}
public async Task EnsureAsync(CancellationToken ct = default)
{
if (State.IsAuthenticated && !State.IsStale)
return;
await _bootstrapper.EnsureStartedAsync();
var result = await _client.Flows.ValidateAsync();
if (!result.IsValid)
{
State.Clear();
return;
}
State.ApplySnapshot(result.Snapshot, _clock.UtcNow);
}
public Task OnLoginAsync()
{
State.MarkStale();
return Task.CompletedTask;
}
public Task OnLogoutAsync()
{
State.Clear();
return Task.CompletedTask;
}
public void MarkStale()
{
State.MarkStale();
}
}
}