Retrieving state data with oidc-client
Asked Answered
G

1

13

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!

Garrard answered 25/1, 2018 at 15:31 Comment(0)
H
20

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.

Hydrops answered 25/1, 2018 at 16:26 Comment(1)
Ahh! There it is, on the user object! Thanks :-)Garrard

© 2022 - 2024 — McMap. All rights reserved.