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()
{
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
]);
}