내용으로 건너뛰기

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?

질문됨 2달 전53회 조회
1개 답변
4
수락된 답변

"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:

전문가
답변함 2달 전
전문가
검토됨 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

관련 콘텐츠