Skip to content

Commit

Permalink
Remove synchronous code from AuthComponentBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Apr 17, 2021
1 parent d40d05a commit 4120faa
Show file tree
Hide file tree
Showing 29 changed files with 131 additions and 110 deletions.
5 changes: 3 additions & 2 deletions Server/Components/AlertsFrame.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@

private string FrameClass => _isOpen ? "open" : null;

protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

if (IsAuthenticated)
{
GetAlerts();
}
return base.OnInitializedAsync();
}

private async Task ClearAlert(Alert alert)
Expand Down
16 changes: 12 additions & 4 deletions Server/Components/AuthComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Identity;
using Remotely.Server.Services;
using Remotely.Shared.Extensions;
using Remotely.Shared.Models;
using System.Threading.Tasks;

namespace Remotely.Server.Components
{
public class AuthComponentBase : ComponentBase
{
public bool IsAuthenticated => AuthService?.IsAuthenticated ?? false;
protected override async Task OnInitializedAsync()
{
IsAuthenticated = await AuthService.IsAuthenticated();
User = await AuthService.GetUser();
Username = User?.UserName;
await base.OnInitializedAsync();
}

public RemotelyUser User => AuthService?.User;
public bool IsAuthenticated { get; private set; }

public string Username => AuthService?.Principal?.Identity?.Name;
public RemotelyUser User { get; private set; }

public string Username { get; private set; }

[Inject]
protected IAuthService AuthService { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/AuthorizedIndex.razor
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

if (IsAuthenticated == true && User is null)
{
await SignInManager.SignOutAsync();
NavManager.NavigateTo("/");
}
await base.OnInitializedAsync();
}
}
3 changes: 2 additions & 1 deletion Server/Components/Branding.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h3 class="mb-3">Branding</h3>


@if (User.IsAdministrator != true)
@if (User?.IsAdministrator != true)
{
<h5 class="text-muted">Only organization administrators can view this page.</h5>
}
Expand Down Expand Up @@ -125,6 +125,7 @@ else

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

var organization = await DataService.GetOrganizationByUserName(User.UserName);

Expand Down
4 changes: 2 additions & 2 deletions Server/Components/Devices/ChatCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ protected override void OnAfterRender(bool firstRender)
JsInterop.ScrollToEnd(_chatMessagesWindow);
base.OnAfterRender(firstRender);
}
protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
AppState.PropertyChanged += AppState_PropertyChanged;
CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;
return base.OnInitializedAsync();
}

private void AppState_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions Server/Components/Devices/ChatFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
AppState.PropertyChanged += AppState_PropertyChanged;
CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;
return base.OnInitializedAsync();
}

private void CircuitConnection_MessageReceived(object sender, Models.CircuitEvent e)
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Devices/DeviceCard.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@attribute [Authorize]
@inherits AuthComponentBase

<div @ref="_card" class="card border-secondary my-3 mr-3 device-card @Theme @GetCardStateClass(Device)"
<div @ref="_card" class="card border-secondary my-3 mr-3 device-card @_theme @GetCardStateClass(Device)"
@onclick="ExpandCard"
@onclick:stopPropagation="true"
@oncontextmenu="ContextMenuOpening"
Expand Down
8 changes: 5 additions & 3 deletions Server/Components/Devices/DeviceCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Remotely.Server.Hubs;
using Remotely.Server.Models;
using Remotely.Server.Services;
using Remotely.Shared.Enums;
using Remotely.Shared.Models;
using Remotely.Shared.Utilities;
using Remotely.Shared.ViewModels;
Expand All @@ -24,6 +25,7 @@ public partial class DeviceCard : AuthComponentBase, IDisposable
{
private ElementReference _card;
private ConcurrentDictionary<string, double> _fileUploadProgressLookup = new();
private Theme _theme;

[Parameter]
public Device Device { get; set; }
Expand Down Expand Up @@ -52,7 +54,6 @@ public partial class DeviceCard : AuthComponentBase, IDisposable

[Inject]
private IJsInterop JsInterop { get; set; }
private string Theme => AppState.EffectiveTheme.ToString().ToLower();

[Inject]
private IToastService ToastService { get; set; }
Expand All @@ -63,10 +64,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_theme = await AppState.GetEffectiveTheme();
AppState.PropertyChanged += AppState_PropertyChanged;
return base.OnInitializedAsync();
}

private void AppState_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions Server/Components/Devices/DevicesFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ public void Refresh()
InvokeAsync(StateHasChanged);
}

protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;
AppState.PropertyChanged += AppState_PropertyChanged;

Expand All @@ -170,8 +172,6 @@ protected override Task OnInitializedAsync()
.Where(x => x.CustomAttributes.Any(x => x.AttributeType == typeof(SortableAttribute)));

_sortableProperties.AddRange(sortableProperties);

return base.OnInitializedAsync();
}

protected override bool ShouldRender()
Expand Down
4 changes: 2 additions & 2 deletions Server/Components/Devices/Terminal.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;
AppState.PropertyChanged += AppState_PropertyChanged;
return base.OnInitializedAsync();
}

protected override Task OnAfterRenderAsync(bool firstRender)
Expand Down
14 changes: 12 additions & 2 deletions Server/Components/LoadingSignal.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
@inject IClientAppState AppState


@if (AppState.EffectiveTheme == Theme.Dark)
@if (_theme == Theme.Dark)
{
<div class="signal" style="border-color: whitesmoke"></div>
}
else if (AppState.EffectiveTheme == Theme.Light)
else if (_theme == Theme.Light)
{
<div class="signal" style="border-color: #333"></div>
}

