Skip to content

Commit

Permalink
Merge pull request #1507 from powerful23/fix-issue-1468
Browse files Browse the repository at this point in the history
fix(@aws-amplify/auth): correctly throw the error when the refresh token is expired
  • Loading branch information
powerful23 authored Aug 29, 2018
2 parents bcca869 + 7adcca4 commit ed188ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
7 changes: 5 additions & 2 deletions packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1965,15 +1965,19 @@ describe('auth unit test', () => {
return callback('err', null);
});

expect.assertions(1);
const spyon3 = jest.spyOn(CognitoUser.prototype, 'getUserData');

expect.assertions(2);
try {
await auth.currentUserPoolUser();
} catch (e) {
expect(e).toBe('err');
expect(spyon3).not.toBeCalled();
}

spyon.mockClear();
spyon2.mockClear();
spyon3.mockClear();
});

test('get user data error because of user is deleted or disabled', async () => {
Expand Down Expand Up @@ -2042,7 +2046,6 @@ describe('auth unit test', () => {
spyon3.mockClear();
});
});
});

describe('sendCustomChallengeAnswer', () => {
test('happy case', async () => {
Expand Down
58 changes: 29 additions & 29 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,43 +858,43 @@ export default class AuthClass {
}

// refresh the session if the session expired.
user.getSession(function(err, session) {
user.getSession((err, session) => {
if (err) {
logger.debug('Failed to get the user session', err);
rej(err);
return;
}
});

// get user data from Cognito
user.getUserData((err, data) => {
if (err) {
logger.debug('getting user data failed', err);
// Make sure the user is still valid
if (err.message === 'User is disabled' || err.message === 'User does not exist.') {
rej(err);
} else {
// the error may also be thrown when lack of permissions to get user info etc
// in that case we just bypass the error
res(user);
}
return;
}
const preferredMFA = data.PreferredMfaSetting || 'NOMFA';
const attributeList = [];

// get user data from Cognito
user.getUserData((err, data) => {
if (err) {
logger.debug('getting user data failed', err);
// Make sure the user is still valid
if (err.message === 'User is disabled' || err.message === 'User does not exist.') {
rej(err);
} else {
// the error may also be thrown when lack of permissions to get user info etc
// in that case we just bypass the error
res(user);
for (let i = 0; i < data.UserAttributes.length; i++) {
const attribute = {
Name: data.UserAttributes[i].Name,
Value: data.UserAttributes[i].Value,
};
const userAttribute = new CognitoUserAttribute(attribute);
attributeList.push(userAttribute);
}
return;
}
const preferredMFA = data.PreferredMfaSetting || 'NOMFA';
const attributeList = [];

for (let i = 0; i < data.UserAttributes.length; i++) {
const attribute = {
Name: data.UserAttributes[i].Name,
Value: data.UserAttributes[i].Value,
};
const userAttribute = new CognitoUserAttribute(attribute);
attributeList.push(userAttribute);
}

const attributes = this.attributesToObject(attributeList);
Object.assign(user, {attributes, preferredMFA});
res(user);
const attributes = that.attributesToObject(attributeList);
Object.assign(user, {attributes, preferredMFA});
res(user);
});
});
});
});
Expand Down

0 comments on commit ed188ba

Please sign in to comment.