2

我正在使用条件验证用户,即如果

用户角色 = 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');
}

它工作正常,但是当身份验证失败时,我希望能够找出失败的原因,以便能够根据身份验证失败的原因显示以下消息:

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

有没有办法做这些事情?

4

5 回答 5

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
    } 
}
于 2015-02-23T09:45:08.127 回答
1

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

  Validating Multiple Fields 

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

于 2015-02-23T09:42:25.047 回答
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');
    }
}

这有效!

于 2015-02-23T10:06:35.733 回答
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 的新手,所以我一直在使用更长的过程

于 2015-02-23T09:51:06.250 回答
0

这是 laravel 用于身份验证的函数。如果你在这里看到: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 接口中,您会看到该接口已被定义,您可以自己实现它并返回身份验证问题的原因,而不是 false 或 true

于 2015-02-23T10:03:18.353 回答