@code {
private Theme _theme;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_theme = await AppState.GetEffectiveTheme();
}
}
9 changes: 5 additions & 4 deletions Server/Components/Scripts/RunScript.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ protected override void OnAfterRender(bool firstRender)
base.OnAfterRender(firstRender);
}

protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

_deviceGroups = DataService.GetDeviceGroups(User.UserName);
_devices = DataService
.GetDevicesForUser(User.UserName)
.OrderBy(x=>x.DeviceName)
.OrderBy(x => x.DeviceName)
.ToArray();

base.OnInitialized();
}

private void DeviceGroupSelectedChanged(ChangeEventArgs args, DeviceGroup deviceGroup)
{
var isSelected = (bool)args.Value;
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/ScriptSchedules.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public partial class ScriptSchedules : AuthComponentBase

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_deviceGroups = DataService.GetDeviceGroups(User.UserName);
_devices = DataService
.GetDevicesForUser(User.UserName)
.OrderBy(x => x.DeviceName)
.ToArray();

await RefreshSchedules();
await base.OnInitializedAsync();
}

private void CreateNew()
Expand Down
4 changes: 2 additions & 2 deletions Server/Hubs/CircuitConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public override async Task OnCircuitClosedAsync(Circuit circuit, CancellationTok

public override async Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken)
{
if (_authService.IsAuthenticated)
if (await _authService.IsAuthenticated())
{
User = _authService.User;
User = await _authService.GetUser();
ConnectionId = Guid.NewGuid().ToString();
_circuitManager.TryAddConnection(ConnectionId, this);
}
Expand Down
24 changes: 12 additions & 12 deletions Server/Pages/ApiKeys.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@inject IDataService DataService
@inject AuthenticationStateProvider AuthProvider
@inject IJsInterop JsInterop
@inject IModalService ModalService
@inject IModalService ModalService


<h3>API Keys</h3>
Expand All @@ -15,22 +15,22 @@
<AlertBanner Message="@_alertMessage" />
}

@if (!string.IsNullOrWhiteSpace(_newKeySecret))
@if (User?.IsAdministrator == true)
{
<h5 class="text-warning font-weight-bold mb-2 mt-4">Warning: The key's secret will only be shown once. Save it now.</h5>
<h5 class="mb-4">
<label class="mr-1">Key Secret:</label>
<input class="form-control custom-control-inline" readonly value="@_newKeySecret" style="width:400px" />
</h5>
}
if (!string.IsNullOrWhiteSpace(_newKeySecret))
{
<h5 class="text-warning font-weight-bold mb-2 mt-4">Warning: The key's secret will only be shown once. Save it now.</h5>
<h5 class="mb-4">
<label class="mr-1">Key Secret:</label>
<input class="form-control custom-control-inline" readonly value="@_newKeySecret" style="width:400px" />
</h5>
}


@if (User.IsAdministrator)
{
<div class="mb-2 mt-4">
<label class="mr-1">New Token Name:</label>
<input @bind="_createKeyName" @bind:event="oninput"
class="form-control form-control-sm custom-control-inline mr-1"
class="form-control form-control-sm custom-control-inline mr-1"
style="width:200px" />

<button class="btn btn-primary" type="button" @onclick="CreateNewKey">Create</button>
Expand Down Expand Up @@ -82,8 +82,8 @@ else

protected override async Task OnInitializedAsync()
{
RefreshData();
await base.OnInitializedAsync();
RefreshData();
}

private async Task CreateNewKey()
Expand Down
6 changes: 3 additions & 3 deletions Server/Pages/DeviceDetails.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ public partial class DeviceDetails : AuthComponentBase
[Inject]
private IToastService ToastService { get; set; }

protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

if (!string.IsNullOrWhiteSpace(DeviceId))
{
Device = DataService.GetDevice(DeviceId);
}

CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;

return base.OnInitializedAsync();
}

private void CircuitConnection_MessageReceived(object sender, Models.CircuitEvent e)
Expand Down
7 changes: 4 additions & 3 deletions Server/Pages/Downloads.razor
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

var authState = await AuthProvider.GetAuthenticationStateAsync();
_isAuthenticated = authState.User.Identity.IsAuthenticated;

if (_isAuthenticated)
{
var currentUser = await UserManager.GetUserAsync(authState.User);
_organizationId = DataService.GetOrganizationById(currentUser.OrganizationID)?.ID;
var currentUser = await DataService.GetUserAsync(authState.User.Identity.Name);
_organizationId = currentUser.OrganizationID;
}
else
{
Expand Down Expand Up @@ -250,6 +252,5 @@
{
ClickOnceMiddleware.AppFileLock.Release();
}
await base.OnInitializedAsync();
}
}
4 changes: 2 additions & 2 deletions Server/Pages/ManageOrganization.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public partial class ManageOrganization : AuthComponentBase

protected override async Task OnInitializedAsync()
{
await RefreshData();

await base.OnInitializedAsync();

await RefreshData();
}

private void CreateNewDeviceGroup()
Expand Down
2 changes: 1 addition & 1 deletion Server/Pages/ScriptsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@

protected override async Task OnInitializedAsync()
{
await RefreshScripts();
await base.OnInitializedAsync();
await RefreshScripts();
}


Expand Down
2 changes: 1 addition & 1 deletion Server/Pages/ServerConfig.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@inherits AuthComponentBase


@if (User.IsServerAdmin)
@if (User?.IsServerAdmin == true)
{
<AlertBanner Message="@_alertMessage" />

Expand Down

0 comments on commit 4120faa

Please sign in to comment.