about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/InvalidSession.razor
blob: 1ec99f6808cbee4d0d89398033785f70da61d953 (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
92
93
94
95
96
@page "/InvalidSession"
@using LibMatrix

<PageTitle>Invalid session</PageTitle>

<h3>Rory&::MatrixUtils - Invalid session encountered</h3>
<p>A session was encountered that is no longer valid. This can happen if you have logged out of the account on another device, or if the access token has expired.</p>

@if (_auth is not null) {
    <p>It appears that the affected user is @_auth.UserId (@_auth.DeviceId) on @_auth.Homeserver!</p>
    <LinkButton OnClick="@(OpenRefreshDialog)">Refresh token</LinkButton>
    <LinkButton OnClick="@(RemoveUser)">Remove</LinkButton>

    @if (_showRefreshDialog) {
        <ModalWindow MinWidth="300" X="275" Y="300" Title="@($"Password for {_auth.UserId}")">
            <FancyTextBox IsPassword="true" @bind-Value="@_password"></FancyTextBox>
            <br/>
            <LinkButton OnClick="TryLogin">Log in</LinkButton>
            @if (_loginException is not null) {
                <pre style="color: red;">@_loginException.RawContent</pre>
            }
        </ModalWindow>
    }
}
else {
    <b>Something has gone wrong and the login was not passed along!</b>
}

@code
{
    [Parameter]
    [SupplyParameterFromQuery(Name = "ctx")]
    public string SessionId { get; set; }

    private UserAuth? _auth { get; set; }

    private bool _showRefreshDialog { get; set; }

    private string _password { get; set; } = "";

    private MatrixException? _loginException { get; set; }

    protected override async Task OnInitializedAsync() {
        var tokens = await sessionStore.GetAllSessions();
        if (tokens.Count == 0) {
            NavigationManager.NavigateTo("/Login");
            return;
        }

        if (tokens.TryGetValue(SessionId, out var session))
            _auth = session.Auth;
        else Console.WriteLine($"Could not find {SessionId} in stored sessions!");

        await base.OnInitializedAsync();
    }

    private async Task RemoveUser() {
        await sessionStore.RemoveSession(SessionId);
        await OnInitializedAsync();
    }

    private async Task OpenRefreshDialog() {
        _showRefreshDialog = true;
        StateHasChanged();
        await Task.CompletedTask;
    }

    private async Task SwitchSession(string sessionId) {
        Console.WriteLine($"Switching to session {sessionId}");
        await sessionStore.SetCurrentSession(sessionId);
        await OnInitializedAsync();
    }

    private async Task TryLogin() {
        if (_auth is null) throw new NullReferenceException("Login is null!");
        try {
            var result = new UserAuth(await HsProvider.Login(_auth.Homeserver, _auth.UserId, _password));
            if (result is null) {
                Console.WriteLine($"Failed to login to {_auth.Homeserver} as {_auth.UserId}!");
                return;
            }

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

            await sessionStore.RemoveSession(SessionId);
            await sessionStore.AddSession(result);
            NavigationManager.NavigateTo("/");
        }
        catch (MatrixException e) {
            Console.WriteLine($"Failed to login to {_auth.Homeserver} as {_auth.UserId}!");
            Console.WriteLine(e);
            _loginException = e;
            StateHasChanged();
        }
    }
}