Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after oidc-client login

I have a asp.net core app with a angular 2 running on it. I'm using the oidc-client.js library to handle logging in. Currently, if the user is already logged in and follows a link to the site, the auth happens properly and the user is taken to the correct page. However, if they're not logged in, the get bounced to the login page just fine, get taken back to the site with the proper url, but eventually get taken to the /auth-callback page. At that point, the redirect url gets lost and they wind up at the default route of the site after auth-callback.

What I'm looking for is if there's any options in the oidc-client library that I can use to persist the correct url after the auth-callback call? Digging through the documentation and examples, nothing jumped out at me. Or am I going to have to put something together myself?

like image 540
Arthurdent510 Avatar asked Dec 06 '17 15:12

Arthurdent510


2 Answers

In your client code when you call signinRedirect you could abuse the state property with something to use later (this is not what it's intended for) e.g new Oidc.UserManager().signinRedirect({state:window.location.href});.

Then in your callback page you can use it:

mgr
  .signinRedirectCallback()
  .then(function (user) {
    window.history.replaceState({}, window.document.title, window.location.origin + window.location.pathname);

    window.location = user.state || "/";
  });
like image 61
Antony Denyer Avatar answered Nov 15 '22 15:11

Antony Denyer


well you have to create a separate html page in your assets to handle getting the tokens and store them after login redirect

  1. login success on your openID provider
  2. redirect to your html static page
  3. your page will get the token and store them in localStorage for example
  4. when tokens are ready, your static html page will redirect to your angular app root

you can create redirect_page.html in /assets folder

  <script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.4.1/oidc-client.min.js"></script>
  <script>
   var config = {
    userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })
   };
   var mgr = new Oidc.UserManager(config);
   mgr.signinRedirectCallback().then(() => {
    window.history.replaceState({},
        window.document.title,
        window.location.origin);
        window.location = "/";
    }, error => {
    console.error(error);
   });

  </script>

and set your userManager config

var config = {
  ...
  redirect_uri: `${clientRoot}/assets/redirect_page.html`,
}
like image 35
Fateh Mohamed Avatar answered Nov 15 '22 16:11

Fateh Mohamed