From 1ca19b5c3821790ec39cbd7056fe88b04a675ed1 Mon Sep 17 00:00:00 2001 From: Henry Fontanier Date: Wed, 30 Oct 2024 13:29:16 +0100 Subject: [PATCH] fix: lowercase emails in db (#8326) * fix: lowercase emails in db * r --------- Co-authored-by: Henry Fontanier --- front/lib/resources/user_resource.ts | 8 +++++--- front/migrations/20241030_lowercase_emails.sql | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 front/migrations/20241030_lowercase_emails.sql diff --git a/front/lib/resources/user_resource.ts b/front/lib/resources/user_resource.ts index 19bb719a5cbc..3ff93ff005bc 100644 --- a/front/lib/resources/user_resource.ts +++ b/front/lib/resources/user_resource.ts @@ -39,7 +39,8 @@ export class UserResource extends BaseResource { > & Partial, "providerId" | "imageUrl">> ): Promise { - const user = await User.create(blob); + const lowerCaseEmail = blob.email?.toLowerCase(); + const user = await User.create({ ...blob, email: lowerCaseEmail }); return new this(User, user.get()); } @@ -104,7 +105,7 @@ export class UserResource extends BaseResource { static async fetchByEmail(email: string): Promise { const user = await User.findOne({ where: { - email, + email: email.toLowerCase(), }, }); @@ -227,8 +228,9 @@ export class UserResource extends BaseResource { lastName: string | null, email: string ): Promise { + const lowerCaseEmail = email.toLowerCase(); const [, affectedRows] = await this.model.update( - { username, firstName, lastName, email }, + { username, firstName, lastName, email: lowerCaseEmail }, { where: { id: this.id, diff --git a/front/migrations/20241030_lowercase_emails.sql b/front/migrations/20241030_lowercase_emails.sql new file mode 100644 index 000000000000..c3f77cfc3c2f --- /dev/null +++ b/front/migrations/20241030_lowercase_emails.sql @@ -0,0 +1,6 @@ +UPDATE + users +SET + email = LOWER(email) +WHERE + email <> LOWER(email); \ No newline at end of file