Files
chesscubing/ChessCubing.App/Pages/Authentication.razor

110 lines
3.0 KiB
Plaintext

@page "/authentication/{action}"
@inject NavigationManager Navigation
@inject IJSRuntime JS
<main class="rules-shell">
<section class="panel panel-wide cta-panel" style="margin-top: 2rem;">
<p class="eyebrow">Authentification</p>
@if (StartsInteractiveFlow)
{
<div>
<strong>@(IsRegisterAction ? "Ouverture de la creation de compte..." : "Ouverture de la connexion...")</strong>
<p>
@(IsRegisterAction
? "Le formulaire Keycloak s'ouvre dans cette fenetre integree."
: "Le formulaire de connexion Keycloak s'ouvre dans cette fenetre integree.")
</p>
</div>
}
else
{
<RemoteAuthenticatorView Action="@Action" />
}
</section>
</main>
@code {
[Parameter]
public string? Action { get; set; }
[SupplyParameterFromQuery(Name = "returnUrl")]
public string? ReturnUrl { get; set; }
[SupplyParameterFromQuery(Name = "embedded")]
public string? Embedded { get; set; }
private bool IsRegisterAction
=> string.Equals(Action, RemoteAuthenticationActions.Register, StringComparison.OrdinalIgnoreCase);
private bool IsLoginAction
=> string.Equals(Action, RemoteAuthenticationActions.LogIn, StringComparison.OrdinalIgnoreCase);
private bool IsEmbeddedFlow
=> string.Equals(Embedded, "1", StringComparison.OrdinalIgnoreCase)
|| string.Equals(Embedded, "true", StringComparison.OrdinalIgnoreCase);
private bool StartsInteractiveFlow
=> IsEmbeddedFlow && (IsLoginAction || IsRegisterAction);
protected override void OnParametersSet()
{
if (!StartsInteractiveFlow)
{
return;
}
var request = new InteractiveRequestOptions
{
Interaction = InteractionType.SignIn,
ReturnUrl = NormalizeReturnUrl(ReturnUrl)
};
if (IsRegisterAction)
{
request.TryAddAdditionalParameter("prompt", "create");
}
Navigation.NavigateToLogin("authentication/login", request);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
{
return;
}
var status = Action switch
{
RemoteAuthenticationActions.LogInCallback => "login-succeeded",
RemoteAuthenticationActions.LogOutCallback => "logout-succeeded",
_ => null,
};
if (status is null || !IsEmbeddedFlow)
{
return;
}
try
{
await JS.InvokeVoidAsync("chesscubingAuthModal.notifyParent", status);
}
catch
{
}
}
private static string NormalizeReturnUrl(string? returnUrl)
{
if (string.IsNullOrWhiteSpace(returnUrl))
{
return "/";
}
return returnUrl.StartsWith("/", StringComparison.Ordinal)
? returnUrl
: $"/{returnUrl}";
}
}