question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect to page after login in Angular Guard

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
jeroenheijmanscommented, Aug 1, 2019

You could see if my example implementation for this feature is useful to you?

0reactions
jeroenheijmanscommented, Mar 8, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found