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
awcodes
awcodes9mo ago
you need to check it in a middleware then redirect the user to the appropriate page.
Alnuaimi
AlnuaimiOP9mo ago
How do it plase
awcodes
awcodes9mo ago
You’ll need to look up middleware in the laravel docs and explore that first. I can’t write the code for you. 😃
Alnuaimi
AlnuaimiOP9mo ago
php artisan make:middleware CheckLicenseExpiration ok?
awcodes
awcodes9mo ago
That’s a start, yes.
Alnuaimi
AlnuaimiOP9mo ago
<?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
awcodes
awcodes9mo ago
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
Alnuaimi
AlnuaimiOP9mo ago
protected $routeMiddleware = [ // مدخلات متوسطة أخرى 'checkLicenseExpiration' => \App\Http\Middleware\CheckLicenseExpiration::class, ]; You mean, add this code here
No description
awcodes
awcodes9mo ago
Are you using filament panels?
Alnuaimi
AlnuaimiOP9mo ago
yes
awcodes
awcodes9mo ago
Then there is a middleware modifier in AdminPanelServiceProvider app/Providers/Filament
Alnuaimi
AlnuaimiOP9mo ago
No description
awcodes
awcodes9mo ago
Yea that one Add it to authMiddleware()
Alnuaimi
AlnuaimiOP9mo ago
ok?
No description
awcodes
awcodes9mo ago
Yep You might want it before the authenticate class though.
Alnuaimi
AlnuaimiOP9mo ago
not work
No description
No description
awcodes
awcodes9mo ago
Try adding it in middleware() then. I would expect it to work there though.
Alnuaimi
AlnuaimiOP9mo ago
did you mean this?
No description
awcodes
awcodes9mo ago
Yes.
Alnuaimi
AlnuaimiOP9mo ago
Not even the dd() statement worked.
No description
awcodes
awcodes9mo ago
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().
Alnuaimi
AlnuaimiOP9mo ago
look to this code inside Authenticate not work
No description
awcodes
awcodes9mo ago
That’s not the Authenticate::class that filament uses. So you can’t use that one as a test.
Alnuaimi
AlnuaimiOP9mo ago
No description
awcodes
awcodes9mo ago
Look at the top of the file. Filament uses its own class.
Alnuaimi
AlnuaimiOP9mo ago
No description
awcodes
awcodes9mo ago
Right. That is not the same file as the class in app/Http/Middleware/Authenticate.
Alnuaimi
AlnuaimiOP9mo ago
Can I change to app/Http/Middleware/Authenticate?
awcodes
awcodes9mo ago
No.
Alnuaimi
AlnuaimiOP9mo ago
php filament:make-middleware CheckLicenseExpiration create like this
awcodes
awcodes9mo ago
Just try Log::debug(‘test’) instead of the dd()
Alnuaimi
AlnuaimiOP9mo ago
No description
Alnuaimi
AlnuaimiOP9mo ago
I do , isPersistent: true
No description
Alnuaimi
AlnuaimiOP9mo ago
php artisan optimize after this run dd()
awcodes
awcodes9mo ago
Try php artisan optimize:clear. Maybe there’s a cache issue.
Alnuaimi
AlnuaimiOP9mo ago
this error where create the route ?I have view licenseExpired.blade.php
No description
awcodes
awcodes9mo ago
Not sure. Based on what you shared I would expect it to exist.
Alnuaimi
AlnuaimiOP9mo ago
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
awcodes
awcodes9mo ago
Yea. I’m suprised that isn’t working you defined the ‘licensedExprired’ route in your web.php from what you shared earlier.
delvin28
delvin287mo ago
i am using filament 3.2 not and it does not work too. maybe have to register the middleware somewhere? @awcodes
Want results from more Discord servers?
Add your server