@page "/profile" @using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Components.Authorization @using Microsoft.Extensions.Configuration @attribute [Authorize] @inject ISessionStore SessionStore @inject IConfiguration Configuration @inject NavigationManager Navigation Профиль — GM-Relay

Профиль

@if (identities is null) {

Загрузка...

} else if (identities.Count == 0) {

Связанные аккаунты не найдены.

} else {

Связанные аккаунты

}

Добавить аккаунт

@if (!HasLinkedPlatform("Discord")) { Привязать Discord } else {

Discord уже привязан.

} @if (currentPlatform == "Discord" && !HasLinkedPlatform("Telegram")) { var botUsername = Configuration["Telegram__BotUsername"] ?? Configuration["Telegram:BotUsername"]; if (!string.IsNullOrWhiteSpace(botUsername)) { var authUrl = new Uri(new Uri(Navigation.BaseUri), "auth/telegram").ToString(); var widgetHtml = $"";
@((MarkupString)widgetHtml)
} }
@if (!string.IsNullOrWhiteSpace(errorMessage)) {
@errorMessage
} @if (!string.IsNullOrWhiteSpace(successMessage)) {
@successMessage
}
@code { private List? identities; private string? currentPlatform; private string? currentExternalUserId; private bool isUnlinking; private string? errorMessage; private string? successMessage; [CascadingParameter] private Task? AuthenticationStateTask { get; set; } [SupplyParameterFromQuery] public string? Linked { get; set; } [SupplyParameterFromQuery(Name = "link_error")] public string? LinkError { get; set; } protected override async Task OnInitializedAsync() { if (AuthenticationStateTask is not null) { var authState = await AuthenticationStateTask; var user = authState.User; if (user.TryGetPlatformIdentity(out var plat, out var extId)) { currentPlatform = plat; currentExternalUserId = extId; } } if (!string.IsNullOrWhiteSpace(Linked)) { successMessage = $"{Linked} аккаунт успешно привязан!"; } if (!string.IsNullOrWhiteSpace(LinkError)) { errorMessage = $"Ошибка привязки: {Uri.UnescapeDataString(LinkError)}"; } await LoadIdentities(); } private async Task LoadIdentities() { try { if (currentPlatform is not null && currentExternalUserId is not null) { identities = await SessionStore.GetLinkedIdentitiesAsync(currentPlatform, currentExternalUserId); } else { identities = []; } } catch (Exception ex) { errorMessage = $"Не удалось загрузить аккаунты: {ex.Message}"; } } private bool HasLinkedPlatform(string platform) { return identities?.Any(i => i.Platform == platform) ?? false; } private async Task Unlink(string platform, string externalUserId) { isUnlinking = true; errorMessage = null; successMessage = null; try { if (currentPlatform is null || currentExternalUserId is null) { errorMessage = "Не удалось определить текущего пользователя."; return; } await SessionStore.UnlinkIdentityAsync(currentPlatform, currentExternalUserId, platform, externalUserId); successMessage = $"{platform} аккаунт отвязан."; await LoadIdentities(); } catch (InvalidOperationException ex) { errorMessage = $"Ошибка отвязки: {ex.Message}"; } catch (Exception ex) { errorMessage = $"Ошибка отвязки: {ex.Message}"; } finally { isUnlinking = false; } } }