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

release(required): fix inflight promise on RN #13204

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ describe('signInWithRedirect', () => {
});

describe('specifications on react-native', () => {
beforeAll(() => {
mockIsBrowser.mockReturnValue(false);
});
it('invokes `completeOAuthFlow` when `openAuthSession`completes', async () => {
const mockOpenAuthSessionResult = {
type: 'success',
Expand Down Expand Up @@ -259,6 +262,19 @@ describe('signInWithRedirect', () => {
);
expect(mockHandleFailure).toHaveBeenCalledWith(expectedError);
});
it('should not set the Oauth flag on non-browser environments', async () => {
const mockOpenAuthSessionResult = {
type: 'success',
url: 'http://redrect-in-react-native.com',
};
mockOpenAuthSession.mockResolvedValueOnce(mockOpenAuthSessionResult);

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

expect(oAuthStore.storeOAuthInFlight).toHaveBeenCalledTimes(0);
});
});

describe('errors', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AuthAction,
assertOAuthConfig,
assertTokenProviderConfig,
isBrowser,
urlSafeEncode,
} from '@aws-amplify/core/internals/utils';

Expand Down Expand Up @@ -87,7 +88,7 @@ const oauthSignIn = async ({
const { value, method, toCodeChallenge } = generateCodeVerifier(128);
const redirectUri = getRedirectUrl(oauthConfig.redirectSignIn);

oAuthStore.storeOAuthInFlight(true);
if (isBrowser()) oAuthStore.storeOAuthInFlight(true);
oAuthStore.storeOAuthState(state);
oAuthStore.storePKCE(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
AMPLIFY_SYMBOL,
assertTokenProviderConfig,
isBrowser,
isTokenExpired,
} from '@aws-amplify/core/internals/utils';

Expand All @@ -32,25 +33,29 @@ export class TokenOrchestrator implements AuthTokenOrchestrator {
tokenStore?: AuthTokenStore;
tokenRefresher?: TokenRefresher;
inflightPromise: Promise<void> | undefined;
waitForInflightOAuth: () => Promise<void> = async () => {
if (!(await oAuthStore.loadOAuthInFlight())) {
return;
}
waitForInflightOAuth: () => Promise<void> = isBrowser()
? async () => {
if (!(await oAuthStore.loadOAuthInFlight())) {
return;
}

if (this.inflightPromise) {
return this.inflightPromise;
}
if (this.inflightPromise) {
return this.inflightPromise;
}

// when there is valid oauth config and there is an inflight oauth flow, try
// to block async calls that require fetching tokens before the oauth flow completes
// e.g. getCurrentUser, fetchAuthSession etc.
// when there is valid oauth config and there is an inflight oauth flow, try
// to block async calls that require fetching tokens before the oauth flow completes
// e.g. getCurrentUser, fetchAuthSession etc.

this.inflightPromise = new Promise<void>((resolve, _reject) => {
addInflightPromise(resolve);
});
this.inflightPromise = new Promise<void>((resolve, _reject) => {
addInflightPromise(resolve);
});

return this.inflightPromise;
};
return this.inflightPromise;
}
: async () => {
// no-op for non-browser environments
};

setAuthConfig(authConfig: AuthConfig) {
oAuthStore.setAuthConfig(authConfig.Cognito as CognitoUserPoolConfig);
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
"name": "[Auth] confirmSignIn (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmSignIn }",
"limit": "26.48 kB"
"limit": "26.50 kB"
},
{
"name": "[Auth] updateMFAPreference (Cognito)",
Expand Down Expand Up @@ -455,7 +455,7 @@
"name": "[Auth] OAuth Auth Flow (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ signInWithRedirect, signOut, fetchAuthSession }",
"limit": "19.80 kB"
"limit": "19.90 kB"
},
{
"name": "[Storage] copy (S3)",
Expand Down