Files
online.tbbank.gov.tm-larave…/app/Modules/ApiAuth/Controllers/ApiAuthController.php
2024-09-17 13:13:36 +05:00

73 lines
1.9 KiB
PHP

<?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;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
class ApiAuthController extends Controller
{
/**
* (Auth) Register user
*/
public function register(AuthRegisterRequest $request): JsonResponse
{
UserRepo::registerUser($request);
sendSMSVerification($request->phone);
return response()->json([
'message' => sprintf('%s: %s', __('Verification code sent to'), $request->phone),
], 201);
}
/**
* (Auth) Login
*/
public function login(AuthLoginRequest $request): JsonResponse
{
$user = User::where('phone', $request->phone)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'incorrect' => ['The provided credentials are incorrect.'],
]);
}
sendSMSVerification($request->phone);
return response()->json([
'message' => sprintf('%s: %s', __('Verification code sent to'), $request->phone),
], 201);
}
/**
* (Auth) Verify the code
*/
public function verify(AuthVerifyRequest $request): JsonResponse
{
$user = User::where('phone', $request->phone)->firstOrFail();
return response()->json([
'message' => $user->createToken(bin2hex(random_bytes(20)))->plainTextToken,
]);
}
/**
* (Auth)* Delete user
*/
public function delete(): JsonResponse
{
auth()->user()->delete();
return response()->json(['message' => 'user deleted successfully']);
}
}