Thursday 3 May 2018

Laravel Custom Login Validation

   public function __construct()
   {
      $this->middleware('guest')->except('logout');
   }

  /**
  * Get the failed login response instance.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\RedirectResponse
  */
  protected function sendFailedLoginResponse(Request $request)
  {
      $errors = [$this->username() => trans('auth.failed')];
      // Load user from database
      $user = \App\User::where($this->username(), $request->{$this->username()})->first();
      if($user==null){
        $errors = ['email' => 'Invalid Email. Please try again'];
      }else{
        // Check if user was successfully loaded, that the password matches
        // and active is not 1. If so, override the default error message.
        if (!Hash::check($request->password, $user->password)) {
            $errors = ['password' => 'Invalid Password. Please Try again'];
        }

      }

      if ($request->expectsJson()) {
          return response()->json($errors, 422);
      }
      return redirect()->back()
          ->withInput($request->only($this->username(), 'remember'))
          ->withErrors($errors);
  }

Reference :

https://gist.github.com/SaeedPrez/b3d35af0bb039e8b8d5caf85ec3b2476
https://laravel-news.com/login-validation

No comments:

Post a Comment