Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): Add ExtraQueryParams to AuthSignInWithRedirectInput #13892

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ describe('signInWithRedirect', () => {
expect(mockUrlSafeEncode).toHaveBeenCalledWith(expectedCustomState);
});

it('uses extra query parameters when specified', async () => {
const expectedDefaultProvider = 'COGNITO';
const exptedParamKey = 'customParam';
const expectedParamValue = 'object';
await signInWithRedirect({
extraQueryParams: { customParam: expectedParamValue },
});
const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toStrictEqual(
`https://oauth.domain.com/oauth2/authorize?redirect_uri=http%3A%2F%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}`,
);
});

describe('specifications on Web', () => {
describe('side effect', () => {
it('attaches oauth listener to the Amplify singleton', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../utils/oauth';
import { createOAuthError } from '../utils/oauth/createOAuthError';
import { listenForOAuthFlowCancellation } from '../utils/oauth/cancelOAuthFlow';
import { ExtraQueryParameters } from '../../../types/inputs';

/**
* Signs in a user with OAuth. Redirects the application to an Identity Provider.
Expand Down Expand Up @@ -57,6 +58,7 @@ export async function signInWithRedirect(
provider,
customState: input?.customState,
preferPrivateSession: input?.options?.preferPrivateSession,
extraQueryParams: input?.extraQueryParams,
});
}

Expand All @@ -66,12 +68,14 @@ const oauthSignIn = async ({
clientId,
customState,
preferPrivateSession,
extraQueryParams,
}: {
oauthConfig: OAuthConfig;
provider: string;
clientId: string;
customState?: string;
preferPrivateSession?: boolean;
extraQueryParams?: ExtraQueryParameters;
}) => {
const { domain, redirectSignIn, responseType, scopes } = oauthConfig;
const randomState = generateState();
Expand Down Expand Up @@ -104,6 +108,7 @@ const oauthSignIn = async ({
code_challenge: toCodeChallenge(),
code_challenge_method: method,
}),
...extraQueryParams,
})
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
Expand Down
18 changes: 18 additions & 0 deletions packages/auth/src/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ export interface AuthSignOutInput {

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

export type OAuthUrlReservedKeys =
| 'redirect_uri'
| 'response_type'
| 'client_id'
| 'identity_provider'
| 'scope'
| 'state'
| 'responseType';

/**
* User-defined custom query params for oAuthUrl.
*/
export type ExtraQueryParameters = Omit<
Record<string, string>,
OAuthUrlReservedKeys
>;

export interface AuthSignInWithRedirectInput {
provider?: AuthProvider | { custom: string };
customState?: string;
Expand All @@ -69,6 +86,7 @@ export interface AuthSignInWithRedirectInput {
*/
preferPrivateSession?: boolean;
};
extraQueryParams?: ExtraQueryParameters;
}

/**
Expand Down