Category : middleware

When I’m creating new users and assign them roles and permissions it works fine, but when I assign role using protected route Route::post(‘/excel/upload’, [ExcelController::class,’upload’])->name(‘uploadExcel’)->middleware(‘auth:sanctum’); i get and error: SpatiePermissionExceptionsGuardDoesNotMatch The given role or permission should use guard web instead of sanctum. auth.php ‘defaults’ => [ ‘guard’ => ‘web’, ‘passwords’ => ‘users’, ], ‘guards’ => [ ..

Read more

in my routes/web/php i have grouped routes fot admin users like that Route::group([‘middleware’ => [‘AdminAuth’]],function(){ Route::get(‘home’, ‘[email protected]’); Route::post(‘changePassword’, ‘[email protected]’); Route::post(‘settings’, ‘[email protected]’); Route::get(‘getorder’, ‘[email protected]’); I added another middleware and wanted to pass different admin users to different controllers Route::middleware([‘RestaurantAdmin’])->group(function () { Route::get(‘home’, ‘[email protected]’); }); this is how Middleware looks like class AdminAuth { public function handle($request, ..

Read more

I know this is a basic laravel question but don’t know how do it. How can I add one route to 2 different middleware (auth) without having to duplicate it? // =admin Route::group([‘middleware’ => [‘auth’]], function() { Route::get(‘/dashboard’, ‘[email protected]’)->name(‘dashboard’); Route::get(‘make-a-sale’, [PurchasesController::class, ‘index’])->name(‘make-a-sale.index’); }); // =cashier Route::group([‘middleware’ => [‘auth’, ‘role:cashier’]], function() { Route::get(‘/dashboard/cashier/profile’, ‘[email protected]’)->name(‘dashboard.cashier.profile’); Route::get(‘make-a-sale’, [PurchasesController::class, ..

Read more

I have the following middleware in my PostController. public function __construct() { $this->middleware(‘auth’)->except([‘index’, ‘show’]); } I understand any authenticated user can still edit a post by visiting localhost/posts/{post}/edit so I’ve protected that by the following code. public function edit(Post $post) { if(auth()->user()->id === $post->user_id){ $categories = Category::all(); return view(‘edit-post’, compact([‘post’, ‘categories’])); } else{ abort(403, ‘Unauthorized.’); ..

Read more

My login controller public function authenticate(Request $request) { $credentials = $request->validate([ ‘username’ => [‘required’], ‘password’ => [‘required’], ]); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended(‘/’); } return back()->withErrors([ ‘error’ => ‘The provided credentials do not match our records.’, ]); } middleware that i check the auth class AuthorizeUser { /** * Handle an incoming request. * ..

Read more

I’m facing login page again and again when try to go to the homepage. I didn’t add any middleware to homepage route but still I’m facing this issue. My Login Controller protected $redirectTo = ‘/’; public function __construct() { $this->middleware(‘guest’)->except(‘logout’); } public function redirectToProvider() { return Socialite::driver(request()->provider)->redirect(); } public function handleProviderCallback() { $provider = request()->provider; ..

Read more

I’m trying to use middlewares to protect Routes based . But I think I’m doing something wrong in the process. This is the Route I have and I want only the users that have administrador role to be able to enter it. Route::get(‘/gestionarMedicos’, [PersonaController::class,’mostrarMedicos’])->name(‘personaMostrarMedicos’)->middleware(‘auth’,’firstLogin’,’role:administrador’); This is my middleware (in the route its the one called ..

Read more

I’m trying to use middlewares to protect Routes based . But I think I’m doing something wrong in the process. This is the Route I have and I want only the users that have administrador role to be able to enter it. Route::get(‘/gestionarMedicos’, [PersonaController::class,’mostrarMedicos’])->name(‘personaMostrarMedicos’)->middleware(‘auth’,’firstLogin’,’role:administrador’); This is my middleware (in the route its the one called ..

Read more

I’m trying to use middlewares to protect Routes based on role. Im testing with this route to allow only the role administrador to be able to enter it. Route::get(‘/gestionarMedicos’, [PersonaController::class,’mostrarMedicos’])->name(‘personaMostrarMedicos’)->middleware(‘auth’,’firstLogin’,’role:administrador’); This is code of my middleware (in the route its the one called route) <?php namespace AppHttpMiddleware; use Closure; use IlluminateHttpRequest; class EnsureUserHasRole { /** ..

Read more

My laravel middleware is behaving funny in that anytime I use a middleware, either custom or any default laravel middleware, I get this error Object of class stdClass could not be converted to string and whenever I trace the file the error is coming from, it targets /vendor/laravel/framework/src/Illuminate/Database/Connection.php. please, if you know what could be ..

Read more

staticTokenVerify is a new middleware i’ve created. Both auth:api & staticTokenVerify middlewares works fine for this route when only one of them is allowed like ‘middleware’ => ‘staticTokenVerify’, & ‘middleware’ => ‘auth:api’, . But when these 2 are passed as an array to allow both middlewares for the route, only the auth:api middleware is working. ..

Read more

$token->token has a value like this – "djdsjfhfj" . Although am inserting the same string as input token, am getting the error msg "unauthenticated" , that i’ve defined for token mismatch case. How can i fix this? class StaticTokenAuthentication { public function handle(Request $request, Closure $next) { $token = StaticTokens::where(‘appName’, ‘contentQ’)->first(); if ($request->input(‘token’) != $token->token) ..

Read more

Laravel in SetLocale Middleware Auth::user is empty. But in the controller returns User data I have Kernal Configuration as below ‘web’ => [ AppHttpMiddlewareEncryptCookies::class, IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class, IlluminateSessionMiddlewareStartSession::class, IlluminateSessionMiddlewareAuthenticateSession::class, IlluminateViewMiddlewareShareErrorsFromSession::class, AppHttpMiddlewareVerifyCsrfToken::class, IlluminateRoutingMiddlewareSubstituteBindings::class, AppHttpMiddlewareAuthenticate::class, AppHttpMiddlewareSetLocale::class, AppHttpMiddlewareCheckForMaintenanceMode::class, ], In the SetLocale I am trying to access Auth::user But it returns empty. In the controller I found the Auth::user data ..

Read more

I was cleaning my Kernel.php from unused route middleware when I noticed the following declaration: ‘can’ => IlluminateAuthMiddlewareAuthorize::class, I double-checked Kernel.php and all the routes: the can middleware was not used anywhere. So I decided to delete it. But the next moment my test suite failed with the following error: { "message": "Target class [can] ..

Read more

Im using laravel 8 with jetstream authentication and bongodb before changing database from mysql to mongodb (using jenssegers/laravel-mongodb) it was everything work fine but when i use mongodb every post methode doesn’t work it give me this erreur (‘419 PAGE EXPIRED’) i know where is the probleme exactlty is in this function namespace IlluminateFoundationHttpMiddleware; protected ..

Read more

I get the following error when adding to the command line like this: error: Call to undefined method IlluminateDatabaseEloquentBuilder::mapInto() This is my code: If i leave out the command line Project::where(‘user_id’, auth()->user()->id) go and replace it with the command line Project::all() everything works fine but i want to get the list of projects belonging to ..

Read more

I am facing an issue in laravel ,where I have role_permission_table: here is the structure of that : migration file: Schema::create(‘role_permissions’, function (Blueprint $table) { $table->id(); $table->unsignedBigInteger(‘role_id’); $table->unsignedBigInteger(‘Item_id’); $table->unsignedBigInteger(‘permission_id’); $table->foreign(‘role_id’)->references(‘id’)->on(‘roles’); $table->foreign(‘Item_id’)->references(‘id’)->on(‘items’); $table->foreign(‘permission_id’)->references(‘id’)->on(‘permissions’); $table->timestamps(); }); And here is the DB table of that in PHPMyAdmin: enter image description here Now I have a role based on ..

Read more