diff --git a/app/Http/Controllers/VerificationController.php b/app/Http/Controllers/VerificationController.php index baaf45b..b46921f 100644 --- a/app/Http/Controllers/VerificationController.php +++ b/app/Http/Controllers/VerificationController.php @@ -12,11 +12,23 @@ class VerificationController extends Controller { public function index() { + if (request()->cookie('device_registered')) { + return redirect()->route('verification.congratulations'); + } + + if (session()->has('coupon_code')) { + return redirect()->route('verification.congratulations'); + } + return view('verification.index'); } public function sendOtp(Request $request) { + if ($request->cookie('device_registered')) { + return back()->withErrors(['phone' => 'Siz eýýäm agza bolduňyz.']); + } + $request->validate([ 'phone' => ['required', 'regex:/^\+993 [67]\d \d{2} \d{2} \d{2}$/'] ], [ @@ -24,6 +36,11 @@ class VerificationController extends Controller ]); $phone = unmask_phone($request->phone); + + $existingCoupon = Coupon::query()->where('phone', $phone)->first(); + if ($existingCoupon) { + return back()->withErrors(['phone' => 'Bu telefon belgisi eýýäm ulanyldy.'])->withInput(); + } // Generate a 4-digit OTP $otp = (string) rand(1000, 9999); @@ -38,7 +55,7 @@ class VerificationController extends Controller ] ); - sendSMS($phone, $otp); + sendSMS($phone, 'Tassyklaýyş belgi: ' . $otp); // Store phone in session for the next step session()->put('verify_phone', $phone); @@ -48,6 +65,14 @@ class VerificationController extends Controller public function verifyForm() { + if (request()->cookie('device_registered')) { + return redirect()->route('verification.congratulations'); + } + + if (session()->has('coupon_code')) { + return redirect()->route('verification.congratulations'); + } + if (! session()->has('verify_phone')) { return redirect()->route('verification.index'); } @@ -111,20 +136,38 @@ class VerificationController extends Controller // Put coupon code in session for the congratulations page session()->put('coupon_code', $coupon->code); - // Clear phone from session as verification is complete - session()->forget('verify_phone'); - return redirect()->route('verification.congratulations'); + // We set a long-lived cookie to mark this device as registered (e.g. 5 years) + return redirect()->route('verification.congratulations') + ->cookie('device_registered', 'true', 60 * 24 * 365 * 5); } public function congratulations() { - if (!session()->has('coupon_code')) { + // If they have the cookie but lost the session, we don't know their code unless we query + // But for simplicity based on the flow, we'll try to find it by phone if available + if (!session()->has('coupon_code') && !request()->cookie('device_registered')) { return redirect()->route('verification.index'); } + $code = session()->get('coupon_code'); + + if (!$code && session()->has('verify_phone')) { + $coupon = Coupon::query()->where('phone', session()->get('verify_phone'))->first(); + if ($coupon) { + $code = $coupon->code; + session()->put('coupon_code', $code); + } + } + + // If for some reason code is completely lost (e.g. cookie exists but session cleared and no phone), + // we can't show a specific code. In this specific scenario, we'll default to redirecting back. + if (!$code) { + return redirect()->route('verification.index')->withoutCookie('device_registered'); + } + return view('verification.congratulations', [ - 'code' => session()->get('coupon_code') + 'code' => $code ]); } diff --git a/resources/views/verification/congratulations.blade.php b/resources/views/verification/congratulations.blade.php index 57e87ff..82008db 100644 --- a/resources/views/verification/congratulations.blade.php +++ b/resources/views/verification/congratulations.blade.php @@ -73,9 +73,6 @@ - - Baş sahypa dolan - diff --git a/resources/views/verification/verify.blade.php b/resources/views/verification/verify.blade.php index 87163b8..b93a0a0 100644 --- a/resources/views/verification/verify.blade.php +++ b/resources/views/verification/verify.blade.php @@ -71,10 +71,11 @@
- SMS kody 2 minutdan 5 minuda çenli baryp biler, garaşmagyňyzy Sizden haýyş edýärin. + SMS kody 2 minutdan 5 minuda çenli baryp biler, garaşmagyňyzy Sizden haýyş edýäris.