Redirect to page after login in Angular Guard
See original GitHub issueI have the following angular-oauth2-oidc configuration (and I am using Identity Server 4):
import { Component } from '@angular/core';
import { AuthConfig, OAuthService, JwksValidationHandler } from 'angular-oauth2-oidc';
export const authConfig: AuthConfig = {
issuer: 'https://localhost:5005',
redirectUri: window.location.origin + "/index.html",
clientId: 'spa',
responseType: 'code',
scope: 'openid profile email offline_access api',
showDebugInformation: true,
clearHashAfterLogin: false
};
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
constructor(private authService: OAuthService) {
this.authService.configure(authConfig);
this.authService.tokenValidationHandler = new JwksValidationHandler();
this.authService.loadDiscoveryDocumentAndTryLogin();
}
}
And I created the Angular’s 7 Guard:
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { OAuthService, OAuthEvent } from 'angular-oauth2-oidc';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AuthenticatedGuard implements CanActivate {
constructor(private router: Router, private authService: OAuthService) { }
canActivate(router: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authService.hasValidIdToken() && this.authService.hasValidAccessToken())
return true;
this.authService.events.subscribe(({ type }: OAuthEvent) => {
switch (type) {
case 'token_received':
return this.router.navigate(['/protected-route']); // How to get the actually route?
}
});
this.authService.initLoginFlow();
return false;
}
}
If I try to access a protected route without being authenticated I am redirected to login.
After successful login I am redirected to Angular’s home page but not to the protected route I tried to access …
How to get redirected, after login, to the protected route that I tried to access before?
Shouldn’t I get the route from ReturnUrl parameter?
Should not exist a auth-callback route on Angular’s component to handle this?
I am not sure about the best way to do this.
Thank you
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top Results From Across the Web
Angular 2 - Redirect to Previous URL after Login with Auth ...
2) Navigate to the login component by clicking in a link - not by typing the URL in the browser bar. Don't log...
Read more >Angular2 - Redirect to calling url after successful login
One possible way to achieve this is by using your AuthGuard to check for your login status and store the url on your...
Read more >Redirect to Previous URL after Login with Auth Guard | RNaura
In this post I'll show you how to redirect a user back to their originally requested url / route after logging into an...
Read more >Guards and Login Redirects in Angular - Gnome on the run
Redirect the user to the page they landed on before being forced to login ... In your web application, you likely require a...
Read more >Better Redirects in Angular Route Guards | juri.dev
Learn how to properly redirect in an Angular route guard.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could see if my example implementation for this feature is useful to you?
Things went a little quiet here (on my end too). I’m gonna close for now, please track #592 or if you feel this is different post a full (but preferably minimal) repro e.g. on StackBlitz so we can further investigate.