Category : laravel-middleware

I’m working on multi-language project using laravel 8.0. Im set the locale on routs but it doesn’t work, shows: 404 not found This is routes/web.py: Route::group([‘prefix’ => ‘{locale}’], function(){ Route::get(‘/’, function() { return redirect()->route(‘login’); })->middleware(‘setLocale’); }); This is Middleware/SetLocale.php: <?php namespace AppHttpMiddleware; use Closure; use IlluminateHttpRequest; class SetLocale { /** * Handle an incoming request. ..

Read more

I’m trying to hit a route that needs to support multiple scopes for auth. It currently requires a scope called manage-clicks-creatives, but I would like to also allow manage-all-clicks-creatives. The Laravel docs seem to indicate that this is possible: The scope middleware may be assigned to a route to verify that the incoming request’s access token has at ..

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

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

After adding the auth:api middleware, my route suddenly stops calling the function it is supposed to. When I remove the middleware, it starts working again. api.php route Route::middleware(‘auth:api’)->get(‘/addloancontroller’, [AppHttpControllersAddLoan::class, ‘store’]) ->name(‘addLoan’); AddLoan.php Controller <?php namespace AppHttpControllers; use AppModelsEarningsRecord; use AppModelsLender; use AppModelsLoanLenders; use AppModelsLoans; use AppModelsLogin; use AppProvidersRouteServiceProvider; use IlluminateHttpRequest; use Auth; use IlluminateSupportFacadesDB; class ..

Read more

I’m trying to set a value in the $request inside my BaseController.php and get the value back on a Middleware class: // BaseController.php public function __construct() { $this->middleware(function ($request, $next) { // I need this inline middleware so I can set common values in my base controller and use it in my other controllers like ..

Read more

//kernel.php protected $routeMiddleware = [ ‘auth’ => AppHttpMiddlewareAuthenticate::class, ‘auth.basic’ => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class, ‘bindings’ => IlluminateRoutingMiddlewareSubstituteBindings::class, ‘cache.headers’ => IlluminateHttpMiddlewareSetCacheHeaders::class, ‘can’ => IlluminateAuthMiddlewareAuthorize::class, ‘guest’ => AppHttpMiddlewareRedirectIfAuthenticated::class, ‘signed’ => IlluminateRoutingMiddlewareValidateSignature::class, ‘throttle’ => IlluminateRoutingMiddlewareThrottleRequests::class, ‘verified’ => IlluminateAuthMiddlewareEnsureEmailIsVerified::class, ]; what should i use auth or guest middleware for authentication ? what is the difference the difference between auth and guest middleware? ..

Read more

it always return 401 unauthorized even I have an access token. In my local computer it is working fine, but in production server (production has private IP to private IP connection with the database server). I don’t know if the problem is in the config of the server. I already check the database connection, it ..

Read more

If i understand right, the CreateFreshApiToken-middleware uses a cookie-based authentication mechanism (https://laravel.com/docs/8.x/passport#consuming-your-api-with-javascript). Accordingly the use of csrf-protection makes sense. What exactly is the difference between using the CreateFreshApiToken-middleware or simply accessing my API through the web-middleware, since both seem to be stateful. In my scenario im working on a laravel-module (https://nwidart.com/laravel-modules/v6/introduction) which is supposed to ..

Read more

Problem Statement: I’m trying modify Multiple Authentication to authenticate different user by using Multiple Models, route, guards and Controller. The user is redirected to teacher/home instead of v2/teacher/home teacher/home is earlier route whereas v2/teacher/home is new modified route. Below is Directory Structure – app/ – Http/ – Controllers/ – Teacher/ – LoginController.php – RegisterController.php – ..

Read more

Problem Statement: I’m trying modify Multiple Authentication to authenticate different user by using Multiple Models, route, guards and Controller. The user is redirected to teacher/home instead of v2/teacher/home teacher/home is earlier route whereas v2/teacher/home is new modified route. Below is Directory Structure – app/ – Http/ – Controllers/ – Teacher/ – LoginController.php – RegisterController.php – ..

Read more

Fairly new to Laravel and tried to make a Login page. The Auth was able to store the user, but it keeps logging me out when I’m going to HomeController where it had ‘auth’ middleware web.php Route::get(‘/login’, [LoginController::class, ‘login’])->name(‘login’); Route::post(‘/check_user’, [LoginController::class, ‘check_user’])->name(‘check_user’); Route::get(‘/home’, [HomeController::class, ‘index’])->name(‘home’); It going fairly well and using dd() I can still ..

Read more

I am new to Laravel 8 and I am using Spatie for roles and permissions. I want to restrict some methods based on the role of the logged-in user. Here is the code in my constructor: function __construct(){ $this->middleware(‘role:instructor’, [‘only’ => [‘index’]]); } If the logged-in user’s role is instructor then I only want to ..

Read more

I have my controller ExampleController: class ExampleController extends Controller { function __construct() { $this->middleware(‘auth:student’)->only([‘store’, ‘update’, ‘destroy’]); } public function index() { if(CheckUser::student()) { dd("Is student"); } dd("Isn’t student"); } /** * Another method’s not relevant. **/ } I’m trying to add some logic if is student. But have one problem, I just can access: Auth::user() ..

Read more