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.

Impossible to redirect to previous page after logging in

See original GitHub issue

Hello!

I am using this library for an oauth protected application. Using an auth-guard, the secured pages trigger a login page which calls an authservice configured as in the code snippet.

Somehow it seems impossible to redirect to the chosen page after logging in, as only one redirect can be set. Anyone an idea how to configure it? Listening to the event “token_received” does not seem to work.

export class AuthService {

  constructor(
    private router: Router,
    private oauthService: OAuthService
  ) {
    this.oauthService.configure(authConfig);
    this.oauthService.setupAutomaticSilentRefresh();

    this.oauthService.tokenValidationHandler = new JwksValidationHandler();
    this.oauthService.tryLogin({});

  }

  login() {
    this.oauthService.initImplicitFlow();
  }

}```

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18

github_iconTop GitHub Comments

7reactions
LeonsBuntiscommented, Oct 22, 2018

@ggjersund I’ve had similar issue, while trying to redirect user back to requested url on onTokenReceived event. Fixed it by setting clearHashAfterLogin setting to false, therefore the router is not triggered on location.hash = '';, so that this.router.navigateByUrl works correctly.

7reactions
jeroenheijmanscommented, Sep 11, 2018

I’m using the same library, and in my production application I have a flow similar to what you describe (which I documented in a toy-problem version in a sample repo). It’s still on my to do list to add the feature you describe (navigate to the intended page after getting back from the ID Server, right?) to my production application, but I have played around with it a bit.

I also found that responding to events like you mention does not work for this feature. I’m guessing the implementation of state upon returning is lagging behind the library’s move towards events. But that’s a guess.

As for a solution, I found that this can work:

Send the intended url along with the user when they go to the ID Server (you might need to do this in the auth guard based on the target URL instead of the current URL):

public login() { this.oauthService.initImplicitFlow(encodeURIComponent(this.router.url)); }

PS. The encodeURIComponent is because of #415 (e.g. ? characters get lost otherwise).

Then when the user gets back you cannot use the event as you noted, but I found that this does work:

this.oauthService.loadDiscoveryDocument()
  .then(() => this.oauthService.tryLogin({
    onTokenReceived: () => this.router.navigateByUrl(this.oauthService.state),
  }))
  // etc.

Edit: see updated/improved solution below.

Hope that helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

redirect user to previous page after login - php - Stack Overflow
You should first get user refer page in a variable using $_SERVER['HTTP_REFERER']; in your login page.
Read more >
How to redirect users back to previous page, after logging in?
This can be done by specifying a Return URL in the form URL by providing a value to Source querystring. For example:.
Read more >
Redirect to previous page after login - WordPress.org
I want visitor to remain on the same page after login or register. Is it possible to accomplish by any hook? The page...
Read more >
Login redirect to previous page - Auth0 Community
Unfortunately rules can't do this for you. Rules execute on the server side of Auth0 and by the time wordpress redirects you away...
Read more >
After Login Redirect To Previous Page In WordPress
Sometimes, It is required to redirect users back to the page they were viewing before logging in. There could be a plugin for...
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