255 lines
8.1 KiB
PHP
255 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Client;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Enums\UserEnum;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\Client\Auth\UserRegistrationRequest;
|
|
use App\Http\Requests\Client\Auth\VerifyOtpRequest;
|
|
use App\Services\Auth\AuthService;
|
|
use App\Services\Auth\OtpService;
|
|
use App\Services\Auth\RegistrationService;
|
|
use Inertia\Inertia;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
private AuthService $authService;
|
|
private OtpService $otpService;
|
|
private RegistrationService $registrationService;
|
|
|
|
|
|
public function __construct(AuthService $authService, OtpService $otpService, RegistrationService $registrationService)
|
|
{
|
|
$this->authService = $authService;
|
|
$this->otpService = $otpService;
|
|
$this->registrationService = $registrationService;
|
|
}
|
|
|
|
public function phone()
|
|
{
|
|
return Inertia::client('Auth/Phone');
|
|
}
|
|
|
|
public function checkPhone(Request $request)
|
|
{
|
|
$request->validate([
|
|
'prefix' => 'required|string',
|
|
'phone' => ['required', 'string', new \App\Rules\PhoneNumber]
|
|
]);
|
|
|
|
$prefix = $request->input('prefix');
|
|
$phone = $request->input('phone');
|
|
|
|
$fullPhone = $this->formatFullPhoneNumber($prefix, $phone);
|
|
|
|
$levels = $this->authService->getLevelsForPhone($fullPhone);
|
|
|
|
if (count($levels) === 1) {
|
|
session(['login_phone' => $fullPhone, 'login_level' => $levels[0]]);
|
|
return redirect()->route('password');
|
|
}
|
|
|
|
if (count($levels) > 1) {
|
|
session(['login_phone' => $fullPhone, 'available_levels' => $levels]);
|
|
return redirect()->route('portal');
|
|
}
|
|
|
|
session(['register_phone' => $fullPhone]);
|
|
return redirect()->route('register.chooseType');
|
|
}
|
|
|
|
|
|
private function formatFullPhoneNumber(string $prefix, string $phone): string
|
|
{
|
|
$phone = ltrim($phone, '0');
|
|
|
|
return $prefix . $phone;
|
|
}
|
|
|
|
public function choosePortal(Request $request)
|
|
{
|
|
$phone = session('login_phone');
|
|
$availableLevels = session('available_levels');
|
|
|
|
if (!$phone || !$availableLevels) return redirect()->route('login');
|
|
|
|
if ($request->level && in_array($request->level, $availableLevels)) {
|
|
session(['login_level' => $request->level]);
|
|
return redirect()->route('password');
|
|
}
|
|
|
|
return Inertia::client('Auth/ChoosePortal', compact('phone', 'availableLevels'));
|
|
}
|
|
|
|
public function showPasswordForm()
|
|
{
|
|
$phone = session('login_phone');
|
|
$level = session('login_level');
|
|
|
|
if (!$phone || !$level) return redirect()->route('login');
|
|
|
|
return Inertia::client('Auth/Password', compact('phone', 'level'));
|
|
}
|
|
|
|
public function authenticate(Request $request)
|
|
{
|
|
$request->validate(['password' => 'required|string']);
|
|
|
|
$phone = session('login_phone');
|
|
$level = session('login_level');
|
|
|
|
if (!$phone || !$level) {
|
|
session()->flash('error', 'لطفاً ابتدا شماره موبایل خود را وارد کنید');
|
|
return redirect()->route('login');
|
|
}
|
|
$persianLevel = $this->convertLevelToPersian($level);
|
|
|
|
$user = $this->authService->authenticateByPhoneAndLevel($phone, $persianLevel, $request->password);
|
|
if (!$user) {
|
|
return back()->withErrors(['password' => 'گذرواژه یا شماره موبایل اشتباه است']);
|
|
}
|
|
|
|
return redirect()->route('dashboard.index');
|
|
}
|
|
|
|
public function sendOtp()
|
|
{
|
|
$phone = session('login_phone');
|
|
|
|
if (!$phone) {
|
|
session()->flash('error', 'شماره موبایل یافت نشد.');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
session()->forget(['otp_verified', 'verified_phone']);
|
|
|
|
$this->otpService->sendForPhone($phone, minutes: 3);
|
|
|
|
session()->flash('success', 'کد تایید ارسال شد.');
|
|
return redirect()->route('otpVerification');
|
|
}
|
|
|
|
public function otpVerification()
|
|
{
|
|
$phone = session('login_phone');
|
|
if (!$phone) {
|
|
session()->flash('error', 'شماره موبایل یافت نشد. مجدداً تلاش کنید.');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
return Inertia::client('Auth/OtpVerification', ['phone' => $phone]);
|
|
}
|
|
|
|
public function verifyOtp(VerifyOtpRequest $request)
|
|
{
|
|
$phone = session('login_phone');
|
|
if (!$phone) {
|
|
session()->flash('error', 'شماره موبایل یافت نشد. مجدداً تلاش کنید.');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
$ok = $this->otpService->verifyCode($phone, $request->otp_code);
|
|
|
|
if (!$ok) {
|
|
session()->flash('error', 'کد وارد شده معتبر نیست یا منقضی شده است.');
|
|
return redirect()->back();
|
|
}
|
|
|
|
session(['otp_verified' => true, 'verified_phone' => $phone]);
|
|
session()->flash('success', 'کد تایید با موفقیت تایید شد.');
|
|
return redirect()->route('resetPassword');
|
|
}
|
|
|
|
public function resetPassword()
|
|
{
|
|
if (!session('otp_verified') || !session('verified_phone')) {
|
|
session()->flash('error', 'لطفاً ابتدا شماره موبایل خود را احراز کنید');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
return Inertia::client('Auth/ResetPassword', ['phone' => session('verified_phone')]);
|
|
}
|
|
|
|
public function updatePassword(Request $request)
|
|
{
|
|
if (!session('otp_verified') || !session('verified_phone')) {
|
|
session()->flash('error', 'لطفاً ابتدا شماره موبایل خود را احراز کنید');
|
|
return redirect()->route('login');
|
|
}
|
|
$level = $this->convertLevelToPersian(session('login_level'));
|
|
|
|
$request->validate(['password' => 'required|string|min:8|confirmed']);
|
|
|
|
$user = User::where('phone', session('verified_phone'))
|
|
->where('level', $level)
|
|
->first();
|
|
if (!$user) {
|
|
session()->flash('error', 'کاربر یافت نشد.');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
$user->update(['password' => Hash::make($request->password)]);
|
|
session()->flush();
|
|
|
|
session()->flash('success', 'رمز عبور با موفقیت تغییر یافت.');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
public function chooseType()
|
|
{
|
|
$phone = session('register_phone');
|
|
return Inertia::client('Auth/ChooseRegisterType', ['phone' => $phone]);
|
|
}
|
|
|
|
public function registerSupervisor()
|
|
{
|
|
$phone = session('register_phone');
|
|
return Inertia::client('Auth/RegisterSupervisor', ['phone' => $phone]);
|
|
}
|
|
|
|
public function registerVeterinarian()
|
|
{
|
|
$phone = session('register_phone');
|
|
return Inertia::client('Auth/RegisterVet', ['phone' => $phone]);
|
|
}
|
|
|
|
public function store(UserRegistrationRequest $request)
|
|
{
|
|
$phone = session('register_phone');
|
|
if (!$phone) {
|
|
session()->flash('error', 'لطفاً ابتدا شماره موبایل خود را وارد کنید');
|
|
return redirect()->route('login');
|
|
}
|
|
|
|
$data = array_merge($request->validated(), ['phone' => $phone]);
|
|
|
|
$this->registrationService->registerAndLogin($data);
|
|
|
|
session()->flash('success', 'ثبتنام با موفقیت انجام شد.');
|
|
|
|
|
|
return redirect()->route('dashboard.index');
|
|
}
|
|
|
|
private function convertLevelToPersian($level)
|
|
{
|
|
return match ($level) {
|
|
'supervisor' => UserEnum::Supervisor->value,
|
|
'doctor' => UserEnum::Doctor->value,
|
|
'veterinarian' => UserEnum::Vet->value,
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
Auth::logout();
|
|
session()->flush();
|
|
return redirect()->route('login');
|
|
}
|
|
}
|