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

时间:2016-01-16 11:14:32

标签: php angularjs laravel laravel-5.1

我一直在努力使用laravel 5.2登录功能消息。我已经覆盖了AuthController中的默认sendFailedLoginResponse函数,该函数适用于失败的尝试。

但是我需要覆盖validate函数响应,我无法弄清楚如何做到这一点。另外,我不想覆盖AuthContrller中的默认登录功能,并希望坚持使用相同的login功能。

覆盖validate函数的原因是我正在创建一个有角度的应用程序,并希望使用一些自定义键以json格式进行响应。

目前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);
}

我希望响应类似于sendFailedResponse

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

由于

2 个答案:

答案 0 :(得分:0)

我对laular没有任何了解和在laravel上处理json但我在为postLogin函数创建自定义错误消息时遇到了类似的问题。看看这段代码,也许你可以在表单请求中做一些事情。

这是我的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);
}

尝试在函数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.',
    ];
  }
}

答案 1 :(得分:0)

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

相关问题