This commit is contained in:
Mekan1206
2026-05-19 19:12:34 +05:00
parent 2ae73dd308
commit 77fab714a6
3 changed files with 107 additions and 13 deletions

View File

@@ -12,11 +12,23 @@ class VerificationController extends Controller
{ {
public function index() 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'); return view('verification.index');
} }
public function sendOtp(Request $request) public function sendOtp(Request $request)
{ {
if ($request->cookie('device_registered')) {
return back()->withErrors(['phone' => 'Siz eýýäm agza bolduňyz.']);
}
$request->validate([ $request->validate([
'phone' => ['required', 'regex:/^\+993 [67]\d \d{2} \d{2} \d{2}$/'] '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); $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 // Generate a 4-digit OTP
$otp = (string) rand(1000, 9999); $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 // Store phone in session for the next step
session()->put('verify_phone', $phone); session()->put('verify_phone', $phone);
@@ -48,6 +65,14 @@ class VerificationController extends Controller
public function verifyForm() 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')) { if (! session()->has('verify_phone')) {
return redirect()->route('verification.index'); return redirect()->route('verification.index');
} }
@@ -111,20 +136,38 @@ class VerificationController extends Controller
// Put coupon code in session for the congratulations page // Put coupon code in session for the congratulations page
session()->put('coupon_code', $coupon->code); 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() 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'); 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', [ return view('verification.congratulations', [
'code' => session()->get('coupon_code') 'code' => $code
]); ]);
} }

View File

@@ -73,9 +73,6 @@
<button class="w-full bg-primary text-on-primary py-md rounded-full font-label-md text-label-md shadow-[0_4px_12px_rgba(0,105,72,0.3)] hover:opacity-90 active:scale-95 transition-all"> <button class="w-full bg-primary text-on-primary py-md rounded-full font-label-md text-label-md shadow-[0_4px_12px_rgba(0,105,72,0.3)] hover:opacity-90 active:scale-95 transition-all">
Häzir ulan Häzir ulan
</button> </button>
<a href="{{ route('verification.index') }}" class="w-full text-center bg-transparent text-primary py-md rounded-full font-label-md text-label-md hover:bg-primary/5 active:scale-95 transition-all">
Baş sahypa dolan
</a>
</div> </div>
<!-- Info Card --> <!-- Info Card -->

View File

@@ -71,10 +71,11 @@
</div> </div>
</form> </form>
<div class="text-center mt-md"> <div class="text-center mt-md">
<form action="{{ route('verification.resend') }}" method="POST" class="inline"> <form action="{{ route('verification.resend') }}" method="POST" class="inline" id="resend-form">
@csrf @csrf
<button type="submit" class="font-label-md text-label-md text-primary hover:underline transition-all"> <button type="button" id="resend-btn" class="font-label-md text-label-md text-primary hover:underline transition-all flex items-center justify-center gap-1 mx-auto">
Kody täzeden iber <span id="countdown-text" class="text-on-surface-variant font-mono text-sm">(02:00)</span>
<span>Kody täzeden iber</span>
</button> </button>
</form> </form>
</div> </div>
@@ -84,7 +85,7 @@
<div class="flex gap-sm"> <div class="flex gap-sm">
<span class="material-symbols-outlined text-on-surface-variant" data-icon="info">info</span> <span class="material-symbols-outlined text-on-surface-variant" data-icon="info">info</span>
<p class="font-body-sm text-body-sm text-on-surface-variant"> <p class="font-body-sm text-body-sm text-on-surface-variant">
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.
</p> </p>
</div> </div>
</div> </div>
@@ -127,5 +128,58 @@
alert('Haýyş, 4 sanly kody giriziň.'); 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();
}
});
</script> </script>
@endsection @endsection