Skip to content

Commit

Permalink
Merge pull request #1134 from givepraise/fix_discord_shortname
Browse files Browse the repository at this point in the history
Fixing short discord names
  • Loading branch information
kristoferlund authored Aug 18, 2023
2 parents cf0137f + 4faa760 commit f034661
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- **API:** Praise now allows for usernames that are two characters to align with Discord requirements #1132
- **Frontend:** JWT token is now refreshed when it expires. If the refresh token has expired as well, user has to login again. #1120
- **Frontend:** Fix styling bug that caused the login button to be hidden on short screens. #1107

Expand Down
2 changes: 1 addition & 1 deletion packages/api/openapi.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/api/src/useraccounts/schemas/useraccounts.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export class UserAccount {
@ApiProperty({
example: 'darth#6755',
type: 'string',
minLength: 4,
minLength: 2,
maxLength: 32,
})
@IsString()
@Prop({ required: true, minlength: 4, maxlength: 32 })
@Prop({ required: true, minlength: 2, maxlength: 32 })
name: string;

@ApiProperty({
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/users/schemas/users.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class User {
@Prop({
required: true,
unique: true,
minlength: 4,
minlength: 2,
maxlength: 50,
validate: {
validator: (username: string) =>
Expand Down
11 changes: 2 additions & 9 deletions packages/api/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class UsersService {
/**
* A valid username is:
* - is lowercase
* - minimum 3 characters
* - minimum 2 characters
* - maximum 50 characters
* - only alphanumeric characters, underscores, dots, and hyphens
* - cannot start with a dot or hyphen
Expand All @@ -251,7 +251,7 @@ export class UsersService {
* - should not already be taken
*/
async generateValidUsername(username: string): Promise<string> {
let newUsername = username
const newUsername = username
.toLowerCase()
.replace(/\s/g, '_')
.replace(/[^a-z0-9_.-]/g, '')
Expand All @@ -260,13 +260,6 @@ export class UsersService {
.replace(/[^a-z0-9]+$/g, '')
.substring(0, 50);

// If the new username is less than 4 characters, pad it with a random number to ensure a minimum length of 4
while (newUsername.length < 4) {
newUsername = `${newUsername}${Math.floor(
Math.random() * 900 + 100,
)}`.substring(0, 50);
}

// Check if the username already exists
const exists = await this.userModel.find({ username: newUsername }).lean();

Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/users/utils/is-valid-username.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* A valid username is:
* - is lowercase
* - minimum 3 characters
* - minimum 2 characters
* - maximum 50 characters
* - only alphanumeric characters, underscores, dots, and hyphens
* - cannot start with a dot or hyphen
* - cannot end with a dot or hyphen
* - cannot contain two dots, two hyphens, or two underscores in a row
*/
export function isValidUsername(username: string) {
const pattern = /^[a-z0-9][a-z0-9_.-]{2,48}[a-z0-9]$/;
const pattern = /^[a-z0-9][a-z0-9_.-]{0,48}[a-z0-9]$/;
return pattern.test(username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const validate = (

// username validation
if (values.username) {
if (values.username.length < 4) {
errors.username = 'Min 4 characters';
} else if (values.username.length > 20) {
errors.username = 'Max 20 characters';
if (values.username.length < 2) {
errors.username = 'Min 2 characters';
} else if (values.username.length > 32) {
errors.username = 'Max 32 characters';
} else {
const pattern = /^[a-z0-9][a-z0-9_.-]{1,18}[a-z0-9]$/;
const pattern = /^[a-z0-9][a-z0-9_.-]{0,30}[a-z0-9]$/;
if (!pattern.test(values.username)) {
errors.username =
'Usernames can only contain lowercase letters, numbers, dots, dashes and underscores';
Expand Down

0 comments on commit f034661

Please sign in to comment.