File: /var/www/html/app/Http/Middleware/CheckIfSeller.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Symfony\Component\HttpFoundation\Response;
class CheckIfSeller
{
public function handle(Request $request, Closure $next): Response
{
$user = Session::get('user');
// Check if the user exists in the session
if (!$user) {
return redirect('/login')->with('status', 'Please log in to continue.');
}
// Verify phone status for the user
$phoneStatus = DB::table('seller_nins')->where('user_id', $user['id'])->whereNull('phone_verified_at')->first();
// Redirect if the user is not a seller or has a verified phone
if ($user['role'] != 'seller') {
return redirect('/nin-registration')->with('error', 'You are yet to verify your NIN and phone number');
}
if ($phoneStatus) {
Session::put('user_phone', $user['phone']);
return redirect('/verify-phone')->with('error', 'You are yet to verify your phone number');
}
return $next($request);
}
}