good job
This commit is contained in:
@@ -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}$/']
|
||||
], [
|
||||
@@ -25,6 +37,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
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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">
|
||||
Häzir ulan
|
||||
</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>
|
||||
|
||||
<!-- Info Card -->
|
||||
|
||||
@@ -71,10 +71,11 @@
|
||||
</div>
|
||||
</form>
|
||||
<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
|
||||
<button type="submit" class="font-label-md text-label-md text-primary hover:underline transition-all">
|
||||
Kody täzeden iber
|
||||
<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">
|
||||
<span id="countdown-text" class="text-on-surface-variant font-mono text-sm">(02:00)</span>
|
||||
<span>Kody täzeden iber</span>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@@ -84,7 +85,7 @@
|
||||
<div class="flex gap-sm">
|
||||
<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">
|
||||
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>
|
||||
</div>
|
||||
</div>
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@endsection
|
||||
Reference in New Issue
Block a user