From 76a122944e0de8e3e63ea723a4df03674639fb21 Mon Sep 17 00:00:00 2001 From: Jordan Dalby Date: Sun, 5 Jan 2025 12:42:19 +0000 Subject: [PATCH] [feat] prefer name over email substring --- server/src/repositories/userRepository.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/src/repositories/userRepository.js b/server/src/repositories/userRepository.js index 7c5579d..939d761 100644 --- a/server/src/repositories/userRepository.js +++ b/server/src/repositories/userRepository.js @@ -129,9 +129,17 @@ class UserRepository { const user = this.findByOIDCIdStmt.get(profile.sub, provider); if (user) return user; - const baseUsername = profile.email?.split('@')[0] || - profile.preferred_username || - profile.sub; + const sanitizeName = (name) => { + return name + .toLowerCase() + .replace(/[^a-z0-9]/g, '') + .slice(0, 30); + }; + + let baseUsername = profile.name ? sanitizeName(profile.name) : + profile.preferred_username ? sanitizeName(profile.preferred_username) : + profile.email?.split('@')[0] || + profile.sub; const username = await this.generateUniqueUsername(baseUsername);