donglu5235 2016-01-16 11:14
浏览 52
已采纳

在laravel 5.2中覆盖自定义登录错误消息

I've been struggling with laravel 5.2 login function messages. I've override the default sendFailedLoginResponse function in AuthController which works for failed attempts.

But I need to override the validate function response as well which I could not figure out how to do that. Also I do not want to override the default login functionality in the AuthContrller and want to stick with the same login function.

The reason for overriding the validate function is that am making an angular app and want the response in json format with some custom keys.

Currently default login function in Illuminate\Foundation\Auth\AuthenticateUsers.php

public function login(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
    ]);
    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->getCredentials($request);

    if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    if ($throttles) {
        $this->incrementLoginAttempts($request);
    }

    return $this->sendFailedLoginResponse($request);
}

I want the response something like in the below sendFailedResponse function in AuthController

 /**
 * Get failed request response
 *
 * @param null
 * @return null
 */
public function sendFailedLoginResponse()
{
    return response()->json( [ 'status' => false, 'message' => $this->getFailedLoginMessage() ]);
}

Thanks

  • 写回答

2条回答 默认 最新

  • doubo4336 2016-01-27 06:12
    关注

    I came up with the solution by implementing JWT for authentication which I could think of is the best solution with Client side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!