diff --git a/packages/aws-amplify-react/__tests__/Auth/Greetings-test.js b/packages/aws-amplify-react/__tests__/Auth/Greetings-test.js index ec37669ad0d..927c3249cfb 100644 --- a/packages/aws-amplify-react/__tests__/Auth/Greetings-test.js +++ b/packages/aws-amplify-react/__tests__/Auth/Greetings-test.js @@ -64,6 +64,25 @@ describe('Greetings', () => { expect(wrapper).toMatchSnapshot(); } }); + + test('render name from attributes', () => { + const wrapper = shallow(); + wrapper.setProps({ + authState: 'signedIn', + theme: 'theme' + }); + + wrapper.setState({ + authData: { + attributes: { + name: 'name' + } + }, + authState: 'signedIn' + }) + + expect(wrapper).toMatchSnapshot(); + }) }); diff --git a/packages/aws-amplify-react/__tests__/Auth/__snapshots__/Greetings-test.js.snap b/packages/aws-amplify-react/__tests__/Auth/__snapshots__/Greetings-test.js.snap index afaab87b992..9c237be1c86 100644 --- a/packages/aws-amplify-react/__tests__/Auth/__snapshots__/Greetings-test.js.snap +++ b/packages/aws-amplify-react/__tests__/Auth/__snapshots__/Greetings-test.js.snap @@ -2,6 +2,34 @@ exports[`Greetings normal case render correctly with authState signedIn 1`] = `""`; +exports[`Greetings normal case render name from attributes 1`] = ` + + + +`; + exports[`Greetings render corrently with other authStates 1`] = `""`; exports[`Greetings render corrently with other authStates 2`] = `""`; diff --git a/packages/aws-amplify-react/src/Auth/Greetings.jsx b/packages/aws-amplify-react/src/Auth/Greetings.jsx index 3244fad5dfc..e352399daf1 100644 --- a/packages/aws-amplify-react/src/Auth/Greetings.jsx +++ b/packages/aws-amplify-react/src/Auth/Greetings.jsx @@ -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 ( diff --git a/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts b/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts index 6dd61031fa3..63fde2c0e4a 100644 --- a/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts +++ b/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts @@ -891,7 +891,8 @@ describe('auth unit test', () => { userPoolId: undefined, userPoolWebClientId: "awsUserPoolsWebClientId", region: "region", - identityPoolId: "awsCognitoIdentityPoolId" + identityPoolId: "awsCognitoIdentityPoolId", + mandatorySignIn: false }); expect.assertions(1); @@ -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(); diff --git a/packages/aws-amplify/src/Auth/Auth.ts b/packages/aws-amplify/src/Auth/Auth.ts index 54673f87fa5..624775808c0 100644 --- a/packages/aws-amplify/src/Auth/Auth.ts +++ b/packages/aws-amplify/src/Auth/Auth.ts @@ -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; } } }