Skip to content
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 @@ -434,6 +434,56 @@ describe('signInWithRedirect', () => {
});
});

describe('preferredRedirectUrl', () => {
it('passes preferredRedirectUrl to getRedirectUrl and uses it in the authorization URL', async () => {
const preferredUrl = 'http://localhost:3000/';
await signInWithRedirect({
provider: 'Google',
options: {
preferredRedirectUrl: preferredUrl,
},
});

const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toContain(
`redirect_uri=${encodeURIComponent(preferredUrl)}`,
);
});

it('uses preferredRedirectUrl when multiple redirect URLs are configured', async () => {
const preferredUrl = 'https://myapp.example.com/callback';
const mockConfigWithMultipleRedirects = {
Auth: {
Cognito: {
...mockAuthConfigWithOAuth.Auth.Cognito,
loginWith: {
...mockAuthConfigWithOAuth.Auth.Cognito.loginWith,
oauth: {
...mockAuthConfigWithOAuth.Auth.Cognito.loginWith.oauth,
redirectSignIn: ['http://localhost:3000/', preferredUrl],
},
},
},
},
};
(Amplify.getConfig as jest.Mock).mockReturnValueOnce(
mockConfigWithMultipleRedirects,
);

await signInWithRedirect({
provider: 'Google',
options: {
preferredRedirectUrl: preferredUrl,
},
});

const [oauthUrl] = mockOpenAuthSession.mock.calls[0];
expect(oauthUrl).toContain(
`redirect_uri=${encodeURIComponent(preferredUrl)}`,
);
});
});

describe('errors', () => {
it('rethrows error thrown from `assertTokenProviderConfig`', async () => {
const mockError = new Error('mock error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function signInWithRedirect(
prompt: input?.options?.prompt,
},
authSessionOpener: input?.options?.authSessionOpener,
preferredRedirectUrl: input?.options?.preferredRedirectUrl,
});
}

Expand All @@ -87,6 +88,7 @@ const oauthSignIn = async ({
preferPrivateSession,
options,
authSessionOpener,
preferredRedirectUrl,
}: {
oauthConfig: OAuthConfig;
provider: string;
Expand All @@ -96,6 +98,7 @@ const oauthSignIn = async ({
preferPrivateSession?: boolean;
options?: SignInWithRedirectInput['options'];
authSessionOpener?: OpenAuthSession;
preferredRedirectUrl?: string;
}) => {
const { domain, redirectSignIn, responseType, scopes } = oauthConfig;
const { loginHint, lang, nonce, prompt } = options ?? {};
Expand All @@ -113,7 +116,10 @@ const oauthSignIn = async ({
: randomState;

const { value, method, toCodeChallenge } = generateCodeVerifier(128);
const redirectUri = getRedirectUrl(oauthConfig.redirectSignIn);
const redirectUri = getRedirectUrl(
oauthConfig.redirectSignIn,
preferredRedirectUrl,
);

if (isBrowser()) oAuthStore.storeOAuthInFlight(true);
oAuthStore.storeOAuthState(state);
Expand Down
6 changes: 6 additions & 0 deletions packages/auth/src/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ export interface AuthSignInWithRedirectInput {
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html
*/
prompt?: AuthPrompt;
/**
* The preferred redirect URL to use for the OAuth flow. Must match one of the
* configured `redirectSignIn` URLs. On native platforms, this allows selecting
* an HTTPS App Link/Universal Link over the default custom scheme.
*/
preferredRedirectUrl?: string;
};
}

Expand Down
Loading