Skip to content

Commit

Permalink
Merge pull request #3230 from jessedoyle/gh-3057
Browse files Browse the repository at this point in the history
fix(@aws-amplify/auth): react-native - guard for window reference
  • Loading branch information
powerful23 authored May 9, 2019
2 parents db55ff3 + 9254312 commit 2267a77
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ export default class AuthClass {

// initiailize cognitoauth client if hosted ui options provided
// to keep backward compatibility:
const cognitoHostedUIConfig = oauth? (isCognitoHostedOpts(this._config.oauth)
? oauth : (<any>oauth).awsCognito)
const cognitoHostedUIConfig = oauth? (isCognitoHostedOpts(this._config.oauth)
? oauth : (<any>oauth).awsCognito)
: undefined;

if (cognitoHostedUIConfig) {
Expand Down Expand Up @@ -218,7 +218,7 @@ export default class AuthClass {
}

dispatchAuthEvent(
'configured',
'configured',
null,
`The Auth category has been configured successfully`
);
Expand Down Expand Up @@ -271,14 +271,14 @@ export default class AuthClass {
this.userPool.signUp(username, password, attributes, validationData, (err, data) => {
if (err) {
dispatchAuthEvent(
'signUp_failure',
'signUp_failure',
err,
`${username} failed to signup`
);
reject(err);
} else {
dispatchAuthEvent(
'signUp',
'signUp',
data,
`${username} has signed up successfully`
);
Expand Down Expand Up @@ -396,7 +396,7 @@ export default class AuthClass {
const currentUser = await this.currentUserPoolUser();
that.user = currentUser;
dispatchAuthEvent(
'signIn',
'signIn',
currentUser,
`A user ${user.getUsername()} has been signed in`
);
Expand Down Expand Up @@ -794,10 +794,10 @@ export default class AuthClass {
logger.debug('cannot get cognito credentials', e);
} finally {
that.user = user;

dispatchAuthEvent(
'signIn',
user,
'signIn',
user,
`${user} has signed in`
);
resolve(user);
Expand Down Expand Up @@ -833,7 +833,7 @@ export default class AuthClass {
} finally {
that.user = user;
dispatchAuthEvent(
'signIn',
'signIn',
user, `${user} has signed in`
);
resolve(user);
Expand All @@ -842,7 +842,7 @@ export default class AuthClass {
onFailure: (err) => {
logger.debug('completeNewPassword failure', err);
dispatchAuthEvent(
'completeNewPassword_failure',
'completeNewPassword_failure',
err,
`${this.user} failed to complete the new password flow`
);
Expand Down Expand Up @@ -1227,7 +1227,7 @@ export default class AuthClass {
throw e;
}

const isSignedInHostedUI = this._oAuthHandler
const isSignedInHostedUI = this._oAuthHandler
&& this._storage.getItem('amplify-signin-with-hostedUI') === 'true';

return new Promise((res, rej) => {
Expand Down Expand Up @@ -1290,15 +1290,15 @@ export default class AuthClass {
logger.debug('no Congito User pool');
}

/**
/**
* Note for future refactor - no reliable way to get username with
* Cognito User Pools vs Identity when federating with Social Providers
* This is why we need a well structured session object that can be inspected
* and information passed back in the message below for Hub dispatch
*/
dispatchAuthEvent(
'signOut',
this.user,
'signOut',
this.user,
`A user has been signed out`
);
this.user = null;
Expand Down Expand Up @@ -1465,7 +1465,7 @@ export default class AuthClass {
if (isFederatedSignInOptions(providerOrOptions)
|| isFederatedSignInOptionsCustom(providerOrOptions)
|| typeof providerOrOptions === 'undefined') {

const options = providerOrOptions || { provider: CognitoHostedUIIdentityProvider.Cognito };
const provider = isFederatedSignInOptions(options)
? options.provider
Expand Down Expand Up @@ -1526,7 +1526,7 @@ export default class AuthClass {
throw new Error(`OAuth responses require a User Pool defined in config`);
}

const currentUrl = URL || window.location.href;
const currentUrl = URL || (JS.browserOrNode().isBrowser ? window.location.href : null);

const hasCodeOrError = !!(parse(currentUrl).query || '')
.split('&')
Expand All @@ -1538,18 +1538,18 @@ export default class AuthClass {
.split('&')
.map(entry => entry.split('='))
.find(([k]) => k === 'access_token' || k === 'error');


if (hasCodeOrError || hasTokenOrError) {
try {

const { accessToken, idToken, refreshToken } = await this._oAuthHandler.handleAuthResponse(currentUrl);
const session = new CognitoUserSession({
IdToken: new CognitoIdToken({ IdToken: idToken }),
RefreshToken: new CognitoRefreshToken({ RefreshToken: refreshToken }),
AccessToken: new CognitoAccessToken({ AccessToken: accessToken })
});

let credentials;
// Get AWS Credentials & store if Identity Pool is defined
if (this._config.identityPoolId) {
Expand All @@ -1559,37 +1559,37 @@ export default class AuthClass {

/*The following is to create a user for the Cognito Identity SDK to store the tokens
When we remove this SDK later that logic will have to be centralized in our new version*/
//#region
//#region
const currentUser = this.createCognitoUser(session.getIdToken().decodePayload()['cognito:username']);
dispatchAuthEvent(
'signIn',
'signIn',
currentUser,
`A user ${currentUser.getUsername()} has been signed in`
);
dispatchAuthEvent(
'cognitoHostedUI',
'cognitoHostedUI',
currentUser,
`A user ${currentUser.getUsername()} has been signed in via Cognito Hosted UI`
);

// This calls cacheTokens() in Cognito SDK
currentUser.setSignInUserSession(session);
//#endregion

if (window && typeof window.history !== 'undefined') {
window.history.replaceState({}, null, (this._config.oauth as AwsCognitoOAuthOpts).redirectSignIn);
}

return credentials;
} catch (err) {
logger.debug("Error in cognito hosted auth response", err);
dispatchAuthEvent(
'signIn_failure',
'signIn_failure',
err,
`The OAuth response flow failed`
);
dispatchAuthEvent(
'cognitoHostedUI_failure',
'cognitoHostedUI_failure',
err,
`A failure occurred when returning to the Cognito Hosted UI`
);
Expand Down

0 comments on commit 2267a77

Please sign in to comment.