Skip to content

Redirect to original route after login #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stanleygu opened this issue Oct 2, 2014 · 11 comments
Open

Redirect to original route after login #615

stanleygu opened this issue Oct 2, 2014 · 11 comments
Labels
Milestone

Comments

@stanleygu
Copy link

If someone who is not authenticated yet initially requests an authenticated route, e.g. /settings, how would it be possible to redirect to the original route after login?

@andrewstuart
Copy link
Member

👍 from me. Or if my token times out while I'm at some view, I'd prefer to either not have to leave the view I'm in (popup login) or at least be taken back after login, since I think OAuth redirection makes a popup hard/impossible.

@kingcody
Copy link
Member

kingcody commented Oct 2, 2014

If you're ok with an actual popup window you can do something like this:

OAuth Popup

@kingcody
Copy link
Member

kingcody commented Oct 2, 2014

@stanleygu in the canary branch, take a look at app/templates/client/app/account(auth)/account(js).js; specifically the logout controller and also the $rootScope.$on('$routeChangeStart', fn) or $rootScope.$on('$stateChangeStart', fn) (whether your doing ui-router or not) that should give you an idea of one way it can be done.

@stanleygu
Copy link
Author

I couldn't figure out how to trigger the route change from the popup.

I ended up using a workaround for now where I pass the original route as a query parameter in the callbackURL during passport auth, which comes back and I do the redirect at that point.

e.g.

callbackURL = config.WEB_HOST + '/auth/github/callback?referrer=' + encodeURI(req.query.referrer);

@hiromitz
Copy link
Contributor

hiromitz commented Oct 9, 2014

I think you can change redirect url In server/auth/auth.service.js.

https://github.com/DaftMonk/generator-angular-fullstack/blob/master/app/templates/server/auth(auth)/auth.service.js#L70

@stanleygu
Copy link
Author

@hiromitz, yep that is where I ended up doing my redirect:

  if (req.query.referrer) {
    res.redirect(req.query.referrer);
  } else {
    res.redirect('/');
  }

However, since that call comes from the OAuth server, the challenge was saving the original destination URL. I ended up sending the original URL to the OAuth server for it to include in the callback URL as a query string, which then gets read inside auth.service.js. Seems kludgey, there is probably a better way to do it.

@azachar
Copy link

azachar commented Oct 16, 2014

@stanleygu can you provide your solution as a PR? Thank you.

@kingcody
Copy link
Member

@stanleygu that's probably how I'd do it as well. IMHO OAuth itself is "kludgey" so you end up having to do some odd things at times.

@azachar
Copy link

azachar commented Nov 26, 2014

Hey Guys,
I did the same effect on the client side only.

I used ngStorage library to store a requested page to $sessionStorage and UI-Route's state change filtering to restore the page after login or after any other page was loaded.

I can provide a PR if there is any interest.

Cheers,
A.

@JaKXz JaKXz added the question label Mar 5, 2015
@dcoffey3296
Copy link

Hey @azachar @stanleygu, I am trying to solve the same thing. I am not clear on where I'd capture the originally requested url, where I'd store it, and where to place the redirect once the user logs in. Either of you up for providing a gist? Anyone else have an example? I asked on Stack Overflow but no solution yet... http://stackoverflow.com/questions/28870430/redirect-to-original-request-after-authentication-angular-fullstack

@Awk34 Awk34 added this to the 2.4.0 milestone Jul 16, 2015
@yeaske
Copy link

yeaske commented Sep 17, 2019

An option using state:

Before login:

     $rootScope.$on('$stateChangeStart', function (event, next) {
      Auth.isLoggedIn(function (loggedIn) {
        if (next.authenticate && !loggedIn) {
          if ($location.url() != '/login') {
            $cookieStore.put('redirectState', next.name);
          }
          $location.path('/login');
        }
      });
    });

After login:

this.$state.go(this.$cookieStore.get('redirectState'));
this.$cookieStore.remove('redirectState');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants