如何用JSON响应替换默认的auth.basic响应?
我有一个如下所示的路线组:如何用JSON响应替换默认的auth.basic响应?,json,authentication,laravel-5,Json,Authentication,Laravel 5,我有一个如下所示的路线组: Route::group(['prefix' => 'recipe','middleware'=>['auth.basic']], function (){ //Some things to do }); 当凭据无效时,Laravel输出“无效凭据”。如何用自己的JSON响应覆盖此响应?在AuthController中,尝试以下操作: public function postLogin(Request $request) {
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(),
));
}
在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(),
));
}
我刚刚看了一下
照明\Auth\SessionGuard
。方法getBasicResponse()
似乎对失败的登录尝试(使用基本身份验证)的响应负责
但是,如何真正覆盖它似乎有点棘手。您可能需要扩展SessionGuard类并实现自己的getBasicResponse()方法。这是最简单的部分,我还不知道如何实际实例化您自己的保护,而不是默认的保护 我刚刚看了一下
照明\Auth\SessionGuard
。方法getBasicResponse()
似乎对失败的登录尝试(使用基本身份验证)的响应负责
但是,如何真正覆盖它似乎有点棘手。您可能需要扩展SessionGuard类并实现自己的getBasicResponse()方法。这是最简单的部分,我还不知道如何实际实例化您自己的保护,而不是默认的保护