1

Am using laravel with android and whenever a 401 error is triggered in laravel i would like to attach to the 401 error a custom header

WWW-Authenticate: xBasic realm=32334

Whenever a 401 response is returned to android i get

com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found

So after aresearch i found out the problem is due to the fact that i need to add a header to the response given i laravel

So am using the default passport oauth/token route which routes are set in authservice provider like

 public function boot()
  {
     $this->registerPolicies();
     Route::group(['middleware'=>'appconnection'], function(){
         Passport::routes();
     });
  }

As from above ive added an appconnection middleware to passport routes now i want to handle the response to check if 401 is ever returned and add the custom header

so in my middleware am stuck at adding the header

class AppConnectionMiddleware
 {
   public function handle($request, Closure $next)
     {
       $returned = $next($request);
         //check if $returned has a 401 status response 
         //am stuck here

     }
 }

So how do i manipulate the response to include the custom response header

1
  • what version of laravel you are using? Commented Mar 27, 2018 at 10:06

1 Answer 1

1
public function handle($request, Closure $next)
{
    $response = $next($request);

    if ($response->status() == 401) {
         $response->header('WWW-Authenticate', 'xBasic realm=32334')
    }

    return $response;
}
Sign up to request clarification or add additional context in comments.

1 Comment

should this be $request->status() or $response->status()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.