From 77fab714a65a77f3aa2245e186fa6d6feace0acb Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Tue, 19 May 2026 19:12:34 +0500 Subject: [PATCH] good job --- .../Controllers/VerificationController.php | 55 ++++++++++++++-- .../verification/congratulations.blade.php | 3 - resources/views/verification/verify.blade.php | 62 +++++++++++++++++-- 3 files changed, 107 insertions(+), 13 deletions(-) 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 @@
-
+ @csrf -
@@ -84,7 +85,7 @@
info

- 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.

@@ -127,5 +128,58 @@ alert('Haýyş, 4 sanly kody giriziň.'); } }); + + // Resend OTP Countdown Logic + const resendBtn = document.getElementById('resend-btn'); + const resendForm = document.getElementById('resend-form'); + const countdownText = document.getElementById('countdown-text'); + let timeLeft = 120; // 2 minutes + + let storedTime = sessionStorage.getItem('resend_timeout'); + + // Reset timer if we just came from a successful resend + const isResent = {{ session('status') ? 'true' : 'false' }}; + if (isResent) { + sessionStorage.setItem('resend_timeout', Date.now().toString()); + storedTime = sessionStorage.getItem('resend_timeout'); + } + + if (storedTime) { + let elapsed = Math.floor((Date.now() - parseInt(storedTime)) / 1000); + timeLeft = Math.max(120 - elapsed, 0); + } else { + sessionStorage.setItem('resend_timeout', Date.now().toString()); + } + + const updateCountdown = () => { + if (timeLeft > 0) { + let m = Math.floor(timeLeft / 60); + let s = timeLeft % 60; + countdownText.innerText = `(0${m}:${s < 10 ? '0' : ''}${s})`; + countdownText.style.display = 'inline'; + timeLeft--; + } else { + countdownText.style.display = 'none'; + } + }; + + updateCountdown(); + let timerId = setInterval(() => { + if (timeLeft > 0) { + updateCountdown(); + } else { + updateCountdown(); // Call one last time to hide it + clearInterval(timerId); + } + }, 1000); + + resendBtn.addEventListener('click', (e) => { + if (timeLeft > 0) { + alert('SMS kody 2 minutdan 5 minuda çenli baryp biler, garaşmagyňyzy Sizden haýyş edýäris.'); + } else { + sessionStorage.removeItem('resend_timeout'); + resendForm.submit(); + } + }); @endsection \ No newline at end of file