106 lines
No EOL
4 KiB
Text
106 lines
No EOL
4 KiB
Text
@page "/login"
|
|
@using System.Diagnostics.CodeAnalysis
|
|
@using Iceshrimp.Frontend.Core.Miscellaneous
|
|
@using Iceshrimp.Frontend.Core.Schemas
|
|
@using Iceshrimp.Frontend.Core.Services
|
|
@using Iceshrimp.Frontend.Localization
|
|
@using Iceshrimp.Shared.Schemas.Web
|
|
@using Microsoft.Extensions.Localization
|
|
@inject ApiService Api
|
|
@inject SessionService SessionService
|
|
@inject NavigationManager Navigation
|
|
@inject IStringLocalizer<Localization> Loc;
|
|
@inject MetadataService Metadata;
|
|
@layout UnauthLayout
|
|
<div class="body">
|
|
<img class="logo" src="/_content/Iceshrimp.Assets.Branding/splash.png"/>
|
|
<span>
|
|
<h3>@Loc["Login to {0}", Name ?? "this Iceshrimp.NET Instance."]</h3></span>
|
|
<div class="login-form">
|
|
<input placeholder="@Loc["Username"]" autocomplete="username" name="username" required="required"
|
|
@bind="@Username"/>
|
|
<input type="password" placeholder="@Loc["Password"]" autocomplete="current-password" name="current-password" required="required"
|
|
@bind="@Password"/>
|
|
<button class="button" @onclick="Submit" disabled="@Loading">@Loc["Login"]</button>
|
|
</div>
|
|
|
|
@if (Loading)
|
|
{
|
|
<span>Loading!</span>
|
|
}
|
|
@if (Failure)
|
|
{
|
|
<span>Authentication Failed</span>
|
|
}
|
|
|
|
</div>
|
|
|
|
@code {
|
|
[SupplyParameterFromQuery(Name = "rd")]
|
|
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
|
|
private string? Redirect { get; set; }
|
|
|
|
private string? Password { get; set; }
|
|
private string? Username { get; set; }
|
|
private bool Loading { get; set; }
|
|
private bool Failure { get; set; }
|
|
private string? Name { get; set; }
|
|
|
|
private async Task Submit()
|
|
{
|
|
Loading = true;
|
|
try
|
|
{
|
|
if (Username == null || Password == null)
|
|
{
|
|
Loading = false;
|
|
Failure = true;
|
|
StateHasChanged(); // Manually triggering a state update, else component will not re-render.
|
|
return;
|
|
}
|
|
|
|
var res = await Api.Auth.LoginAsync(new AuthRequest { Username = Username, Password = Password });
|
|
switch (res.Status)
|
|
{
|
|
case AuthStatusEnum.Authenticated:
|
|
SessionService.AddUser(new StoredUser
|
|
{ // Token nor user will ever be null on an authenticated response
|
|
Id = res.User!.Id,
|
|
Username = res.User.Username,
|
|
DisplayName = res.User.DisplayName,
|
|
AvatarUrl = res.User.AvatarUrl,
|
|
BannerUrl = res.User.BannerUrl,
|
|
InstanceName = res.User.InstanceName,
|
|
InstanceIconUrl = res.User.InstanceIconUrl,
|
|
Token = res.Token!,
|
|
Host = res.User.Host,
|
|
IsAdmin = res.IsAdmin ?? false,
|
|
Emojis = res.User.Emojis,
|
|
MovedTo = res.User.MovedTo
|
|
});
|
|
SessionService.SetSession(res.User.Id);
|
|
Navigation.NavigateTo(Uri.TryCreate(Redirect, UriKind.Relative, out _) ? Redirect : "/", true);
|
|
break;
|
|
case AuthStatusEnum.Guest:
|
|
Failure = true;
|
|
Loading = false;
|
|
break;
|
|
case AuthStatusEnum.TwoFactor:
|
|
//FixMe: Implement Two Factor
|
|
break;
|
|
}
|
|
}
|
|
catch (ApiException)
|
|
{
|
|
Loading = false;
|
|
Failure = false;
|
|
StateHasChanged(); // Manually triggering a state update, else component will not re-render.
|
|
}
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var metadata = await Metadata.Instance.Value;
|
|
Name = metadata.Name;
|
|
}
|
|
} |