Skip to content

Amplify Auth not handling authorization code from Entra redirect (manual token exchange required)

0

I’ve integrated Entra with Cognito. When I click on my app for SSO, it redirects to my login page with an authorization code in the URL.

On the login page, I currently have to manually extract the code from the URL and make an Axios call to the Cognito endpoint to exchange it for access, refresh, and ID tokens.

I tried using signInWithRedirect, but it’s not working as expected—it prompts me to log in again instead of handling the existing authorization code.

Is there any built-in method in Amplify Auth or amplify/ui-angular that can automatically handle this code exchange and token retrieval, instead of manually calling the Cognito API every time?

asked 19 days ago49 views
1 Answer
4
Accepted Answer

"The short answer is: Amplify is designed for SP-initiated flows, meaning the process must start within your app via signInWithRedirect(). Amplify won't automatically 'pick up' a code it didn't request itself due to OAuth2 security standards (PKCE)."

When you arrive at your app with a code already in the URL (likely an IdP-initiated flow from Entra), Amplify’s internal OAuth handler ignores it because it lacks the corresponding 'state' and 'PKCE verifier' that it would have created if it had started the request itself.

Is there a built-in method?

Not specifically for a 'pre-existing' code. However, you have two options to avoid manual Axios calls:

  1. The 'Amplify Way' (Recommended): Don't redirect from Entra with a code directly. Instead, redirect the user to your app's login page and call signInWithRedirect({ provider: 'IdentityServer' }) (your Entra provider name). This ensures Amplify manages the PKCE flow and token exchange automatically.

  2. The Custom Hub Listener: If you must handle an external code, you are already doing the 'correct' workaround by exchanging it manually. Amplify's Auth category doesn't have a handleCode(code) method because it needs to validate the session state for security.

see also:

EXPERT
answered 19 days ago
EXPERT
reviewed 19 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.