Skip to content

Commit 7855aef

Browse files
committed
Bonus points, change location.replace with Blazor.navigateTo for additional coolness
1 parent 9c88b58 commit 7855aef

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/Components/Blazor/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ private async Task ProcessLogIn(string returnUrl)
219219
case RemoteAuthenticationStatus.Redirect:
220220
break;
221221
case RemoteAuthenticationStatus.Success:
222-
Navigation.NavigateTo(returnUrl);
222+
await NavigateToReturnUrl(returnUrl);
223223
break;
224224
case RemoteAuthenticationStatus.Failure:
225225
var uri = Navigation.ToAbsoluteUri($"{ApplicationPaths.LogInFailedPath}?message={Uri.EscapeDataString(result.ErrorMessage)}").ToString();
226-
Navigation.NavigateTo(uri);
226+
await NavigateToReturnUrl(uri);
227227
break;
228228
case RemoteAuthenticationStatus.OperationCompleted:
229229
default:
@@ -248,7 +248,7 @@ private async Task ProcessLogInCallback()
248248
break;
249249
case RemoteAuthenticationStatus.Failure:
250250
var uri = Navigation.ToAbsoluteUri($"{ApplicationPaths.LogInFailedPath}?message={Uri.EscapeDataString(result.ErrorMessage)}").ToString();
251-
Navigation.NavigateTo(uri);
251+
await NavigateToReturnUrl(uri);
252252
break;
253253
default:
254254
throw new InvalidOperationException($"Invalid authentication result status '{result.Status}'.");
@@ -283,15 +283,15 @@ private async Task ProcessLogOut(string returnUrl)
283283
break;
284284
case RemoteAuthenticationStatus.Failure:
285285
var uri = Navigation.ToAbsoluteUri($"{ApplicationPaths.LogOutFailedPath}?message={Uri.EscapeDataString(result.ErrorMessage)}").ToString();
286-
Navigation.NavigateTo(uri);
286+
await NavigateToReturnUrl(uri);
287287
break;
288288
default:
289289
throw new InvalidOperationException($"Invalid authentication result status '{result.Status ?? "(null)"}'.");
290290
}
291291
}
292292
else
293293
{
294-
Navigation.NavigateTo(returnUrl);
294+
await NavigateToReturnUrl(returnUrl);
295295
}
296296
}
297297

@@ -311,7 +311,7 @@ private async Task ProcessLogOutCallback()
311311
break;
312312
case RemoteAuthenticationStatus.Failure:
313313
var uri = Navigation.ToAbsoluteUri($"{ApplicationPaths.LogOutFailedPath}?message={Uri.EscapeDataString(result.ErrorMessage)}").ToString();
314-
Navigation.NavigateTo(uri);
314+
await NavigateToReturnUrl(uri);
315315
break;
316316
default:
317317
throw new InvalidOperationException($"Invalid authentication result status '{result.Status ?? "(null)"}'.");
@@ -403,7 +403,7 @@ private string GetParameter(string key)
403403
return null;
404404
}
405405

406-
private async Task NavigateToReturnUrl(string returnUrl) => await JS.InvokeVoidAsync("location.replace", returnUrl);
406+
private async Task NavigateToReturnUrl(string returnUrl) => await JS.InvokeVoidAsync("Blazor.navigateTo", returnUrl, false, true);
407407

408408
private ValueTask RedirectToRegister()
409409
{

src/Components/Blazor/WebAssembly.Authentication/test/RemoteAuthenticatorCoreTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task AuthenticationManager_Login_NavigatesToReturnUrlOnSuccess()
5656
await renderer.Dispatcher.InvokeAsync<object>(() => remoteAuthenticator.SetParametersAsync(parameters));
5757

5858
// Assert
59-
Assert.Equal("https://www.example.com/base/fetchData", remoteAuthenticator.Navigation.Uri);
59+
Assert.Equal("https://www.example.com/base/fetchData", jsRuntime.LastInvocation.args[0]);
6060
}
6161

