Okta Sign-in Widget: run a hook after the Okta session is created

My use-case is that I want to forward the Okta user ID to user analytics once the user has signed in, and manage this from the Okta side, to avoid needing to implement the same functionality across all SSO apps. The user signs into to an OIE, okta-hosted widget.

I’ve tried using the hooks functionality mentioned here: okta/okta-signin-widget: HTML/CSS/JS widget that provides out-of-the-box authentication UX for your organization’s apps (github.com)

Currently I have a post-event hook configured to run after the success-redirect event. The hook attempts to call the Sessions API to learn some details about the signed in user. However, this event appears to run before the okta widget exchanges its stateToken for a session cookie, and so the call fails.

Is there a later event I can place a hook on, or can I place some Javascript in the interstitial page itself to collect some user analytics?

var config = OktaUtil.getSignInWidgetConfig();

config.hooks = {
    'success-redirect': {
        after: [
            async function afterSuccessRedirect() {
                return new Promise((resolve, reject) => {
                    console.log("HOOK: AFTER SUCCESS REDIRECT");
                    let xhr = new XMLHttpRequest();
                    xhr.open("GET", "https://<okta_custom_domain>.com/api/v1/sessions/me");

                    xhr.onload = (event) => {
                        console.log("HOOK ASR Done");
                        console.log(xhr.responseText);
                        resolve();
                    }

                    xhr.onerror = (event) => {
                        console.log("HOOK ASR XHR Error ", event);
                        resolve();
                    }
                    xhr.send();
                });
            }
        ]
    }
}

Hi,
I think you could use the afterRender hook to trigger your code at a more specific moment with the help of controllers.

Example :

signIn.on(‘afterRender’, function (context) {
console.log(context.controller)
if (context.controller !== " ‘’) {
return;
}
});

With the console.log for context, by doing a full login process, you should be able to determine at which step you want your code to be triggered. Once the good controller found, you put it in the if condition, and then it should do the trick.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.