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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