Integrer l'authentification Keycloak dans l'application
This commit is contained in:
@@ -1,109 +1,16 @@
|
||||
@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" />
|
||||
}
|
||||
<div>
|
||||
<strong>Le systeme d'authentification est maintenant integre a l'application.</strong>
|
||||
<p>Utilise les boutons du menu en haut de page pour te connecter ou creer un compte sans quitter le site.</p>
|
||||
</div>
|
||||
</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}";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user