about summary refs log tree commit diff
path: root/BugMine.Web/Pages/Auth/LegacyLogin.razor
blob: eec381a41fcb304f71d42b435c7222d9cfb3485f (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
@page "/Auth/LegacyLogin"
@using System.Text.Json.Serialization
@using LibMatrix.Services
@inject HomeserverProviderService hsProvider
<h3>Login</h3>
<hr/>

<span style="display: block;">
    <label>User ID:</label>
    <span>@@</span><!--
    --><FancyTextBox @bind-Value="@authData.Username"></FancyTextBox><!--
    --><span>:</span><!--
    --><FancyTextBox @bind-Value="@authData.Homeserver"></FancyTextBox>
</span>
<span style="display: block;">
    <label>Password:</label>
    <FancyTextBox @bind-Value="@authData.Password" IsPassword="true"></FancyTextBox>
</span>
<span style="display: block">
    <label>Proxy (<a href="https://cgit.rory.gay/matrix/MxApiExtensions.git">MxApiExtensions</a> or similar):</label>
    <FancyTextBox @bind-Value="@authData.Proxy"></FancyTextBox>
</span>
<br/>
<LinkButton OnClick="@(() => LoginWithAuth(authData))">Log in</LinkButton>

<h4>Continue as guest (doesn't work)</h4>
<hr/>
<LinkButton OnClick="@(() => LoginWithAuth(new LoginStruct { Homeserver = "matrix.org", Username = "guest", Password = "guest" }))">Log in as guest</LinkButton>

@code {
    private LoginStruct authData = new();

    List<UserAuth>? LoggedInSessions { get; set; } = new();
    
    async Task LoginWithAuth(LoginStruct record) {
        if (LoggedInSessions.Any(x => x.UserId == $"@{record.Username}:{record.Homeserver}" && x.Proxy == record.Proxy)) return;
        StateHasChanged();
        try {
            var result = new UserAuth(await hsProvider.Login(record.Homeserver, record.Username, record.Password, record.Proxy)) {
                Proxy = record.Proxy
            };
            if (result == null) {
                Console.WriteLine($"Failed to login to {record.Homeserver} as {record.Username}!");
                return;
            }

            Console.WriteLine($"Obtained access token for {result.UserId}!");

            await BugMineStorage.AddToken(result);
            await BugMineStorage.SetCurrentToken(result);
        }
        catch (Exception e) {
            Console.WriteLine($"Failed to login to {record.Homeserver} as {record.Username}!");
            Console.WriteLine(e);
            record.Exception = e;
        }

        StateHasChanged();
    }

    private class LoginStruct {
        public string? Homeserver { get; set; } = "";
        public string? Username { get; set; } = "";
        public string? Password { get; set; } = "";
        public string? Proxy { get; set; }
        
        [JsonIgnore]
        internal Exception? Exception { get; set; }
    }

}