diff --git a/src/GoTrueApi.ts b/src/GoTrueApi.ts index 3d721b643..7ba22b9c2 100644 --- a/src/GoTrueApi.ts +++ b/src/GoTrueApi.ts @@ -151,7 +151,9 @@ export default class GoTrueApi { ): Promise<{ user: User | null; data: User | null; error: Error | null }> { try { const data: any = await get(`${this.url}/user`, { headers: this._createRequestHeaders(jwt) }) - return { user: data, data, error: null } + let user = data + if (data.new_email) user.email = data.new_email + return { user, data, error: null } } catch (error) { return { user: null, data: null, error } } diff --git a/test/__snapshots__/clientWithAutoConfirmEnabled.test.ts.snap b/test/__snapshots__/clientWithAutoConfirmEnabled.test.ts.snap index 3ac01943e..cfa79c71c 100644 --- a/test/__snapshots__/clientWithAutoConfirmEnabled.test.ts.snap +++ b/test/__snapshots__/clientWithAutoConfirmEnabled.test.ts.snap @@ -36,6 +36,48 @@ Object { } `; +exports[`Get user after updating email 1`] = ` +Object { + "app_metadata": Object { + "provider": "email", + }, + "aud": Any, + "confirmed_at": Any, + "created_at": Any, + "email": "client_ac_enabled_Lou23@hotmail.com", + "email_change_sent_at": "2021-02-15T07:41:15.8564305Z", + "id": Any, + "last_sign_in_at": Any, + "new_email": "updated_client_ac_enabled_Lou23@hotmail.com", + "role": "", + "updated_at": Any, + "user_metadata": Object { + "hello": "world", + }, +} +`; + +exports[`Get user after updating email twice 1`] = ` +Object { + "app_metadata": Object { + "provider": "email", + }, + "aud": Any, + "confirmed_at": Any, + "created_at": Any, + "email": "client_ac_enabled_Lou23@hotmail.com", + "email_change_sent_at": "2021-02-15T07:41:15.8699128Z", + "id": Any, + "last_sign_in_at": Any, + "new_email": "twice_client_ac_enabled_Lou23@hotmail.com", + "role": "", + "updated_at": Any, + "user_metadata": Object { + "hello": "world", + }, +} +`; + exports[`Update user 1`] = ` Object { "app_metadata": Object { @@ -55,6 +97,48 @@ Object { } `; +exports[`User changes email 1`] = ` +Object { + "app_metadata": Object { + "provider": "email", + }, + "aud": Any, + "confirmed_at": Any, + "created_at": Any, + "email": Any, + "email_change_sent_at": "2021-02-15T07:41:15.8564305Z", + "id": Any, + "last_sign_in_at": Any, + "new_email": "updated_client_ac_enabled_Lou23@hotmail.com", + "role": "", + "updated_at": Any, + "user_metadata": Object { + "hello": "world", + }, +} +`; + +exports[`User changes email twice 1`] = ` +Object { + "app_metadata": Object { + "provider": "email", + }, + "aud": Any, + "confirmed_at": Any, + "created_at": Any, + "email": Any, + "email_change_sent_at": "2021-02-15T07:41:15.8699128Z", + "id": Any, + "last_sign_in_at": Any, + "new_email": "twice_client_ac_enabled_Lou23@hotmail.com", + "role": "", + "updated_at": Any, + "user_metadata": Object { + "hello": "world", + }, +} +`; + exports[`signIn() 1`] = ` Object { "access_token": Any, diff --git a/test/clientWithAutoConfirmEnabled.test.ts b/test/clientWithAutoConfirmEnabled.test.ts index 9aca4c8c6..861281ecc 100644 --- a/test/clientWithAutoConfirmEnabled.test.ts +++ b/test/clientWithAutoConfirmEnabled.test.ts @@ -10,6 +10,8 @@ const auth = new GoTrueClient({ }) const email = `client_ac_enabled_${faker.internet.email()}` +const updatedEmail = `updated_${email}` +const updatedEmailTwice = `twice_${email}` const password = faker.internet.password() test('signUp()', async () => { @@ -150,6 +152,82 @@ test('Get user after updating', async () => { expect(user?.email).toBe(email) }) +test('User changes email', async () => { + let { error, user } = await auth.update({ email: updatedEmail }) + expect(error).toBeNull() + expect(user?.email).toBe(email) + // expect(user?.new_email).toBe(updatedEmail) + expect(user).toMatchSnapshot({ + id: expect.any(String), + aud: expect.any(String), + email: expect.any(String), + updated_at: expect.any(String), + last_sign_in_at: expect.any(String), + confirmed_at: expect.any(String), + created_at: expect.any(String), + user_metadata: { + hello: 'world', + }, + }) +}) + +test('Get user after updating email', async () => { + let user = auth.user() + expect(user?.email).toBe(email) + // expect(user?.new_email).toBe(updatedEmail) + + expect(user).toMatchSnapshot({ + id: expect.any(String), + aud: expect.any(String), + email: email, + updated_at: expect.any(String), + last_sign_in_at: expect.any(String), + confirmed_at: expect.any(String), + created_at: expect.any(String), + user_metadata: { + hello: 'world', + }, + }) +}) + +test('User changes email twice', async () => { + let { error, user } = await auth.update({ email: updatedEmailTwice }) + expect(error).toBeNull() + expect(user?.email).toBe(email) + // expect(user?.new_email).toBe(updatedEmail) + expect(user).toMatchSnapshot({ + id: expect.any(String), + aud: expect.any(String), + email, + updated_at: expect.any(String), + last_sign_in_at: expect.any(String), + confirmed_at: expect.any(String), + created_at: expect.any(String), + user_metadata: { + hello: 'world', + }, + }) +}) + +test('Get user after updating email twice', async () => { + let user = auth.user() + expect(user?.email).toBe(email) + // expect(user?.new_email).toBe(updatedEmail) + + expect(user).toMatchSnapshot({ + id: expect.any(String), + aud: expect.any(String), + email: email, + updated_at: expect.any(String), + last_sign_in_at: expect.any(String), + confirmed_at: expect.any(String), + created_at: expect.any(String), + user_metadata: { + hello: 'world', + }, + }) +}) + test('signOut', async () => { let res = await auth.signOut() expect(res).toMatchSnapshot()