Laravel:Auth::attemp - 为什么身份验证失败?
Laravel: Auth::attemp - Why Authentication failed?
我正在使用条件对用户进行身份验证,即如果
User role = 1 Login should be successful else login should fail
现在我正在做的是:
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');
}
它工作正常,但是当身份验证失败时,我希望能够找出失败的原因,因此我将能够根据身份验证失败的原因显示以下消息:
- 不存在的电子邮件地址
- 密码错误
- 用户角色不正确。
有办法做到这些吗?
先尝试验证,然后再尝试登录
Validating Multiple Fields
诀窍是在失败的部分使用 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
}
}
希望这对您有所帮助
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 的新手,所以我一直在使用较长的流程
这是用于验证目的的函数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 接口中,您会看到该接口已被定义,您可以自己实现它,并且 return 身份验证问题的原因而不是 false 或 true
感谢@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');
}
}
这成功了!!
我正在使用条件对用户进行身份验证,即如果
User role = 1 Login should be successful else login should fail
现在我正在做的是:
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');
}
它工作正常,但是当身份验证失败时,我希望能够找出失败的原因,因此我将能够根据身份验证失败的原因显示以下消息:
- 不存在的电子邮件地址
- 密码错误
- 用户角色不正确。
有办法做到这些吗?
先尝试验证,然后再尝试登录
Validating Multiple Fields
诀窍是在失败的部分使用 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
}
}
希望这对您有所帮助
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 的新手,所以我一直在使用较长的流程
这是用于验证目的的函数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 接口中,您会看到该接口已被定义,您可以自己实现它,并且 return 身份验证问题的原因而不是 false 或 true
感谢@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');
}
}
这成功了!!