简体   繁体   English

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

[英]Overriding custom login error messages in laravel 5.2

I've been struggling with laravel 5.2 login function messages. 我一直在挣扎laravel 5.2登录功能消息。 I've override the default sendFailedLoginResponse function in AuthController which works for failed attempts. 我已经覆盖了sendFailedLoginResponse中的默认sendFailedLoginResponse函数,该函数可用于失败的尝试。

But I need to override the validate function response as well which I could not figure out how to do that. 但是我也需要重写validate函数的响应,我无法弄清楚该怎么做。 Also I do not want to override the default login functionality in the AuthContrller and want to stick with the same login function. 另外,我也不想覆盖AuthContrller中的默认登录功能,并且希望坚持使用相同的login功能。

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. 覆盖validate函数的原因是正在制作一个有角度的应用程序,并希望使用一些自定义键以json格式进行响应。

Currently default login function in Illuminate\\Foundation\\Auth\\AuthenticateUsers.php 当前默认的login功能在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 我想要类似sendFailedResponse中以下sendFailedResponse函数的AuthController

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

Thanks 谢谢

I don't know anything about Angular and handling json on laravel but I had similar problem while creating custom error message for postLogin function. 我对Angular和在laravel上处理json一无所知,但是在为postLogin函数创建自定义错误消息时遇到了类似的问题。 take a look at this code, perhaps you could do something within form request. 看一下这段代码,也许您可​​以在表单请求中执行某些操作。

This is my AuthController.php 这是我的AuthController.php

use App\Http\Requests\LoginFormRequest;

/**
 * Handle a login request to the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function postLogin(LoginFormRequest $request)
{
    return $this->login($request);
}

Try using form request on the function postLogin 尝试在函数postLogin上使用表单请求

class LoginFormRequest extends Request
{
  /**
   * Determine if the user is authorized to make this request.
   *
   * @return bool
   */
  public function authorize()
  {
    return true;
  }

  /**
   * Get the validation rules that apply to the request.
   *
   * @return array
   */
  public function rules()
  {
    return [
        'email'            => 'required|email',
        'password'         => 'required|min:6',
    ];
  }

  public function messages()
  {
    return [
        'required'  => 'Your :attribute is required.',
        'min'  => ':attribute must be at least :min characters in length.',
        'email' => 'Please type valid email address.',
    ];
  }
}

我通过实现JWT进行身份验证提出了该解决方案,我认为这是客户端的最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM