From 0630647272ad591da4697da0ed1c7104d5abe32f Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Mon, 2 Sep 2024 23:29:48 +0500 Subject: [PATCH] add auth api --- .../ApiAuth/Controllers/ApiAuthController.php | 64 +++++++++++++++++++ .../Repositories/ApiAuthRepository.php | 5 ++ .../ApiAuth/Requests/AuthLoginRequest.php | 25 ++++++++ .../ApiAuth/Requests/AuthRegisterRequest.php | 32 ++++++++++ .../ApiAuth/Requests/AuthVerifyRequest.php | 33 ++++++++++ app/Repos/UserRepo.php | 43 +++++++++++++ routes/api.php | 11 ++-- 7 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 app/Modules/ApiAuth/Controllers/ApiAuthController.php create mode 100644 app/Modules/ApiAuth/Repositories/ApiAuthRepository.php create mode 100644 app/Modules/ApiAuth/Requests/AuthLoginRequest.php create mode 100644 app/Modules/ApiAuth/Requests/AuthRegisterRequest.php create mode 100644 app/Modules/ApiAuth/Requests/AuthVerifyRequest.php create mode 100644 app/Repos/UserRepo.php diff --git a/app/Modules/ApiAuth/Controllers/ApiAuthController.php b/app/Modules/ApiAuth/Controllers/ApiAuthController.php new file mode 100644 index 0000000..d8bbd2b --- /dev/null +++ b/app/Modules/ApiAuth/Controllers/ApiAuthController.php @@ -0,0 +1,64 @@ +phone_number); + + return response()->json([ + 'message' => sprintf('%s: %s', __('Verification code sent to'), $request->phone_number), + ], 201); + } + + /** + * (Auth) Login + */ + public function login(AuthLoginRequest $request): JsonResponse + { + sendSMSVerification($request->phone_number); + + return response()->json([ + 'message' => sprintf('%s: %s', __('Verification code sent to'), $request->phone_number), + ], 201); + } + + /** + * (Auth) Verify the code + */ + public function verify(AuthVerifyRequest $request): JsonResponse + { + $user = User::where('phone_number', $request->phone_number)->firstOrFail(); + + return response()->json([ + 'message' => $user->createToken(bin2hex(random_bytes(20)))->plainTextToken, + ]); + } + + /** + * (Auth)* Delete user + * + * @authenticated + */ + public function delete(): JsonResponse + { + auth()->user()->delete(); + + return response()->json(['message' => 'user deleted successfully']); + } +} diff --git a/app/Modules/ApiAuth/Repositories/ApiAuthRepository.php b/app/Modules/ApiAuth/Repositories/ApiAuthRepository.php new file mode 100644 index 0000000..ab26ca8 --- /dev/null +++ b/app/Modules/ApiAuth/Repositories/ApiAuthRepository.php @@ -0,0 +1,5 @@ + + */ + public function rules(): array + { + return [ + /** + * Phone number to authenticate + * @var int + * @example 65707012 + */ + 'phone_number' => ['required', 'integer', 'between:61000000,71999999'], + ]; + } +} diff --git a/app/Modules/ApiAuth/Requests/AuthRegisterRequest.php b/app/Modules/ApiAuth/Requests/AuthRegisterRequest.php new file mode 100644 index 0000000..f7d1915 --- /dev/null +++ b/app/Modules/ApiAuth/Requests/AuthRegisterRequest.php @@ -0,0 +1,32 @@ + + */ + public function rules(): array + { + return [ + /** + * Phone number to authenticate + * @var int + * @example 65707012 + */ + 'phone_number' => ['required', 'integer', 'between:61000000,71999999', 'unique:users,phone_number'], + + /** + * User's name + * @var string + * @example Mahmyt Allaberdiyev + */ + 'name' => ['required', 'string', 'max:255'], + ]; + } +} diff --git a/app/Modules/ApiAuth/Requests/AuthVerifyRequest.php b/app/Modules/ApiAuth/Requests/AuthVerifyRequest.php new file mode 100644 index 0000000..aa30283 --- /dev/null +++ b/app/Modules/ApiAuth/Requests/AuthVerifyRequest.php @@ -0,0 +1,33 @@ + + */ + public function rules(): array + { + return [ + /** + * Phone number to authenticate + * @var int + * @example 65707012 + */ + 'phone_number' => ['required', 'integer', 'between:61000000,65999999'], + + /** + * Verification code (OTP) + * @var int + * @example 432123 + */ + 'code' => ['required', 'integer', new PhoneCodeVerification($this->phone_number)], + ]; + } +} diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php new file mode 100644 index 0000000..aa2ff10 --- /dev/null +++ b/app/Repos/UserRepo.php @@ -0,0 +1,43 @@ + $request->phone_number, + 'name' => $request->name, + 'username' => static::generateUsername($request->name), + 'locale' => app()->getLocale(), + 'password' => Str::random(6), + 'active' => true, + ]); + } + + /** + * Generate random and unique username + */ + public static function generateUsername(string $name): string + { + // Convert the full name to lowercase and replace spaces with underscores + $username = Str::slug($name, '_'); + + $count = DB::table('users')->where('username', $username)->count(); + + if ($count > 0) { + $username = $username.'_'.($count + 1); + } + + return $username; + } +} diff --git a/routes/api.php b/routes/api.php index 889937e..83f3aa7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,6 @@ get('/user', function (Request $request) { - return $request->user(); -}); +// Auth... +Route::post('auth/register', [ApiAuthController::class, 'register']); +Route::post('auth/login', [ApiAuthController::class, 'login']); +Route::post('auth/verify', [ApiAuthController::class, 'verify']); +Route::middleware('auth:sanctum') + ->post('auth/delete-user', [ApiAuthController::class, 'delete']);