6262
[Fact]
@@ -109,7 +109,7 @@ public async Task AuthenticationManager_Login_NavigatesToLoginFailureOnError()
109109
// Assert
110110
Assert.Equal(
111111
"https://www.example.com/base/authentication/login-failed?message=There was an error trying to log in",
112-
remoteAuthenticator.Navigation.Uri);
112+
jsRuntime.LastInvocation.args[0]);
113113

114114
}
115115

@@ -220,7 +220,7 @@ public async Task AuthenticationManager_LoginCallback_NavigatesToLoginFailureOnE
220220
// Assert
221221
Assert.Equal(
222222
"https://www.example.com/base/authentication/login-failed?message=There was an error trying to log in",
223-
remoteAuthenticator.Navigation.Uri);
223+
jsRuntime.LastInvocation.args[0]);
224224

225225
}
226226

@@ -358,7 +358,7 @@ public async Task AuthenticationManager_Logout_NavigatesToLogoutFailureOnError()
358358
// Assert
359359
Assert.Equal(
360360
"https://www.example.com/base/authentication/logout-failed?message=There was an error trying to log out",
361-
remoteAuthenticator.Navigation.Uri);
361+
jsRuntime.LastInvocation.args[0]);
362362
}
363363

364364
[Fact]
@@ -468,7 +468,7 @@ public async Task AuthenticationManager_LogoutCallback_NavigatesToLoginFailureOn
468468
// Assert
469469
Assert.Equal(
470470
"https://www.example.com/base/authentication/logout-failed?message=There was an error trying to log out",
471-
remoteAuthenticator.Navigation.Uri);
471+
jsRuntime.LastInvocation.args[0]);
472472

473473
}
474474

src/Components/Web.JS/src/Services/NavigationManager.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,40 @@ export function attachToEventDelegator(eventDelegator: EventDelegator) {
7272
});
7373
}
7474

75-
export function navigateTo(uri: string, forceLoad: boolean) {
75+
export function navigateTo(uri: string, forceLoad: boolean, replace: boolean = false) {
7676
const absoluteUri = toAbsoluteUri(uri);
7777

7878
if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) {
7979
// It's an internal URL, so do client-side navigation
80-
performInternalNavigation(absoluteUri, false);
80+
performInternalNavigation(absoluteUri, false, replace);
8181
} else if (forceLoad && location.href === uri) {
8282
// Force-loading the same URL you're already on requires special handling to avoid
8383
// triggering browser-specific behavior issues.
8484
// For details about what this fixes and why, see https://github.com/aspnet/AspNetCore/pull/10839
8585
const temporaryUri = uri + '?';
8686
history.replaceState(null, '', temporaryUri);
8787
location.replace(uri);
88+
} else if (replace){
89+
history.replaceState(null, '', absoluteUri)
8890
} else {
8991
// It's either an external URL, or forceLoad is requested, so do a full page load
9092
location.href = uri;
9193
}
9294
}
9395

94-
function performInternalNavigation(absoluteInternalHref: string, interceptedLink: boolean) {
96+
function performInternalNavigation(absoluteInternalHref: string, interceptedLink: boolean, replace: boolean = false) {
9597
// Since this was *not* triggered by a back/forward gesture (that goes through a different
9698
// code path starting with a popstate event), we don't want to preserve the current scroll
9799
// position, so reset it.
98100
// To avoid ugly flickering effects, we don't want to change the scroll position until the
99101
// we render the new page. As a best approximation, wait until the next batch.
100102
resetScrollAfterNextBatch();
101103

102-
history.pushState(null, /* ignored title */ '', absoluteInternalHref);
104+
if(!replace){
105+
history.pushState(null, /* ignored title */ '', absoluteInternalHref);
106+
}else{
107+
history.replaceState(null, /* ignored title */ '', absoluteInternalHref);
108+
}
103109
notifyLocationChanged(interceptedLink);
104110
}
105111

0 commit comments

Comments
 (0)