Laravel:Auth :: attempt - 为什么身份验证失败?

时间:2015-02-23 09:38:09

标签: laravel-4 laravel-validation

我用条件验证用户,例如

  

用户角色= 1 登录成功,否则登录失败

现在我正在做的是:

if (Auth::attempt(array('email' => $email, 'password' => $password, 'role' => 1)))
{
    // Login User
} else {
    return Redirect::route('admin.login')
        ->with('message', 'Unauthorized user Or wrong email / password combination');
}

它工作正常,但是当身份验证失败时,我希望能够弄清楚它失败的原因,这样我就可以根据身份验证失败的原因显示以下消息:

  • 不存在的电子邮件地址
  • 密码错误
  • 用户角色不正确。

有没有办法做这些事情?

5 个答案:

答案 0 :(得分:3)

诀窍是在失败的部分中使用Auth::validate()。您必须做几个验证才能解决问题

if (Auth::attempt(array('email' => $email, 'password' => $password, 'role' => 1)))
{
    // Login User
} else {
    Auth::validate(array('email' => $email, 'password' => $password)) {
           // Wrong role
 } else { 
    if (User::where('email', $email)->first()) {
           // Wrong password
    }  else {
       // Wrong username
    } 
}

答案 1 :(得分:1)

首先尝试验证然后尝试登录

  Validating Multiple Fields 

http://laravel.com/docs/4.2/validation

答案 2 :(得分:0)

我希望这可能会有所帮助

public function postSignIn() {
    // checking for  validation
    $validator = Validator::make(Input::all(), array(
                'email'     => 'required|email',
                'password'  => 'required'

    ));
    //if validation fails
    if ($validator->fails()) {
            return Redirect::route('sign-In')
                            ->withErrors($validator)
                            ->withInput();
 } else {
        $auth = Auth::attempt(array(
                    'email' => Input::get('email'),
                    'password' => Input::get('password'),
                    'active' => 1,
                        ));

        if ($auth) {
                return Redirect::route('home');
       } else {
                return Redirect::route('sign-In')->with('fails', 'Email/password wrong, or account not activated.') ;
            }
视图上的

在每个字段的顶部或下面写下这些响应验证,其中字段名称是电子邮件是字段名称

      @if($errors->has('email'))
        {{$errors->first('email')}}
      @endif


      @if($errors->has('password'))
        {{$errors->first('passwrd')}}
      @endif

因为我是laravel的新手,所以我一直在使用更长的过程

答案 3 :(得分:0)

这是laravel用于auth目的的函数。 如果你在这里看到:vendor / laravel / framework / src / illuminate / Auth / Guard.php

public function attempt(array $credentials = array(), $remember = false, $login = true)
{
    $this->fireAttemptEvent($credentials, $remember, $login);

    $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);

    // If an implementation of UserInterface was returned, we'll ask the provider
    // to validate the user against the given credentials, and if they are in
    // fact valid we'll log the users into the application and return true.
    if ($this->hasValidCredentials($user, $credentials))
    {
        if ($login) $this->login($user, $remember);

        return true;
    }

    return false;
}

如果您通过源查看此函数,请使用hasValidCredentials函数 如果你进一步挖掘功能是:

protected function hasValidCredentials($user, $credentials)
    {
        return ! is_null($user) &&     $this->provider->validateCredentials($user, $credentials);
    }

并且在UserProviderInterface界面中你看到这个界面已被定义,你可以自己实现它并返回auth问题的原因而不是false或true

答案 4 :(得分:0)

感谢@The Shift Exchange,我是如何理解的:

if (Auth::attempt(array('email' => $email, 'password' => $password, 'role' => 1)))
{
    dd('Login Successful');
} else {
    if( Auth::validate( array( 'email' => $email, 'password' => $password ) ) ) 
    {
        // Wrong Role
        dd('Unauthorized user');
    } else {
        dd('Wrong email / password');
    }
}

这有效!!