Skip to content

Commit c936ed5

Browse files
authored
Merge pull request #15 from OpenNBS/bugfix/fix-14-username-generation
fix: improve username generation to avoid conflicts with existing use…
2 parents 63a95ea + e4b0cee commit c936ed5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

server/src/user/user.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,15 @@ export class UserService {
106106
.replace(/[\u0300-\u036f]/g, '')
107107
.replace(/[^a-zA-Z0-9_]/g, '');
108108

109-
// Find if there's already a user with the same username
110-
let nameTaken = await this.usernameExists(baseUsername);
109+
let newUsername = baseUsername;
110+
let counter = 1;
111111

112-
while (nameTaken) {
113-
const newUsername = `${baseUsername}.${Math.floor(Math.random() * 100)}`;
114-
nameTaken = await this.usernameExists(newUsername);
112+
// Check if the base username already exists
113+
while (await this.usernameExists(newUsername)) {
114+
newUsername = `${baseUsername}_${counter}`;
115+
counter++;
115116
}
116117

117-
return baseUsername;
118+
return newUsername;
118119
}
119120
}

0 commit comments

Comments
 (0)