From 43f834c0bdc2829c68859887ee9e2a59df716786 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 24 Jun 2022 12:49:00 +0200 Subject: [PATCH 1/2] feat: add new errors for phone auth --- .env | 2 +- app/config/errors.php | 15 +++++++++++++++ app/controllers/api/account.php | 10 +++++----- src/Appwrite/Extend/Exception.php | 3 +++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/.env b/.env index f9ee06b57a..e9ca6cc70e 100644 --- a/.env +++ b/.env @@ -56,7 +56,7 @@ _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= _APP_SMTP_USERNAME= _APP_SMTP_PASSWORD= -_APP_PHONE_PROVIDER=phone://mock +_APP_PHONE_PROVIDER= _APP_PHONE_FROM= _APP_STORAGE_LIMIT=30000000 _APP_STORAGE_PREVIEW_LIMIT=20000000 diff --git a/app/config/errors.php b/app/config/errors.php index b1e418e517..8420ddbb74 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -48,6 +48,11 @@ return [ 'description' => 'SMTP is disabled on your Appwrite instance. You can learn more about setting up SMTP in our docs.', 'code' => 503, ], + Exception::GENERAL_PHONE_DISABLED => [ + 'name' => Exception::GENERAL_PHONE_DISABLED, + 'description' => 'Phone provider is not configured. Please check the _APP_PHONE_PROVIDER environment variable of your Appwrite server.', + 'code' => 503, + ], Exception::GENERAL_ARGUMENT_INVALID => [ 'name' => Exception::GENERAL_ARGUMENT_INVALID, 'description' => 'The request contains one or more invalid arguments. Please refer to the endpoint documentation.', @@ -170,6 +175,16 @@ return [ 'description' => 'The requested authentication method is either disabled or unsupported. Please check the supported authentication methods in the Appwrite console.', 'code' => 501, ], + Exception::USER_PHONE_ALREADY_EXISTS => [ + 'name' => Exception::USER_PHONE_ALREADY_EXISTS, + 'description' => 'A user with the same phone number already exists in the current project.', + 'code' => 409, + ], + Exception::USER_PHONE_NOT_FOUND => [ + 'name' => Exception::USER_PHONE_NOT_FOUND, + 'description' => 'The current user does not have a phone number associated with their account.', + 'code' => 400, + ], /** Teams */ Exception::TEAM_NOT_FOUND => [ diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 0d5613beac..f61490b116 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -859,7 +859,7 @@ App::post('/v1/account/sessions/phone') ->inject('phone') ->action(function (string $userId, string $number, Request $request, Response $response, Document $project, Database $dbForProject, Audit $audits, Event $events, EventPhone $messaging, Phone $phone) { if (empty(App::getEnv('_APP_PHONE_PROVIDER'))) { - throw new Exception('Phone Disabled', 503, Exception::GENERAL_SMTP_DISABLED); + throw new Exception('Phone provider not configured', 503, Exception::GENERAL_PHONE_DISABLED); } $roles = Authorization::getRoles(); @@ -1596,7 +1596,7 @@ App::patch('/v1/account/phone') try { $user = $dbForProject->updateDocument('users', $user->getId(), $user); } catch (Duplicate $th) { - throw new Exception('Phone number already exists', 409, Exception::USER_EMAIL_ALREADY_EXISTS); + throw new Exception('Phone number already exists', 409, Exception::USER_PHONE_ALREADY_EXISTS); } $audits @@ -2263,12 +2263,12 @@ App::post('/v1/account/verification/phone') ->inject('messaging') ->action(function (Request $request, Response $response, Phone $phone, Document $user, Database $dbForProject, Audit $audits, Event $events, Stats $usage, EventPhone $messaging) { - if (empty(App::getEnv('_APP_SMTP_HOST'))) { - throw new Exception('SMTP Disabled', 503, Exception::GENERAL_SMTP_DISABLED); + if (empty(App::getEnv('_APP_PHONE_PROVIDER'))) { + throw new Exception('Phone provider not configured', 503, Exception::GENERAL_PHONE_DISABLED); } if (empty($user->getAttribute('phone'))) { - throw new Exception('User has no phone number.', 400); + throw new Exception('User has no phone number.', 400, Exception::USER_PHONE_NOT_FOUND); } $roles = Authorization::getRoles(); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 94130f41a3..69cc32b715 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -40,6 +40,7 @@ class Exception extends \Exception public const GENERAL_UNAUTHORIZED_SCOPE = 'general_unauthorized_scope'; public const GENERAL_RATE_LIMIT_EXCEEDED = 'general_rate_limit_exceeded'; public const GENERAL_SMTP_DISABLED = 'general_smtp_disabled'; + public const GENERAL_PHONE_DISABLED = 'general_phone_disabled'; public const GENERAL_ARGUMENT_INVALID = 'general_argument_invalid'; public const GENERAL_QUERY_LIMIT_EXCEEDED = 'general_query_limit_exceeded'; public const GENERAL_QUERY_INVALID = 'general_query_invalid'; @@ -66,6 +67,8 @@ class Exception extends \Exception public const USER_SESSION_NOT_FOUND = 'user_session_not_found'; public const USER_UNAUTHORIZED = 'user_unauthorized'; public const USER_AUTH_METHOD_UNSUPPORTED = 'user_auth_method_unsupported'; + public const USER_PHONE_ALREADY_EXISTS = 'user_phone_already_exists'; + public const USER_PHONE_NOT_FOUND = 'user_phone_not_found'; /** Teams */ public const TEAM_NOT_FOUND = 'team_not_found'; From 600eb0d0d04789f3c6fed1c7e3761e8a4c1f126a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 24 Jun 2022 12:54:30 +0200 Subject: [PATCH 2/2] feat: revert .env change --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index e9ca6cc70e..f9ee06b57a 100644 --- a/.env +++ b/.env @@ -56,7 +56,7 @@ _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= _APP_SMTP_USERNAME= _APP_SMTP_PASSWORD= -_APP_PHONE_PROVIDER= +_APP_PHONE_PROVIDER=phone://mock _APP_PHONE_FROM= _APP_STORAGE_LIMIT=30000000 _APP_STORAGE_PREVIEW_LIMIT=20000000