我有一個看起來像這樣的路線組:「證書無效」如何用JSON響應替換默認的auth.basic響應?
Route::group(['prefix' => 'recipe','middleware'=>['auth.basic']], function(){
//Some things to do
});
當憑證無效Laravel輸出我如何用自己的JSON響應覆蓋此響應?
我有一個看起來像這樣的路線組:「證書無效」如何用JSON響應替換默認的auth.basic響應?
Route::group(['prefix' => 'recipe','middleware'=>['auth.basic']], function(){
//Some things to do
});
當憑證無效Laravel輸出我如何用自己的JSON響應覆蓋此響應?
在AuthController,試試這個:
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required', 'password' => 'required',
]);
$credentials = [
'email' => $request->input('email'),
'password' => $request->input('password')
];
if (Auth::attempt($credentials, $request->has('remember')))
{
return redirect()->intended($this->redirectPath())
->with('success', 'You are successfully logged in');
}
return Response::json(array(
'success' => false,
'errors' => $this->getFailedLoginMessage(),
));
}
我只是看看Illuminate\Auth\SessionGuard
。方法getBasicResponse()
似乎負責失敗登錄嘗試的響應(使用基本身份驗證)。
protected function getBasicResponse()
{
$headers = ['WWW-Authenticate' => 'Basic'];
return new Response('Invalid credentials.', 401, $headers);
}
如何實際覆蓋它似乎有點棘手,但。您可能需要擴展SessionGuard類並實現您自己的getBasicResponse()方法。這很容易,如何真正實例化你自己的後衛,而不是默認的,我還不知道。