如果数据库中存在电子邮件,请执行用户验证

时间:2015-12-08 17:32:26

标签: php laravel laravel-5 laravel-5.1 basic-authentication

在Laravel中,是否可以仅根据用户电子邮件进行登录 - 无需密码。

我尝试过

public function postSignIn(){

      $validator = Validator::make(Input::only('email') , array(
        'email' => 'required',
      ));

      if ($validator->fails()) {
        return Redirect::to('/')->with('error', 'Please fill out your information ')->withErrors($validator)->withInput();
      }

      $email = strtolower(Input::get('email'));

      $auth = Auth::attempt(array('email' => $email ));

      if ($auth) {
        return Redirect::to('/dashboard')->with('success', 'Hi '. Auth::user()->name .'! You have been successfully logged in.');
      }

      else {
        return Redirect::to('/')->with('error', 'Username/Password Wrong')
          ->with('username', $username)
          ->withErrors($validator);
      }

我一直在:Undefined index: password

我是否需要修改任何类型的Auth驱动程序才能使其正常工作?

对此的任何提示/建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以这样做:

public function postSignIn(){

      $validator = Validator::make(Input::only('email') , array(
        'email' => 'required',
      ));

      if ($validator->fails()) {
        return Redirect::to('/')->with('error', 'Please fill out your information ')->withErrors($validator)->withInput();
      }

      $email = strtolower(Input::get('email'));

      try{
          $user = User::where('email', $email)->firstOrFail();
          Auth::login($user);

          return Redirect::to('/dashboard')->with('success', 'Hi '. Auth::user()->name .'! You have been successfully logged in.');
      }
      catch(ModelNotFoundException $exception){
          return Redirect::to('/')->with('error', 'Username/Password Wrong')
            ->with('username', $username)
            ->withErrors($validator);

      }
}

您可以在here中查看有关如何手动登录用户的更多信息(您必须向下滚动一下)