@page "/Login"
@using System.Text.Json
@using LibMatrix.Responses
@using MatrixRoomUtils.Web.Shared.SimpleComponents
@inject ILocalStorageService LocalStorage
@inject IJSRuntime JsRuntime
Login
@@:
Parsed records
Username |
Homeserver |
@foreach (var (homeserver, username, password) in records) {
var record = (homeserver, username, password);
@username |
@homeserver |
records.Remove(record)">Remove |
}
@code {
readonly List<(string homeserver, string username, string password)> records = new();
(string homeserver, string username, string password) newRecordInput = ("", "", "");
List LoggedInSessions { get; set; } = new();
async Task Login() {
var loginTasks = records.Select(async record => {
var (homeserver, username, password) = record;
if (LoggedInSessions.Any(x => x.UserId == $"@{username}:{homeserver}")) return;
try {
var result = await HomeserverProvider.Login(homeserver, username, password);
if (result == null) {
Console.WriteLine($"Failed to login to {homeserver} as {username}!");
return;
}
Console.WriteLine($"Obtained access token for {result.UserId}!");
await MRUStorage.AddToken(result);
LoggedInSessions = await MRUStorage.GetAllTokens();
}
catch (Exception e) {
Console.WriteLine($"Failed to login to {homeserver} as {username}!");
Console.WriteLine(e);
}
StateHasChanged();
});
await Task.WhenAll(loginTasks);
}
private async Task FileChanged(InputFileChangeEventArgs obj) {
LoggedInSessions = await MRUStorage.GetAllTokens();
Console.WriteLine(JsonSerializer.Serialize(obj, new JsonSerializerOptions {
WriteIndented = true
}));
await using var rs = obj.File.OpenReadStream();
using var sr = new StreamReader(rs);
var TsvData = await sr.ReadToEndAsync();
records.Clear();
foreach (var line in TsvData.Split('\n')) {
var parts = line.Split('\t');
if (parts.Length != 3)
continue;
records.Add((parts[0], parts[1], parts[2]));
}
}
private async Task AddRecord() {
LoggedInSessions = await MRUStorage.GetAllTokens();
records.Add(newRecordInput);
newRecordInput = ("", "", "");
}
}