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

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 ..

Read more

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 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’); ..

Read more

{ 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 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 ..

Read more

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

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 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, ..

Read more