Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth current authenticated user enhancement #713

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
4 changes: 3 additions & 1 deletion packages/aws-amplify/src/Auth/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,9 @@ export default class AuthClass {
} else {
logger.debug('get current authenticated userpool user');
try {
this.user = await this.currentUserPoolUser();
const user = await this.currentUserPoolUser();
const attributes = this.attributesToObject(await this.userAttributes(user));
this.user = Object.assign({}, user, { attributes });
return this.user;
} catch (e) {
return Promise.reject('not authenticated');
Expand Down