Okta AuthJS, autorization code flow

Hello,
I’m a little bit stuck with the Okta AuthJS redirection.

We use the Authorization Code flow and a call from an app:

The app makes an Autorize call that triggers the display of the page developed with okta authJs on the widget.
But to redirect to the app with a code, I tried many methods without success:

authClient.signInWithCredential(…).then …:

  • authClient.session.setCookieAndRedirect(transaction.sessionToken, “https://www.myapp.com/gp/mobile/oauth”);
  • authClient.session.setCookieAndRedirect(config.redirectUri);
  • authClient.token.getWithRedirect({sessionToken: transaction.sessionToken});

But I still can’t redirect to the provider with a code and finish the flow initiated by it.

I also tried with getWithRedirect and responseType= code, but it triggers an autorize with a code in return, but this one is already used to get the tokens.

Thanks
Sébastien

Not sure if i understand your question correctly.

I am using just authClient.token.getWithRedirect, it will be redirected to Okta Hosted login page, the page will check if there is a valid session.

  • If yes, it will return the code for you to exchange for token.
  • If no, it will present the login widget and return code once you have performed login successfully.

Once you got the code, you just need to exchange for token using the code and save to your storage.
GitHub -redirect and routing

if (authClient.token.isLoginRedirect()) {
          var token = await authClient.token.parseFromUrl();

          // Save token to localstorage.
          authClient.tokenManager.setTokens(token.tokens);
          ...
          window.location.href = "https://myMainPage.com";
} else {
         authClient.token.getWithRedirect({
            responseType: ["id_token", "token"]
          });
}

Take note - setCookieAndRedirect - Require access to Third Party Cookies
(could be the cause of failed redirect. :smiley: )
image

2 Likes