Retrieving state data with oidc-client

Larsi picture Larsi · Jan 25, 2018 · Viewed 8k times · Source

How to keep the original url that the user was navigating to? Say I have an unauthenticated user navigates to http://localhost:9000/customer/123

To authenticate the user I would do a:

// in my app.js  
new Oidc.UserManager().signinRedirect({state:'customer/123'}); // need a way to keep this url

When that returns to the callback.html where I need a way of going to the origianal url:

// callback.html
<script src="oidc-client.js"></script>
<script>
    Oidc.Log.logger = console;
    new Oidc.UserManager().signinRedirectCallback().then(function () {

        var state = 'customer/123' // how to do a redirect to the page originally requested page?
        window.location.href="http://localhost:9000/ + state 
    }).catch(function (e) {
        console.error(e);
    });
</script>

Or maybe there are other build in ways of getting the origianl url?

Thanks for any help!

Answer

m3n7alsnak3 picture m3n7alsnak3 · Jan 25, 2018

Your approach is good. You sign in the way you are doing it, and then in the callback, slightly modify your code (use the user object that is returned):

new Oidc.UserManager().signinRedirectCallback().then(function (user){
    var url = user.state;
    //more code here
}

The state will be part of the user object, and will have the value that you have submited. And you can redirect to wherever you want.