Majeed
552
2
Laravel

How to add Bearer Token in Request Header Using Laravel Controller or Middleware

Posted 10 months ago by Majeed

I'm working on Laravel API Authentication. for every request I'm added the Bearer Token in request Header. But I want to do this from my Backend controller or middleware. i'm tried this way But it's not working.

My Middleware.

 public function handle($request, Closure $next)
    {
        $request->header('Authorization',"Bearer ".$request->bearerToken());

        return $next($request);
    }

My Kernal File.

protected $middlewareGroups = [

        'Header'  =>[
            \App\Http\Middleware\HeaderMiddleware::class
        ],
    ];

and my Route

Route::group(['middleware' => ['auth:api','Header']], function(){
Route::post('details', 'API\[email protected]');
Route::get('test','API\[email protected]');
});

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.