summary refs log tree commit diff
path: root/extra/admin-api/Utilities/Spacebar.Client/Pages/Auth/Login.razor
blob: 63b0c0c758690bf0f4219c0c7b33cec739e831e9 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@page "/login"
@using ArcaneLibs.Blazor.Components
@using Spacebar.Client.WebCore
@using Spacebar.Sdk.Core
@inject SpacebarClientWellKnownResolverService clientWellKnownResolver
@inject SessionStore sessionStore
@inject SpacebarClientProviderService clientProvider

<PageTitle>Log in</PageTitle>

<span>Instance (server name):</span>
<FancyTextBox @bind-Value="@ServerName"/>
<LinkButton InlineText="true">[🧭 Discover]</LinkButton>
<br/>
<span>Email:</span>
<FancyTextBox @bind-Value="@Username"/>
<br/>
<span>Password:</span>
<FancyTextBox @bind-Value="@Password" IsPassword="true"/>
<br/>
<LinkButton OnClickAsync="LoginAsync">Log in</LinkButton>

<pre>@ServerValidationStatus</pre>

@code{

    private string ServerName {
        get;
        set {
            field = value;
            CheckServer(value);
        }
    } = "spacebar.chat";

    private string Username { get; set; } = "";
    private string Password { get; set; } = "";

    private string? ServerValidationStatus;

    private async Task SetStatus(string? status) {
        ServerValidationStatus = status;
        StateHasChanged();
        await Task.Yield();
    }

    private async Task CheckServer(string serverName) {
        await SetStatus("Checking server: " + serverName);
        try {
            await clientWellKnownResolver.ResolveClientWellKnown(serverName);
            await SetStatus($"Server {serverName} is valid!");
        }
        catch (Exception e) {
            await SetStatus("Could not validate server: " + e.Message);
        }
    }

    private async Task LoginAsync() {
        try {
            await SetStatus($"Looking up {ServerName}...");
            var cwk = await clientWellKnownResolver.ResolveClientWellKnown(ServerName);
            
            await SetStatus($"Trying to log in to {ServerName}...");
            var usc = await clientProvider.GetUnauthenticatedClientAsync(ServerName);
            var lrsp = await usc.LoginAsync(new() {
                Login = Username,
                Password = Password
            });

            await SetStatus($"Logged in as user ID {lrsp.UserId}, fetching profile...");
            var asc = await clientProvider.GetAuthenticatedClientAsync(ServerName, lrsp.Token!);
            var cu = await asc.GetCurrentUser();
            await SetStatus($"Logged in as {cu.Username}#{cu.Discriminator} ({cu.Id})! Saving...");
            await sessionStore.AddSession(new SessionEntry() {
                ServerName = ServerName,
                AccessToken = lrsp.Token!,
                ProfileCache = new() {
                    Id = cu.Id,
                    Username = cu.Username,
                    Discriminator = cu.Discriminator,
                    AvatarUrl = cu.Avatar ?? "",
                }
            }, setCurrent: true);

            // await SetStatus(lrsp.ToJson());
        }
        catch (Exception e) {
            await SetStatus("Failed to log in: " + e);
        }
    }

}