diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs b/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs index 1317bffe55..c62c24e1e3 100644 --- a/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs +++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs @@ -74,6 +74,16 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication /// [Parameter] public RenderFragment LogOutSucceeded { get; set; } = DefaultLoggedOutFragment; + /// + /// Gets or sets an event callback that will be invoked with the stored authentication state when a log in operation succeeds. + /// + [Parameter] public EventCallback OnLogInSucceeded { get; set; } + + /// + /// Gets or sets an event callback that will be invoked with the stored authentication state when a log out operation succeeds. + /// + [Parameter] public EventCallback OnLogOutSucceeded { get; set; } + /// /// Gets or sets the to use for performin JavaScript interop. /// @@ -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: diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs b/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs index f9c98cbed6..6f793eff79 100644 --- a/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs +++ b/src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs @@ -21,6 +21,8 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication public class RemoteAuthenticatorCoreTests { private const string _action = nameof(RemoteAuthenticatorViewCore.Action); + private const string _onLogInSucceded = nameof(RemoteAuthenticatorViewCore.OnLogInSucceeded); + private const string _onLogOutSucceeded = nameof(RemoteAuthenticatorViewCore.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 { - [_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 { - [_action] = RemoteAuthenticationActions.LogOutCallback + [_action] = RemoteAuthenticationActions.LogOutCallback, + [_onLogOutSucceeded] = new EventCallbackFactory().Create( + 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); }