Category : middleware

public function __construct() { $this->middleware(‘auth:api’)->only([‘index’, ‘show’]); $this->middleware([‘auth:api’, ‘role:seeker|admin’])->only(‘store’); $this->middleware([‘auth:api’, ‘role:employer|admin’])->only(‘destroy’); $this->middleware([‘auth:api’, ‘role:admin’])->only(‘update’); } There are three roles in the website, admin, employer, and seeker. I use Laravel Spatie middleware for authorization with the controller constructor. All authenticated users will have access to index() and show() methods. Admin users will have access to all methods. Seekers ..

Read more

this is where I need middlware public function getFileAttribute() { return route(‘backend.file_url’, [ ‘path’ => $this->attributes[‘attachment’], ]); } this is my route Route::get(‘file-url’, ‘[email protected]’)->name(‘file_url’); I want to use middlaware Like this but not in web.php or api.php I need it in controller Route::get(‘/download/{ticket}’, ‘[email protected]’)->name(‘download’)->middleware([‘can:download,ticket’]); Sourc..

Read more

So i have this code in my route : // Facilitator Priviledges Route::group([‘roles’=>’facilitator’],function(){ //Material Route::get(‘/material’, ‘[email protected]’); Route::post(‘/material’, ‘[email protected]’); Route::get(‘/material/{course:id}/create’, ‘[email protected]’); Route::get(‘/material/detail/{course:id}’, ‘[email protected]’); Route::get(‘/material/{material:id}/edit’, ‘[email protected]’); Route::patch(‘/material/{material:id}’, ‘[email protected]’); Route::delete(‘/material/{material:id}’, ‘[email protected]’); }); //Admin Priviledges Route::group([‘roles’=>’admin’],function(){ Route::resource(‘/categories’, ‘CategoriesController’); //Material Route::get(‘/material’, ‘[email protected]’); Route::post(‘/material’, ‘[email protected]’); Route::get(‘/material/{course:id}/create’, ‘[email protected]’); Route::get(‘/material/detail/{course:id}’, ‘[email protected]’); Route::get(‘/material/{material:id}/edit’, ‘[email protected]’); Route::patch(‘/material/{material:id}’, ‘[email protected]’); Route::delete(‘/material/{material:id}’, ‘[email protected]’); }); It has the same route, ..

Read more

I am developing an API Application using Lumen 8. I have 3 types of users. For them, I use 3 different Models. I am using JWT authentication [packege- tymon/jwt-auth]. Here is my Auth.php config. I use 3 types of auth guard return [ ‘defaults’ => [ ‘guard’ => env(‘AUTH_GUARD’, ‘user’), ‘passwords’ => ‘users’, ], ‘guards’ ..

Read more

I have a middleware to rediret to HTTPS like so: <?php namespace AppHttpMiddleware; use Closure; use IlluminateHttpRequest; class HttpsProtocolMiddleware { public function handle($request, Closure $next) { if (!$request->secure() && app()->environment(‘production’)) { dd("need to redirect"); return redirect()->secure($request->getRequestUri()); } return $next($request); } } however it is not working since if go to https://project.domain.com i get a "too ..

Read more

Here is middleware code public function handle($request, Closure $next) { $user = Auth::user(); if (!$user->team->id) { throw new BizlyException(‘forbidden’, [], ‘Access Denied.’); } $team = Team::find($user->team->id); app()->instance(‘team’, $team); app()->instance(‘user’, $user); return $next($request); } So i want to user team in controller’s constructor when I use in controller’s constructor it’s show me error class team does ..

Read more

So i changed config/auth.php to change default auth table from user to accounts like this : ‘defaults’ => [ ‘guard’ => ‘web’, ‘passwords’ => ‘accounts’, ], ‘guards’ => [ ‘web’ => [ ‘driver’ => ‘session’, ‘provider’ => ‘accounts’, ], ‘api’ => [ ‘driver’ => ‘token’, ‘provider’ => ‘accounts’, ‘hash’ => false, ], ], ‘providers’ => ..

Read more

This is for Laravel 8.x with Jetstream/Livewire scaffold having Stancl/Tenancy. The initialization of tenant models or session settings not working right. Either I did not do it right or inbuilt problem. The entire package was built as per instructions of Stencl/tenancy v3.x. I can see the dd(AppUser::all()) as per code outlined below Route::middleware([ ‘web’, InitializeTenancyByDomain::class, ..

Read more

I am trying to configure my site for redirect from http to https. AppServiceProvider.php public function boot() { if($this->app->environment(‘production’)) { URL::forceScheme(‘https’); } .env APP_URL=https://127.0.0.1:8000 I created an HttpsProtocol file in the root App/Http/Middleware/HttpsProtocol.php <?php namespace AppHttpMiddleware; use Closure; class HttpsProtocol { public function handle($request, Closure $next) { if (!$request->secure()) { return redirect()->secure($request->getRequestUri()); } return $next($request); ..

Read more

I am trying to achieve this using middleware web.php Route::get(‘/test’, ‘[email protected]’)->middleware(‘TestLogin’); redirect to /test if session is found Route::get(‘/test1’, ‘[email protected]’)->middleware(‘TestLogin’); redirect to test1 if session is set Middleware -TestLogin public function handle($request, Closure $next)` { if($request->session()->get(‘Username’)) { return redirect()->route(); // what to write here to redirect to the path its being called from } return ..

Read more

I’m trying to listen to simple get param (test param) in laravel middleware. here is my code Route::middleware(‘api’)->get(‘/betshistory’, function (Request $request) { return $request->user(); }); here is my url: http://localhost:8080/api/betshistory?test=45 i tried doing this: print_r($request->parameter(‘test’)); print_r(Route::current()->parameter(‘test’)); print_r($request->route()->paremeters(‘test’)); but i cannot catch it. what am i missing? Thanks Sourc..

Read more

I have a question. Do anyone know how to take the id id this case public function handle($request, Closure $next) { if($request->has(‘id’)) { $notification = auth()->user()->unreadNotifications()->where(‘I need put something here’, $request->id)->first(); if($notification) { $notification->markAsRead(); } } return $next($request); } My table structule is like Id: 4 Notifiable_type AppUser Notifiable_id 1 Data : {"evaluation_id":1} !!! and ..

Read more

I have been struggling to implement jwt authentication. I have 3 guards: ‘guards’ => [ ‘web’ => [ ‘driver’ => ‘session’, ‘provider’ => ‘users’, ], ‘api’ => [ ‘driver’ => ‘token’, ‘provider’ => ‘users’, ], ‘remote’ => [ ‘driver’ => ‘jwt’, ‘provider’ => ‘clients’, ] ], ‘providers’ => [ ‘users’ => [ ‘driver’ => ‘eloquent’, ..

Read more

I have a problem with middleware and sessions in Laravel 8. I can’t get the sessions previously registered in the middleware. The next code is about my middleware, I tested with ->save(), Injection of Session/Store at construct, etc. Nothing work. protected $session; public function __construct(Store $session) { $this->session = $session; } /** * Handle an ..

Read more

i’m in trouble for 2 days with the laravel auth middlewares. I have understand how the Auth system work with JWTtoken. In my controller all work fine i got my user login and i can access with Auth::guard->user(). But i have one problem in my middleware Authenticate.php : <?php namespace AppHttpMiddleware; use IlluminateAuthMiddlewareAuthenticate as Middleware; ..

Read more