Current File : /home/tradevaly/public_html/app/Http/Middleware/EXPIRE.php |
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
class EXPIRE
{
public function handle(Request $request, Closure $next)
{
// Check if the user is authenticated and has a valid subscription
if ($this->isUserAuthenticatedWithValidSubscription()) {
return $next($request); // Allow the request to continue
}
// If the user is not authenticated or has an expired subscription
$this->showSubscriptionExpiredMessage();
return redirect()->route('user.subs');
}
protected function isUserAuthenticatedWithValidSubscription()
{
// Check if the user is authenticated
if (Auth::guard('web')->check()) {
// Get the user's next_pay attribute as a Carbon instance
$nextPay = Carbon::parse(Auth::guard('web')->user()->next_pay);
// Check if the next_pay date is in the future
if ($nextPay->isFuture()) {
return true; // User has a valid subscription
}
}
return false; // User is not authenticated or has an expired subscription
}
protected function showSubscriptionExpiredMessage()
{
Toastr::error('Package has expired. Please renew your subscription to continue using our service.', '', [
'progressBar' => true,
'closeButton' => true,
'positionClass' => 'toast-top-right',
]);
}
}