Yes
This commit is contained in:
8
app/Http/Controllers/Controller.php
Normal file
8
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
114
app/Http/Controllers/VerificationController.php
Normal file
114
app/Http/Controllers/VerificationController.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\PhoneVerification;
|
||||
use App\Models\Coupon;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class VerificationController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return view('verification.index');
|
||||
}
|
||||
|
||||
public function sendOtp(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'phone' => ['required', 'regex:/^\+993 [67]\d \d{2} \d{2} \d{2}$/']
|
||||
], [
|
||||
'phone.regex' => 'The phone number must be in the format: +993 KX XX XX XX (where K is 6 or 7).'
|
||||
]);
|
||||
|
||||
$phone = $request->phone;
|
||||
|
||||
// Generate a 4-digit OTP
|
||||
$otp = (string) rand(1000, 9999);
|
||||
|
||||
// Log it to simulate SMS
|
||||
Log::info("OTP for {$phone} is {$otp}");
|
||||
|
||||
// Store in DB
|
||||
PhoneVerification::updateOrCreate(
|
||||
['phone' => $phone],
|
||||
[
|
||||
'otp' => $otp,
|
||||
'expires_at' => now()->addMinutes(10),
|
||||
'verified' => false
|
||||
]
|
||||
);
|
||||
|
||||
// Store phone in session for the next step
|
||||
session()->put('verify_phone', $phone);
|
||||
|
||||
return redirect()->route('verification.verify.form');
|
||||
}
|
||||
|
||||
public function verifyForm()
|
||||
{
|
||||
if (!session()->has('verify_phone')) {
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
return view('verification.verify', [
|
||||
'phone' => session()->get('verify_phone')
|
||||
]);
|
||||
}
|
||||
|
||||
public function verifyOtp(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'phone' => 'required',
|
||||
'otp' => 'required|digits:4'
|
||||
]);
|
||||
|
||||
$verification = PhoneVerification::query()->where('phone', $request->phone)->first();
|
||||
|
||||
if (!$verification || $verification->otp !== $request->otp) {
|
||||
return back()->withErrors(['otp' => 'Invalid OTP.']);
|
||||
}
|
||||
|
||||
if ($verification->expires_at < now()) {
|
||||
return back()->withErrors(['otp' => 'OTP has expired.']);
|
||||
}
|
||||
|
||||
// Mark as verified
|
||||
$verification->update(['verified' => true]);
|
||||
|
||||
// Generate or get Coupon
|
||||
$coupon = Coupon::firstOrCreate(
|
||||
['phone' => $request->phone],
|
||||
['code' => $this->generateCouponCode()]
|
||||
);
|
||||
|
||||
// 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');
|
||||
}
|
||||
|
||||
public function congratulations()
|
||||
{
|
||||
if (!session()->has('coupon_code')) {
|
||||
return redirect()->route('verification.index');
|
||||
}
|
||||
|
||||
return view('verification.congratulations', [
|
||||
'code' => session()->get('coupon_code')
|
||||
]);
|
||||
}
|
||||
|
||||
private function generateCouponCode()
|
||||
{
|
||||
// Format X_YYYY (X is integer, Y is capital char)
|
||||
$x = rand(1, 9);
|
||||
$yyyy = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
|
||||
|
||||
return "{$x}_{$yyyy}";
|
||||
}
|
||||
}
|
||||
10
app/Models/Coupon.php
Normal file
10
app/Models/Coupon.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Coupon extends Model
|
||||
{
|
||||
protected $fillable = ['phone', 'code'];
|
||||
}
|
||||
14
app/Models/PhoneVerification.php
Normal file
14
app/Models/PhoneVerification.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PhoneVerification extends Model
|
||||
{
|
||||
protected $fillable = ['phone', 'otp', 'expires_at', 'verified'];
|
||||
protected $casts = [
|
||||
'expires_at' => 'datetime',
|
||||
'verified' => 'boolean'
|
||||
];
|
||||
}
|
||||
32
app/Models/User.php
Normal file
32
app/Models/User.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
24
app/Providers/AppServiceProvider.php
Normal file
24
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user