Skip to content

Commit

Permalink
feat(auth): add option to disable idp oauth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Kumar committed May 13, 2024
1 parent b88df66 commit c08a927
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ export class AuthClass {
: (<any>oauth).awsCognito
: undefined;

const isIdpInitiatedOAuthEnabled =
cognitoHostedUIConfig?.idpEnabled ?? true; // default true, avoid breaking change
const isSpInitiatedOAuthInFlight =
this._storage.getItem('amplify-sp-initiated-oauth-inFlight') === 'true';

if (cognitoHostedUIConfig) {
const cognitoAuthParams = Object.assign(
{
Expand All @@ -249,18 +254,20 @@ export class AuthClass {
cognitoClientId: cognitoAuthParams.cognitoClientId,
});

// **NOTE** - Remove this in a future major release as it is a breaking change
// Prevents _handleAuthResponse from being called multiple times in Expo
// See https://github.com/aws-amplify/amplify-js/issues/4388
const usedResponseUrls = {};
urlListener(({ url }) => {
if (usedResponseUrls[url]) {
return;
}
if (isIdpInitiatedOAuthEnabled || isSpInitiatedOAuthInFlight) {
// **NOTE** - Remove this in a future major release as it is a breaking change
// Prevents _handleAuthResponse from being called multiple times in Expo
// See https://github.com/aws-amplify/amplify-js/issues/4388
const usedResponseUrls = {};
urlListener(({ url }) => {
if (usedResponseUrls[url]) {
return;
}

usedResponseUrls[url] = true;
this._handleAuthResponse(url);
});
usedResponseUrls[url] = true;
this._handleAuthResponse(url);
});
}
}

dispatchAuthEvent(
Expand Down Expand Up @@ -2437,6 +2444,7 @@ export class AuthClass {
? this._config.oauth.redirectSignIn
: this._config.oauth.redirectUri;

this._storage.setItem('amplify-sp-initiated-oauth-inFlight', 'true');
this._oAuthHandler.oauthSignIn(
this._config.oauth.responseType,
this._config.oauth.domain,
Expand Down Expand Up @@ -2517,6 +2525,11 @@ export class AuthClass {

if (hasCodeOrError || hasTokenOrError) {
this._storage.setItem('amplify-redirected-from-hosted-ui', 'true');
// clear temp value
if (this._storage.getItem('amplify-sp-initiated-oauth-inFlight')) {
this._storage.removeItem('amplify-sp-initiated-oauth-inFlight');
}

try {
const { accessToken, idToken, refreshToken, state } =
await this._oAuthHandler.handleAuthResponse(currentUrl);
Expand Down

0 comments on commit c08a927

Please sign in to comment.