[Blazor][Wasm] Add missing state callbacks (#19405)

* Adds missing callback when logging in and logging out successfully
This commit is contained in:
Javier Calvarro Nelson 2020-02-27 09:48:48 -08:00 committed by GitHub
parent eed3605ae5
commit 2b72b637bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -74,6 +74,16 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
/// </summary>
[Parameter] public RenderFragment LogOutSucceeded { get; set; } = DefaultLoggedOutFragment;
/// <summary>
/// Gets or sets an event callback that will be invoked with the stored authentication state when a log in operation succeeds.
/// </summary>
[Parameter] public EventCallback<TAuthenticationState> OnLogInSucceeded { get; set; }
/// <summary>
/// Gets or sets an event callback that will be invoked with the stored authentication state when a log out operation succeeds.
/// </summary>
[Parameter] public EventCallback<TAuthenticationState> OnLogOutSucceeded { get; set; }
/// <summary>
/// Gets or sets the <see cref="IJSRuntime"/> to use for performin JavaScript interop.
/// </summary>
@ -242,6 +252,10 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
// is when we are doing a redirect sign in flow.
throw new InvalidOperationException("Should not redirect.");
case RemoteAuthenticationStatus.Success:
if (OnLogInSucceeded.HasDelegate)
{
await OnLogInSucceeded.InvokeAsync(result.State);
}
await NavigateToReturnUrl(GetReturnUrl(result.State));
break;
case RemoteAuthenticationStatus.OperationCompleted:
@ -305,6 +319,10 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
// is when we are doing a redirect sign in flow.
throw new InvalidOperationException("Should not redirect.");
case RemoteAuthenticationStatus.Success:
if (OnLogOutSucceeded.HasDelegate)
{
await OnLogOutSucceeded.InvokeAsync(result.State);
}
await NavigateToReturnUrl(GetReturnUrl(result.State, Navigation.ToAbsoluteUri(ApplicationPaths.LogOutSucceededPath).ToString()));
break;
case RemoteAuthenticationStatus.OperationCompleted:

View File

@ -21,6 +21,8 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
public class RemoteAuthenticatorCoreTests
{
private const string _action = nameof(RemoteAuthenticatorViewCore<RemoteAuthenticationState>.Action);
private const string _onLogInSucceded = nameof(RemoteAuthenticatorViewCore<RemoteAuthenticationState>.OnLogInSucceeded);
private const string _onLogOutSucceeded = nameof(RemoteAuthenticatorViewCore<RemoteAuthenticationState>.OnLogOutSucceeded);
[Fact]
public async Task AuthenticationManager_Throws_ForInvalidAction()
@ -183,9 +185,14 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
State = remoteAuthenticator.AuthenticationState
});
var loggingSucceededCalled = false;
var parameters = ParameterView.FromDictionary(new Dictionary<string, object>
{
[_action] = RemoteAuthenticationActions.LogInCallback
[_action] = RemoteAuthenticationActions.LogInCallback,
[_onLogInSucceded] = new EventCallbackFactory().Create< RemoteAuthenticationState>(
remoteAuthenticator,
(state) => loggingSucceededCalled = true),
});
// Act
@ -193,6 +200,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
// Assert
Assert.Equal(fetchDataUrl, jsRuntime.LastInvocation.args[0]);
Assert.True(loggingSucceededCalled);
}
@ -431,9 +439,14 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
State = remoteAuthenticator.AuthenticationState
});
var loggingOutSucceededCalled = false;
var parameters = ParameterView.FromDictionary(new Dictionary<string, object>
{
[_action] = RemoteAuthenticationActions.LogOutCallback
[_action] = RemoteAuthenticationActions.LogOutCallback,
[_onLogOutSucceeded] = new EventCallbackFactory().Create<RemoteAuthenticationState>(
remoteAuthenticator,
(state) => loggingOutSucceededCalled = true),
});
// Act
@ -441,6 +454,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
// Assert
Assert.Equal(fetchDataUrl, jsRuntime.LastInvocation.args[0]);
Assert.True(loggingOutSucceededCalled);
}