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

Chore: test email updates #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/GoTrueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Comment on lines +154 to +156
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one way we can resolve the strange behavior

we might even want to add an initial_email field which stores the email that the user first used during signup? I think it's even better to be added to the app_data

{
  "app_metadata": Object {
    "provider": "email",
    "initial_email": "user@email.com"
  },
  "email": "updated@email.com",         // original email
  "id": Any<String>,
  "role": "",
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this solution seems great!

In a future version it would be cool to differentiate the email from the user login, and to be able to choose a username.

} catch (error) {
return { user: null, data: null, error }
}
Expand Down
84 changes: 84 additions & 0 deletions test/__snapshots__/clientWithAutoConfirmEnabled.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,48 @@ Object {
}
`;

exports[`Get user after updating email 1`] = `
Object {
"app_metadata": Object {
"provider": "email",
},
"aud": Any<String>,
"confirmed_at": Any<String>,
"created_at": Any<String>,
"email": "client_ac_enabled_Lou23@hotmail.com",
"email_change_sent_at": "2021-02-15T07:41:15.8564305Z",
"id": Any<String>,
"last_sign_in_at": Any<String>,
"new_email": "updated_client_ac_enabled_Lou23@hotmail.com",
"role": "",
"updated_at": Any<String>,
"user_metadata": Object {
"hello": "world",
},
}
`;

exports[`Get user after updating email twice 1`] = `
Object {
"app_metadata": Object {
"provider": "email",
},
"aud": Any<String>,
"confirmed_at": Any<String>,
"created_at": Any<String>,
"email": "client_ac_enabled_Lou23@hotmail.com",
"email_change_sent_at": "2021-02-15T07:41:15.8699128Z",
"id": Any<String>,
"last_sign_in_at": Any<String>,
"new_email": "twice_client_ac_enabled_Lou23@hotmail.com",
"role": "",
"updated_at": Any<String>,
"user_metadata": Object {
"hello": "world",
},
}
`;

exports[`Update user 1`] = `
Object {
"app_metadata": Object {
Expand All @@ -55,6 +97,48 @@ Object {
}
`;

exports[`User changes email 1`] = `
Object {
"app_metadata": Object {
"provider": "email",
},
"aud": Any<String>,
"confirmed_at": Any<String>,
"created_at": Any<String>,
"email": Any<String>,
"email_change_sent_at": "2021-02-15T07:41:15.8564305Z",
"id": Any<String>,
"last_sign_in_at": Any<String>,
"new_email": "updated_client_ac_enabled_Lou23@hotmail.com",
"role": "",
"updated_at": Any<String>,
"user_metadata": Object {
"hello": "world",
},
}
`;

exports[`User changes email twice 1`] = `
Object {
"app_metadata": Object {
"provider": "email",
},
"aud": Any<String>,
"confirmed_at": Any<String>,
"created_at": Any<String>,
"email": Any<String>,
"email_change_sent_at": "2021-02-15T07:41:15.8699128Z",
"id": Any<String>,
"last_sign_in_at": Any<String>,
"new_email": "twice_client_ac_enabled_Lou23@hotmail.com",
"role": "",
"updated_at": Any<String>,
"user_metadata": Object {
"hello": "world",
},
}
`;

exports[`signIn() 1`] = `
Object {
"access_token": Any<String>,
Expand Down
78 changes: 78 additions & 0 deletions test/clientWithAutoConfirmEnabled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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()
Expand Down