Skip to content

Commit

Permalink
Merge pull request #713 from powerful23/auth-currentAuthenticatedUser…
Browse files Browse the repository at this point in the history
…-enhancement

Auth current authenticated user enhancement
  • Loading branch information
powerful23 authored May 1, 2018
2 parents 799b40c + 8716592 commit 5e8e9db
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
19 changes: 19 additions & 0 deletions packages/aws-amplify-react/__tests__/Auth/Greetings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ describe('Greetings', () => {
expect(wrapper).toMatchSnapshot();
}
});

test('render name from attributes', () => {
const wrapper = shallow(<Greetings/>);
wrapper.setProps({
authState: 'signedIn',
theme: 'theme'
});

wrapper.setState({
authData: {
attributes: {
name: 'name'
}
},
authState: 'signedIn'
})

expect(wrapper).toMatchSnapshot();
})
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

exports[`Greetings normal case render correctly with authState signedIn 1`] = `""`;

exports[`Greetings normal case render name from attributes 1`] = `
<NavBar
theme="theme"
>
<Nav
theme="theme"
>
<NavRight
theme="theme"
>
<span>
<NavItem
theme="theme"
>
Hello name
</NavItem>
<NavButton
onClick={[Function]}
theme="theme"
>
Sign Out
</NavButton>
</span>
</NavRight>
</Nav>
</NavBar>
`;

exports[`Greetings render corrently with other authStates 1`] = `""`;

exports[`Greetings render corrently with other authStates 2`] = `""`;
Expand Down
9 changes: 8 additions & 1 deletion packages/aws-amplify-react/src/Auth/Greetings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ export default class Greetings extends AuthPiece {
userGreetings(theme) {
const user = this.state.authData;
const greeting = this.props.inGreeting || this.inGreeting;
const name = user.name || user.username;
// get name from attributes first
const nameFromAttr = user.attributes?
(user.attributes.name ||
(user.attributes.given_name?
(user.attributes.given_name + ' ' + user.attributes.family_name) : undefined))
: undefined;

const name = nameFromAttr || user.name || user.username;
const message = (typeof greeting === 'function')? greeting(name) : greeting;
return (
<span>
Expand Down
11 changes: 9 additions & 2 deletions packages/aws-amplify/__tests__/Auth/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ describe('auth unit test', () => {
userPoolId: undefined,
userPoolWebClientId: "awsUserPoolsWebClientId",
region: "region",
identityPoolId: "awsCognitoIdentityPoolId"
identityPoolId: "awsCognitoIdentityPoolId",
mandatorySignIn: false
});

expect.assertions(1);
Expand All @@ -918,8 +919,14 @@ describe('auth unit test', () => {
});
});

const spyon2 = jest.spyOn(Auth.prototype, 'userAttributes').mockImplementationOnce(() => {
return Promise.resolve([{
Name: 'name',
Value: 'val'
}]);
});
expect.assertions(1);
expect(await auth.currentAuthenticatedUser()).toEqual(user);
expect(await auth.currentAuthenticatedUser()).toEqual({"attributes": {"name": "val"}});

spyon.mockClear();

Expand Down
15 changes: 12 additions & 3 deletions packages/aws-amplify/src/Auth/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,20 @@ export default class AuthClass {
return this.user;
} else {
logger.debug('get current authenticated userpool user');
let user = null;
try {
this.user = await this.currentUserPoolUser();
return this.user;
user = await this.currentUserPoolUser();
} catch (e) {
throw 'not authenticated';
}
let attributes = {};
try {
attributes = this.attributesToObject(await this.userAttributes(user));
} catch (e) {
return Promise.reject('not authenticated');
logger.debug('cannot get user attributes');
} finally {
this.user = Object.assign({}, user, { attributes });
return this.user;
}
}
}
Expand Down

0 comments on commit 5e8e9db

Please sign in to comment.