Skip to content

Commit

Permalink
feat(auth): fix inflight promise on RN
Browse files Browse the repository at this point in the history
  • Loading branch information
Samaritan1011001 committed Mar 30, 2024
1 parent 3a01869 commit f3bd2dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
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

0 comments on commit f3bd2dc

Please sign in to comment.