Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 33ba1ca

Browse files
committedOct 5, 2024
feat(auth): Add ExtraQueryParams to AuthSignInWithRedirectInput
1 parent a0d1447 commit 33ba1ca

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
 

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ describe('signInWithRedirect', () => {
176176
expect(mockUrlSafeEncode).toHaveBeenCalledWith(expectedCustomState);
177177
});
178178

179+
it('uses extra query parameters when specified', async () => {
180+
const expectedDefaultProvider = 'COGNITO';
181+
const exptedParamKey = 'customParam';
182+
const expectedParamValue = 'object';
183+
await signInWithRedirect({
184+
extraQueryParams: { customParam: expectedParamValue },
185+
});
186+
const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
187+
expect(oauthUrl).toStrictEqual(
188+
`https://oauth.domain.com/oauth2/authorize?redirect_uri=https%3A%2F%2Fproxy.goincop1.workers.dev%3A443%2Fhttp%2Flocalhost%3A3000%2F&response_type=code&client_id=userPoolClientId&identity_provider=${expectedDefaultProvider}&scope=phone%20email%20openid%20profile%20aws.cognito.signin.user.admin&state=oauth_state&code_challenge=code_challenge&code_challenge_method=S256&${exptedParamKey}=${expectedParamValue}`,
189+
);
190+
});
191+
179192
describe('specifications on Web', () => {
180193
describe('side effect', () => {
181194
it('attaches oauth listener to the Amplify singleton', async () => {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '../utils/oauth';
2626
import { createOAuthError } from '../utils/oauth/createOAuthError';
2727
import { listenForOAuthFlowCancellation } from '../utils/oauth/cancelOAuthFlow';
28+
import { ExtraQueryParameters } from '../../../types/inputs';
2829

2930
/**
3031
* Signs in a user with OAuth. Redirects the application to an Identity Provider.
@@ -57,6 +58,7 @@ export async function signInWithRedirect(
5758
provider,
5859
customState: input?.customState,
5960
preferPrivateSession: input?.options?.preferPrivateSession,
61+
extraQueryParams: input?.extraQueryParams,
6062
});
6163
}
6264

@@ -66,12 +68,14 @@ const oauthSignIn = async ({
6668
clientId,
6769
customState,
6870
preferPrivateSession,
71+
extraQueryParams,
6972
}: {
7073
oauthConfig: OAuthConfig;
7174
provider: string;
7275
clientId: string;
7376
customState?: string;
7477
preferPrivateSession?: boolean;
78+
extraQueryParams?: ExtraQueryParameters;
7579
}) => {
7680
const { domain, redirectSignIn, responseType, scopes } = oauthConfig;
7781
const randomState = generateState();
@@ -104,6 +108,7 @@ const oauthSignIn = async ({
104108
code_challenge: toCodeChallenge(),
105109
code_challenge_method: method,
106110
}),
111+
...extraQueryParams,
107112
})
108113
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
109114
.join('&');

‎packages/auth/src/types/inputs.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ export interface AuthSignOutInput {
5454

5555
export type AuthProvider = 'Amazon' | 'Apple' | 'Facebook' | 'Google';
5656

57+
export type OAuthUrlReservedKeys =
58+
| 'redirect_uri'
59+
| 'response_type'
60+
| 'client_id'
61+
| 'identity_provider'
62+
| 'scope'
63+
| 'state'
64+
| 'responseType';
65+
66+
/**
67+
* User-defined custom query params for oAuthUrl.
68+
*/
69+
export type ExtraQueryParameters = Omit<
70+
Record<string, string>,
71+
OAuthUrlReservedKeys
72+
>;
73+
5774
export interface AuthSignInWithRedirectInput {
5875
provider?: AuthProvider | { custom: string };
5976
customState?: string;
@@ -69,6 +86,7 @@ export interface AuthSignInWithRedirectInput {
6986
*/
7087
preferPrivateSession?: boolean;
7188
};
89+
extraQueryParams?: ExtraQueryParameters;
7290
}
7391

7492
/**

0 commit comments

Comments
 (0)
Please sign in to comment.