How to Add Middleware to check if license expired and redirect to somewhere
Before Run login page I want to check Is the license expired or not? If it is expired Is the license expired or not? If it is expired, another page opens for me. I want an example of that. I just want to know where to write the code.
42 Replies
you need to check it in a middleware then redirect the user to the appropriate page.
How do it plase
You’ll need to look up middleware in the laravel docs and explore that first. I can’t write the code for you. 😃
php artisan make:middleware CheckLicenseExpiration
ok?
That’s a start, yes.
<?php
namespace App\Http\Middleware;
use Closure;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redirect;
class CheckLicenseExpiration
{
public function handle($request, Closure $next)
{
// التحقق من تاريخ انتهاء الترخيص
$expirationDate = Carbon::createFromFormat('Y-m-d', config('app.license_expiration_date'));
if (Carbon::now()->gt($expirationDate)) {
// في حالة انتهاء الترخيص، يتم إعادة توجيه المستخدم إلى صفحة الترخيص المنتهي
return Redirect::route('licenseExpired');
}
// إذا كان الترخيص ساري المفعول، يسمح بالمرور لصفحة الدخول
return $next($request);
}
}
Next, you must register this product as a broker in app/Http/Kernel.php: protected $routeMiddleware = [
// مدخلات متوسطة أخرى
'checkLicenseExpiration' => \App\Http\Middleware\CheckLicenseExpiration::class,
];
after that in web.php Route::middleware(['checkLicenseExpiration'])->group(function () {
Route::get('/login', function () {
return view('login');
});
});
Route::get('/license/expired', function () {
return view('licenseExpired');
})->name('licenseExpired');
// config/app.php
return [
// Other configuration options
'license_expiration_date' => '2024-03-12', // Expiry date example
];
I did all these steps and the code did not work
You need to add the middleware in your filament panel. Unless you are using custom login route.
Route(‘login’) isn’t the filament login route so you can’t do that in web.php
protected $routeMiddleware = [
// مدخلات متوسطة أخرى
'checkLicenseExpiration' => \App\Http\Middleware\CheckLicenseExpiration::class,
]; You mean, add this code here
Are you using filament panels?
yes
Then there is a middleware modifier in AdminPanelServiceProvider
app/Providers/Filament
Yea that one
Add it to authMiddleware()
ok?
Yep
You might want it before the authenticate class though.
not work
Try adding it in middleware() then. I would expect it to work there though.
did you mean this?
Yes.
Not even the dd() statement worked.
Hmm. Not sure then. Seems like something else is going on.
Do you have ray? It might be that the dd isn’t halting since it’s a livewire request. Could also try logging it instead of dd().
look to this code inside Authenticate not work
That’s not the Authenticate::class that filament uses.
So you can’t use that one as a test.
Look at the top of the file. Filament uses its own class.
Right. That is not the same file as the class in app/Http/Middleware/Authenticate.
Can I change to app/Http/Middleware/Authenticate?
No.
php filament:make-middleware CheckLicenseExpiration create like this
Just try Log::debug(‘test’) instead of the dd()
I do , isPersistent: true
php artisan optimize after this run dd()
Try php artisan optimize:clear. Maybe there’s a cache issue.
this error where create the route ?I have view licenseExpired.blade.php
Not sure. Based on what you shared I would expect it to exist.
if (!$this->validateLicense($licensePath, $keyPath)) {
// If the license is invalid or expired, redirect the user to the license expired page
return Redirect::route('licenseExpired');
}
how to Redirect to file licenseExpired.blade.php in resorce view
Yea. I’m suprised that isn’t working you defined the ‘licensedExprired’ route in your web.php from what you shared earlier.
i am using filament 3.2 not and it does not work too. maybe have to register the middleware somewhere? @awcodes