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)],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
43
app/Repos/UserRepo.php
Normal file
43
app/Repos/UserRepo.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Repos;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class UserRepo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Register new user
|
||||||
|
*/
|
||||||
|
public static function registerUser(Request $request): User
|
||||||
|
{
|
||||||
|
return User::create([
|
||||||
|
'phone_number' => $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use App\Modules\ApiAuth\Controllers\ApiAuthController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -14,6 +14,9 @@ use Illuminate\Support\Facades\Route;
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
// Auth...
|
||||||
return $request->user();
|
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']);
|
||||||
|
|||||||
Reference in New Issue
Block a user