Skip to content

fix-6117-empty-users-getting-created-in-appwrite-console #6127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/config/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@
'description' => 'OAuth2 provider returned some error.',
'code' => 424,
],
Exception::USER_NAME_INVALID => [
'name' => Exception::USER_NAME_INVALID,
'description' => 'Name must be at least 2 characters long.',
'code' => 400,
],
Exception::USER_EMAIL_INVALID => [
'name' => Exception::USER_EMAIL_INVALID,
'description' => 'Invalid email format.',
'code' => 400,
],
Exception::USER_PASSWORD_INVALID => [
'name' => Exception::USER_PASSWORD_INVALID,
'description' => 'Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, and one number.',
'code' => 400,
],
Exception::EMAIL_REQUIRED => [
'name' => Exception::EMAIL_REQUIRED,
'description' => 'Email address is missing. Please enter it to continue.',
'code' => 400,
],


/** Teams */
Exception::TEAM_NOT_FOUND => [
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/api/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ function createUser(string $hash, mixed $hashOptions, string $userId, ?string $e
}

try {
// Validation logic
// Regular expression for validating an email address
$emailRegex = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';

// Regular expression for validating a phone number, starting with '+'
$phoneRegex = '/^\+[0-9]{1,15}$/';

// Regular expression for validating a password with at least one uppercase letter, one lowercase letter, and one number
$passwordRegex = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/';

if (!isset($name) || strlen($name) < 2) {
throw new Exception(Exception::USER_NAME_INVALID);
}

if (is_null($email)) {
throw new Exception(Exception::EMAIL_REQUIRED);
}

if (!is_null($email) && !preg_match($emailRegex, $email)) {
throw new Exception(Exception::USER_EMAIL_INVALID);
}

if (!preg_match($passwordRegex, $password)) {
throw new Exception(Exception::USER_PASSWORD_INVALID);
}


$userId = $userId == 'unique()'
? ID::unique()
: ID::custom($userId);
Expand Down
4 changes: 4 additions & 0 deletions src/Appwrite/Extend/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class Exception extends \Exception
public const USER_OAUTH2_BAD_REQUEST = 'user_oauth2_bad_request';
public const USER_OAUTH2_UNAUTHORIZED = 'user_oauth2_unauthorized';
public const USER_OAUTH2_PROVIDER_ERROR = 'user_oauth2_provider_error';
public const USER_NAME_INVALID = 'user_name_invalid';
public const USER_EMAIL_INVALID = 'user_email_invalid';
public const USER_PASSWORD_INVALID = 'user_password_invalid';
public const EMAIL_REQUIRED = 'email_required';

/** Teams */
public const TEAM_NOT_FOUND = 'team_not_found';
Expand Down