Skip to content

Commit 24ab070

Browse files
authored
feat: add support for idp_identifier query parameter when going through OAuth using signInWithRedirect function (#14573)
1 parent defd1d3 commit 24ab070

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

packages/auth/__tests__/providers/cognito/signInWithRedirect.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ describe('signInWithRedirect', () => {
192192
);
193193
});
194194

195+
it('uses idpIdentifier when specified', async () => {
196+
const expectedIdpIdentifier = 'example.com';
197+
await signInWithRedirect({
198+
provider: { idpIdentifier: expectedIdpIdentifier },
199+
});
200+
const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
201+
expect(oauthUrl).toStrictEqual(
202+
`https://oauth.domain.com/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&idp_identifier=${expectedIdpIdentifier}&scope=phone+email+openid+profile+aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256`,
203+
);
204+
});
205+
195206
it('uses custom state if specified', async () => {
196207
const expectedCustomState = 'verify_me';
197208
await signInWithRedirect({ customState: expectedCustomState });

packages/auth/src/providers/cognito/apis/signInWithRedirect.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,21 @@ export async function signInWithRedirect(
5151
}
5252

5353
let provider = 'COGNITO'; // Default
54+
let idpIdentifier: string | undefined;
5455

5556
if (typeof input?.provider === 'string') {
5657
provider = cognitoHostedUIIdentityProviderMap[input.provider];
5758
} else if (input?.provider?.custom) {
5859
provider = input.provider.custom;
60+
} else if (input?.provider?.idpIdentifier) {
61+
({ idpIdentifier } = input.provider);
5962
}
6063

6164
return oauthSignIn({
6265
oauthConfig: authConfig.loginWith.oauth,
6366
clientId: authConfig.userPoolClientId,
6467
provider,
68+
idpIdentifier,
6569
customState: input?.customState,
6670
preferPrivateSession: input?.options?.preferPrivateSession,
6771
options: {
@@ -77,6 +81,7 @@ export async function signInWithRedirect(
7781
const oauthSignIn = async ({
7882
oauthConfig,
7983
provider,
84+
idpIdentifier,
8085
clientId,
8186
customState,
8287
preferPrivateSession,
@@ -85,6 +90,7 @@ const oauthSignIn = async ({
8590
}: {
8691
oauthConfig: OAuthConfig;
8792
provider: string;
93+
idpIdentifier?: string;
8894
clientId: string;
8995
customState?: string;
9096
preferPrivateSession?: boolean;
@@ -117,10 +123,17 @@ const oauthSignIn = async ({
117123
['redirect_uri', redirectUri],
118124
['response_type', responseType],
119125
['client_id', clientId],
120-
['identity_provider', provider],
121-
['scope', scopes.join(' ')],
122126
]);
123127

128+
// Add either identity_provider or idp_identifier, but not both
129+
if (idpIdentifier) {
130+
params.append('idp_identifier', idpIdentifier);
131+
} else {
132+
params.append('identity_provider', provider);
133+
}
134+
135+
params.append('scope', scopes.join(' '));
136+
124137
loginHint && params.append('login_hint', loginHint);
125138
lang && params.append('lang', lang);
126139
nonce && params.append('nonce', nonce);

packages/auth/src/types/inputs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ export type AuthProvider = 'Amazon' | 'Apple' | 'Facebook' | 'Google';
6969
export type AuthPrompt = 'NONE' | 'LOGIN' | 'CONSENT' | 'SELECT_ACCOUNT';
7070

7171
export interface AuthSignInWithRedirectInput {
72-
provider?: AuthProvider | { custom: string };
72+
provider?:
73+
| AuthProvider
74+
| { custom: string; idpIdentifier?: never }
75+
| { idpIdentifier: string; custom?: never };
7376
customState?: string;
7477
options?: {
7578
/**

0 commit comments

Comments
 (0)