From 2975db7d8acc3c44017c33bf6c11e338da292bd2 Mon Sep 17 00:00:00 2001 From: Pavel Baluev Date: Tue, 29 Oct 2024 12:19:40 +0100 Subject: [PATCH] Lowercase user email --- .../20241029121408_users_users-email-lowercase.js | 14 ++++++++++++++ src/server/auth/providers/google/index.ts | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/modules/users/server/migrations/20241029121408_users_users-email-lowercase.js diff --git a/src/modules/users/server/migrations/20241029121408_users_users-email-lowercase.js b/src/modules/users/server/migrations/20241029121408_users_users-email-lowercase.js new file mode 100644 index 0000000..81223fb --- /dev/null +++ b/src/modules/users/server/migrations/20241029121408_users_users-email-lowercase.js @@ -0,0 +1,14 @@ +// @ts-check +const { Sequelize, DataTypes } = require('sequelize') + +module.exports = { + async up({ context: queryInterface, appConfig }) { + await queryInterface.sequelize.query(` + UPDATE users + SET email = LOWER(email) + WHERE email <> LOWER(email) + `) + }, + + async down({ context: queryInterface, appConfig }) {}, +} diff --git a/src/server/auth/providers/google/index.ts b/src/server/auth/providers/google/index.ts index a513b95..29a8de8 100644 --- a/src/server/auth/providers/google/index.ts +++ b/src/server/auth/providers/google/index.ts @@ -70,13 +70,14 @@ export const plugin: FastifyPluginCallback = async ( const sanitizeInput = (x: string) => x.replace(/[\u0000-\u001F\u007F-\u009F]/g, '') + const email = data.email.toLowerCase() if (!user) { user = await fastify.db.User.create({ fullName: sanitizeInput(data.name), - email: sanitizeInput(data.email), + email: sanitizeInput(email), avatar: sanitizeInput(data.picture), - roles: [appConfig.getDefaultUserRoleByEmail(data.email)], + roles: [appConfig.getDefaultUserRoleByEmail(email)], }) } else { await user.set({ avatar: sanitizeInput(data.picture) }).save()