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
awcodes4mo ago
you need to check it in a middleware then redirect the user to the appropriate page.
Alnuaimi
Alnuaimi4mo ago
How do it plase
awcodes
awcodes4mo 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
Alnuaimi4mo ago
php artisan make:middleware CheckLicenseExpiration ok?
awcodes
awcodes4mo ago
That’s a start, yes.
Alnuaimi
Alnuaimi4mo 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
awcodes4mo 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
Alnuaimi4mo ago
protected $routeMiddleware = [ // مدخلات متوسطة أخرى 'checkLicenseExpiration' => \App\Http\Middleware\CheckLicenseExpiration::class, ]; You mean, add this code here
No description
awcodes
awcodes4mo ago
Are you using filament panels?
Alnuaimi
Alnuaimi4mo ago
yes
awcodes
awcodes4mo ago
Then there is a middleware modifier in AdminPanelServiceProvider app/Providers/Filament
Alnuaimi
Alnuaimi4mo ago
No description
awcodes
awcodes4mo ago
Yea that one Add it to authMiddleware()
Alnuaimi
Alnuaimi4mo ago
ok?
No description
awcodes
awcodes4mo ago
Yep You might want it before the authenticate class though.
Alnuaimi
Alnuaimi4mo ago
not work
No description
No description
awcodes
awcodes4mo ago
Try adding it in middleware() then. I would expect it to work there though.
Alnuaimi
Alnuaimi4mo ago
did you mean this?
No description
awcodes
awcodes4mo ago
Yes.
Alnuaimi
Alnuaimi4mo ago
Not even the dd() statement worked.
No description
awcodes
awcodes4mo 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
Alnuaimi4mo ago
look to this code inside Authenticate not work
No description
awcodes
awcodes4mo ago
That’s not the Authenticate::class that filament uses. So you can’t use that one as a test.
Alnuaimi
Alnuaimi4mo ago
No description
awcodes
awcodes4mo ago
Look at the top of the file. Filament uses its own class.
Alnuaimi
Alnuaimi4mo ago
No description
awcodes
awcodes4mo ago
Right. That is not the same file as the class in app/Http/Middleware/Authenticate.
Alnuaimi
Alnuaimi4mo ago
Can I change to app/Http/Middleware/Authenticate?
awcodes
awcodes4mo ago
No.
Alnuaimi
Alnuaimi4mo ago
php filament:make-middleware CheckLicenseExpiration create like this
awcodes
awcodes4mo ago
Just try Log::debug(‘test’) instead of the dd()
Alnuaimi
Alnuaimi4mo ago
No description
Alnuaimi
Alnuaimi4mo ago
I do , isPersistent: true
No description
Alnuaimi
Alnuaimi4mo ago
php artisan optimize after this run dd()
awcodes
awcodes4mo ago
Try php artisan optimize:clear. Maybe there’s a cache issue.
Alnuaimi
Alnuaimi4mo ago
this error where create the route ?I have view licenseExpired.blade.php
No description
awcodes
awcodes4mo ago
Not sure. Based on what you shared I would expect it to exist.
Alnuaimi
Alnuaimi4mo 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
awcodes4mo ago
Yea. I’m suprised that isn’t working you defined the ‘licensedExprired’ route in your web.php from what you shared earlier.
delvin28
delvin282mo 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
More Posts
Testing - how can i test if a from from a suffix action has a certain field?Hello, im making tests on a form which has fields that have suffix actions. Im struggling trying to Override traits in tablesHey! What is the simplest solution to override two traits of Filament tables? I am working on a coluHow to make TextInput accept only 2 decimalsThe price must only contain 2 decimals ```php Forms\Components\TextInput::make('price') ->numeriHow to customize the label for options in CheckboxList?Right now I am getting the options for ChecboxList like: ```php CheckboxList::make('cars') ->optioRepeater set a Variable per FieldHello There I'm creating a Form with a Repeater Element. For each Field in that Repeater I need to Is there a way to disable the tenant registration?I am currently using the tenant methods on the admin panel, tenancy is working. I have a custom `->Unable to use TinyEditor Media ModalI have the problem that I cannot use the media modals on a current Filament V2 project (e.g. the hreThe POST method is not supported for route admin/login. Supported methods: GET, HEAD.login request method is POST. how i can change it ? when i see list of routes admin/login method is Using tab in custom pageHow can I use filter tab in livewire component? I saw this converstaion but got an error. https://giIs it possible to use queryStringIdentifier() for widgets with simple pagination?I'm using the `queryStringIdentifier()` for 2 widget table to resolve the pagination issue, but when