good
This commit is contained in:
@@ -2,75 +2,61 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\PhoneVerification;
|
||||
use App\Enums\OtpVerificationResult;
|
||||
use App\Http\Requests\SendOtpRequest;
|
||||
use App\Http\Requests\VerifyOtpRequest;
|
||||
use App\Models\Coupon;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Services\CouponService;
|
||||
use App\Services\OtpService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
|
||||
class VerificationController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (request()->cookie('device_registered')) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
}
|
||||
private const DEVICE_COOKIE_NAME = 'device_registered';
|
||||
|
||||
if (session()->has('coupon_code')) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
public function __construct(
|
||||
private OtpService $otpService,
|
||||
private CouponService $couponService,
|
||||
) {}
|
||||
|
||||
public function index(): View|RedirectResponse
|
||||
{
|
||||
if ($redirect = $this->redirectIfAlreadyRegistered()) {
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
return view('verification.index');
|
||||
}
|
||||
|
||||
public function sendOtp(Request $request)
|
||||
public function sendOtp(SendOtpRequest $request): RedirectResponse
|
||||
{
|
||||
if ($request->cookie('device_registered')) {
|
||||
if ($request->cookie(self::DEVICE_COOKIE_NAME)) {
|
||||
return back()->withErrors(['phone' => 'Siz eýýäm agza bolduňyz.']);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'phone' => ['required', 'regex:/^\+993 [67]\d \d{2} \d{2} \d{2}$/']
|
||||
], [
|
||||
'phone.regex' => 'Telefon belgiňiz şu formatda bolmaly: +993 KX XX XX XX (bu ýerde K 6 ýa-da 7).'
|
||||
]);
|
||||
|
||||
$phone = unmask_phone($request->phone);
|
||||
$phone = $request->unmaskedPhone();
|
||||
|
||||
$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);
|
||||
|
||||
// Store in DB
|
||||
PhoneVerification::updateOrCreate(
|
||||
['phone' => $phone],
|
||||
[
|
||||
'otp' => $otp,
|
||||
'expires_at' => now()->addMinutes(10),
|
||||
'verified' => false
|
||||
]
|
||||
);
|
||||
|
||||
sendSMS($phone, 'Tassyklaýyş belgi: ' . $otp);
|
||||
if (! $this->otpService->issue($phone)) {
|
||||
return back()->withErrors(['phone' => 'SMS iberilmedi. Soňrak synanyşyň.'])->withInput();
|
||||
}
|
||||
|
||||
// Store phone in session for the next step
|
||||
session()->put('verify_phone', $phone);
|
||||
|
||||
return redirect()->route('verification.verify.form');
|
||||
}
|
||||
|
||||
public function verifyForm()
|
||||
public function verifyForm(): View|RedirectResponse
|
||||
{
|
||||
if (request()->cookie('device_registered')) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
}
|
||||
|
||||
if (session()->has('coupon_code')) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
if ($redirect = $this->redirectIfAlreadyRegistered()) {
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
if (! session()->has('verify_phone')) {
|
||||
@@ -78,105 +64,137 @@ class VerificationController extends Controller
|
||||
}
|
||||
|
||||
return view('verification.verify', [
|
||||
'phone' => session()->get('verify_phone')
|
||||
'phone' => session()->get('verify_phone'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function resendOtp()
|
||||
public function resendOtp(Request $request): RedirectResponse
|
||||
{
|
||||
if ($request->cookie(self::DEVICE_COOKIE_NAME)) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
}
|
||||
|
||||
$phone = session()->get('verify_phone');
|
||||
|
||||
if (!$phone) {
|
||||
if (! $phone) {
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
// Generate a 4-digit OTP
|
||||
$otp = (string) rand(1000, 9999);
|
||||
|
||||
// Store in DB
|
||||
PhoneVerification::updateOrCreate(
|
||||
['phone' => $phone],
|
||||
[
|
||||
'otp' => $otp,
|
||||
'expires_at' => now()->addMinutes(10),
|
||||
'verified' => false
|
||||
]
|
||||
);
|
||||
if (Coupon::query()->where('phone', $phone)->exists()) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
}
|
||||
|
||||
sendSMS($phone, $otp);
|
||||
if (! $this->otpService->issue($phone)) {
|
||||
return back()->withErrors(['otp' => 'SMS iberilmedi. Soňrak synanyşyň.']);
|
||||
}
|
||||
|
||||
return back()->with('status', 'Täze kod iberildi!');
|
||||
}
|
||||
|
||||
public function verifyOtp(Request $request)
|
||||
public function verifyOtp(VerifyOtpRequest $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'phone' => 'required',
|
||||
'otp' => 'required|digits:4'
|
||||
]);
|
||||
$phone = session()->get('verify_phone');
|
||||
|
||||
$verification = PhoneVerification::query()->where('phone', $request->phone)->first();
|
||||
|
||||
if (!$verification || $verification->otp !== $request->otp) {
|
||||
return back()->withErrors(['otp' => 'Nädogry kod.']);
|
||||
if (! $phone) {
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
if ($verification->expires_at < now()) {
|
||||
$result = $this->otpService->verify($phone, $request->string('otp')->toString());
|
||||
|
||||
if ($result === OtpVerificationResult::Expired) {
|
||||
return back()->withErrors(['otp' => 'Kodyň möhleti gutardy.']);
|
||||
}
|
||||
|
||||
// Mark as verified
|
||||
$verification->update(['verified' => true]);
|
||||
if ($result === OtpVerificationResult::Invalid) {
|
||||
return back()->withErrors(['otp' => 'Nädogry kod.']);
|
||||
}
|
||||
|
||||
// Generate or get Coupon
|
||||
$coupon = Coupon::firstOrCreate(
|
||||
['phone' => $request->phone],
|
||||
['code' => $this->generateCouponCode()]
|
||||
);
|
||||
$coupon = $this->couponService->findOrCreateForPhone($phone);
|
||||
|
||||
// Put coupon code in session for the congratulations page
|
||||
session()->put('coupon_code', $coupon->code);
|
||||
|
||||
// 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);
|
||||
return redirect()
|
||||
->route('verification.congratulations')
|
||||
->withCookie($this->makeDeviceCookie($phone));
|
||||
}
|
||||
|
||||
public function congratulations()
|
||||
public function congratulations(): View|RedirectResponse
|
||||
{
|
||||
// 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')) {
|
||||
if (! session()->has('coupon_code') && ! request()->cookie(self::DEVICE_COOKIE_NAME)) {
|
||||
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 (! $code) {
|
||||
$phone = $this->phoneFromDeviceCookie();
|
||||
|
||||
if ($phone) {
|
||||
$coupon = Coupon::query()->where('phone', $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');
|
||||
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 (! $code) {
|
||||
return redirect()
|
||||
->route('verification.index')
|
||||
->withoutCookie(self::DEVICE_COOKIE_NAME);
|
||||
}
|
||||
|
||||
return view('verification.congratulations', [
|
||||
'code' => $code
|
||||
'code' => $code,
|
||||
]);
|
||||
}
|
||||
|
||||
private function generateCouponCode()
|
||||
private function redirectIfAlreadyRegistered(): ?RedirectResponse
|
||||
{
|
||||
// Format X_YYYY (X is integer, Y is capital char)
|
||||
$x = rand(1, 9);
|
||||
$yyyy = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
|
||||
if (request()->cookie(self::DEVICE_COOKIE_NAME) || session()->has('coupon_code')) {
|
||||
return redirect()->route('verification.congratulations');
|
||||
}
|
||||
|
||||
return "{$x}_{$yyyy}";
|
||||
return null;
|
||||
}
|
||||
|
||||
private function makeDeviceCookie(string $phone): Cookie
|
||||
{
|
||||
return cookie(
|
||||
self::DEVICE_COOKIE_NAME,
|
||||
encrypt($phone),
|
||||
60 * 24 * 365 * 5,
|
||||
'/',
|
||||
null,
|
||||
null,
|
||||
true,
|
||||
false,
|
||||
'lax',
|
||||
);
|
||||
}
|
||||
|
||||
private function phoneFromDeviceCookie(): ?string
|
||||
{
|
||||
$value = request()->cookie(self::DEVICE_COOKIE_NAME);
|
||||
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$phone = decrypt($value);
|
||||
|
||||
return is_string($phone) && preg_match('/^6\d{7}$/', $phone) ? $phone : null;
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
52
app/Http/Requests/SendOtpRequest.php
Normal file
52
app/Http/Requests/SendOtpRequest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
class SendOtpRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'phone' => ['required', 'regex:/^\+993 [67]\d \d{2} \d{2} \d{2}$/'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'phone.regex' => 'Telefon belgiňiz şu formatda bolmaly: +993 KX XX XX XX (bu ýerde K 6 ýa-da 7).',
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator(Validator $validator): void
|
||||
{
|
||||
$validator->after(function (Validator $validator): void {
|
||||
if ($validator->errors()->isNotEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (unmask_phone($this->string('phone')->toString()) === '') {
|
||||
$validator->errors()->add('phone', 'Telefon belgiňiz şu formatda bolmaly: +993 KX XX XX XX (bu ýerde K 6 ýa-da 7).');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function unmaskedPhone(): string
|
||||
{
|
||||
return unmask_phone($this->string('phone')->toString());
|
||||
}
|
||||
}
|
||||
23
app/Http/Requests/VerifyOtpRequest.php
Normal file
23
app/Http/Requests/VerifyOtpRequest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class VerifyOtpRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'otp' => ['required', 'digits:4'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user