#### Category : middleware

So I made a little chat functionality in laravel using pusher so it updates in real time. I have an event with the following methods I call using broadcast(new MessageSent($message,$conversationId)); whenever a message is sent. public function broadcastOn() { return [‘chat-‘ . $this->conversationId]; } public function broadcastAs() { return ‘new-message’; } And a piece .. Read more AppHttpControllersAdminExampleController.php public function index() { dd(‘index’); } appPoliciesExamplePolicy.php public function viewAny(User$user) { return true; } appProvidersAuthServiceProvider.php protected $policies = [ ‘AppModelsExample’ => ‘AppPoliciesExamplePolicy’, ]; routes/admin.php Route::get(‘example’, [ExampleController::class, ‘index’])->middleware(‘can:viewAny,AppModelsExample’) But the ->middleware(‘can:viewAny,AppModelsExample’) always returns 403, and when I remove it, the dd(‘index’) will run. Have I missed something? Sourc.. Read more I just made a new Route Group for my project that goes like this: Route::group([‘prefix’ => ‘admin’], function(){ Route::resource(‘/movies’, AppHttpControllersMovieController::class); })->middleware(‘auth’); And I wanted to add auth middleware but I get this error: Call to a member function middleware() on null So what is going wrong here ? How can I fix this issue ? .. Read more is it possible to add Laravel middleware that is triggered only when a visitor of the website hits external link? I know how to create a "route middleware", but not for external links. Sourc.. Read more I am working on a project in which I have three type of users Admin and user1 and user2. I want user1 and user2 to able to use certain features in application only if the admin has assigned an invoice to them. I have tried using helper function given below.$invoice = Invoice::pluck(‘user_id’)->toArray(); if (Auth::user()->admin ..

i want to make two authentication roles(admin and user). every thing work fine, but for example when i am logged as user and i try to access the admin dashboard i want want redirected to the user dashboard instead cause i must not have access to it as a user… the problem is when i ..

I am trying to send some data from the handle function in a middleware: <?php namespace AppHttpMiddleware; class LanguageSwitcher { /** * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure$next * @return mixed */ public function handle($request, Closure$next) { if (!Session::has(‘locale’)) { Session::put(‘locale’,Config::get(‘app.locale’)); } App::setLocale(session(‘locale’)); $locale = session(‘locale’); // .. Read more I have some static files in my public folder. – app – config … – public ├ fonts ├ images … And I want to add custom header (CSP) to the response of each static file which available publicly. I’ve already created new middleware to modify response headers globally: App/Http/Middleware/AfterMiddleware.php <?php namespace AppHttpMiddleware; use Closure; .. Read more When I request to login on breeze, user date is passing but auth middleware redirect back to login page. It has also 302 response in result. Do you have any experience on it ? I use Laravel 8 Breeze Package Sourc.. Read more I have two problem: Problem one is when i’m limiting a post request by laravel in-built rate limitter it’s also counting when a validation is failed and the request is no complete and i want to limit it when the request is successfully done. this is my code for rate limit: public function __construct(){$this->middleware(‘throttle:3,10’)->only(‘registerVerification’); ..

{ public function handle($request, Closure$next) { if (! $request->expectsJson()) { abort(response()->json([ ‘success’ => false, ‘data’ => ‘Unauthorize’ ])); } return$next($request); } } I tried this but doesn’t matter whether the condition is true, it nevershows me data i should / souldn’t be able to see (doesn’t matter that I provide a token or .. Read more i have a question here i have created the two AUTH one for admin panel and one for company panel and both have the different tables. The route file is same for the both but with different prefixes and with different middleware’s. But the issue is i am accessing the both the admin and company .. Read more I am pretty new to web development. I am programming native iOS-Apps with Swift for a while. Since a few months I am learning VueJS. In my iOS-Projects Im using AWS as a backend service for Authentication, API, Storage, etc. Now I was reading about ‘middleware’ with Laravel for Authentication. Where is the difference? I .. Read more How to use in one Laravel blade view, information for guests that are not logged in and also for users that are Auth to appear the same stuff but also some other auth commands? Any example in that can be used in controllers and in view would be highly appreciated. I assume that middleware will .. Read more I am building a social app where users can have a conversation with each other. I have a Conversation model which can have 2 or more participants. A Conversation can be viewed by accessing a route with a unique slug ID such as /conversations/HfMSEdsSQF1613152442 How can I restrict access to such a URL to the .. Read more I was trying to set a cookie to define a user-preferred language. I did that by having a link that leads to a helper controller : /set-locale/{locale} public function edit_locale($locale) { $durata= 2628000; // "forever" if (Cookie::has(‘locale’)) { Cookie::queue(Cookie::forget(‘locale’)); // If locale cookie is already defined, delete it } Cookie::queue("locale",$locale, $durata); // Set the .. Read more The checkout process worked fine with Stripe testing, however, I am testing with a real card and everything on Stripe and the database get populated properly BUT when I call$request->user()->subscription() it returns null even though there is a subscription. As shown in the code below, I can manually find the subscription using the user_id ..
I used middleware by defining a function in policy like below: in UserPolicy.php: public function accessUser(User $user) { return$user->inRole(‘admin’); } public function manageUser(User $user, User$secondUser) { return $user->inRole(‘admin’) ||$secondUser->id === $user->id; } in UserController.php: public function __construct() {$this->middleware(‘can:accessUser’, [‘except’ => [‘show’]]); } public function index(Request $request) { … } public function .. Read more how to Create an endpoint named “middleware-counter“ that will be mapped to [email protected] When the request is received by the counter method it should return the number of middlewares added to the endpoint. Sourc.. Read more All I want to do is use a URL with a language parameter that results in the error message which I want to show the user. If I execute the commented code, I get the error; Header may not contain more than a single header, new line detected. On the other hand, if I execute .. Read more I’m trying to send a request to register a user on my vue application However, I keep getting this error: Access to XMLHttpRequest at ‘http://127.0.0.1:8000/register’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. I followed the .. Read more I am doing a project in Laravel. I have a database with posts and users. These posts can be modified and edited by the user who created it and the admin. To do this I created a new field for users, there is an admin and two editor. After limiting the access with the middleware, .. Read more I made a login API and put it in CORS middleware to run it from elsewhere like – Route::group([‘middleware’ => [‘cors’]], function () { Route::post(‘login’, [AuthController::class, ‘login’]); }); The middleware ‘cors’ is written in$routeMiddleware in ‘kernel.php’ file like protected \$routeMiddleware = [ //some other middlewares ‘cors’ => AppHttpMiddlewareCors::class, ]; And in the same file, ..