add auth api
This commit is contained in:
64
app/Modules/ApiAuth/Controllers/ApiAuthController.php
Normal file
64
app/Modules/ApiAuth/Controllers/ApiAuthController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\ApiAuth\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Modules\ApiAuth\Requests\AuthLoginRequest;
|
||||
use App\Modules\ApiAuth\Requests\AuthRegisterRequest;
|
||||
use App\Modules\ApiAuth\Requests\AuthVerifyRequest;
|
||||
use App\Repos\UserRepo;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ApiAuthController extends Controller
|
||||
{
|
||||
/**
|
||||
* (Auth) Register user
|
||||
*/
|
||||
public function register(AuthRegisterRequest $request): JsonResponse
|
||||
{
|
||||
UserRepo::registerUser($request);
|
||||
|
||||
sendSMSVerification($request->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']);
|
||||
}
|
||||
}
|
||||
5
app/Modules/ApiAuth/Repositories/ApiAuthRepository.php
Normal file
5
app/Modules/ApiAuth/Repositories/ApiAuthRepository.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\ApiAuth\Repositories;
|
||||
|
||||
class ApiAuthRepository {}
|
||||
25
app/Modules/ApiAuth/Requests/AuthLoginRequest.php
Normal file
25
app/Modules/ApiAuth/Requests/AuthLoginRequest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\ApiAuth\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AuthLoginRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
/**
|
||||
* Phone number to authenticate
|
||||
* @var int
|
||||
* @example 65707012
|
||||
*/
|
||||
'phone_number' => ['required', 'integer', 'between:61000000,71999999'],
|
||||
];
|
||||
}
|
||||
}
|
||||
32
app/Modules/ApiAuth/Requests/AuthRegisterRequest.php
Normal file
32
app/Modules/ApiAuth/Requests/AuthRegisterRequest.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\ApiAuth\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AuthRegisterRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
||||
*/
|
||||
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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
33
app/Modules/ApiAuth/Requests/AuthVerifyRequest.php
Normal file
33
app/Modules/ApiAuth/Requests/AuthVerifyRequest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\ApiAuth\Requests;
|
||||
|
||||
use App\Rules\PhoneCodeVerification;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AuthVerifyRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
||||
*/
|
||||
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)],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user