Laravel:其他登录条件不起作用 [英] Laravel: Additional Login Conditions not working
问题描述
我正在做Laravel身份验证.我不知道弄清楚我正在使用Auth::routes
是否重要.
我在用户模型中添加了active
字段.但是即使不活跃,我也可以登录.
I'm doing a Laravel authentication. I don't know if it is important to clarify that I'm using the Auth::routes
.
I added an active
field to my users model. But even when is not active, I'm able to log in.
这是我的用户模型:
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Laravel\Passport\HasApiTokens;
use Laratrust\Traits\LaratrustUserTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;
class User extends Authenticatable
{
use Notifiable;
use LaratrustUserTrait;
use HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'active'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'active' => 'boolean',
'email_verified_at' => 'datetime',
];
}
然后,作为文档在指定其他条件"下指定部分,我在LoginController.php
内部添加了带有try参数的额外参数的自定义验证.
Then, as the documentation specifies under the "Specifying Additional Conditions" section, I added inside my LoginController.php
the custom validation with the extra parameter to the attempt function.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Handle an authentication attempt.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function authenticate(Request $request)
{
$email = $request->input('email');
$password = $request->input('password');
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
return redirect()->intended($redirectTo);
}
}
}
我在做什么错了?
推荐答案
弄清Laravel文档代码的放置位置很重要.
为了使其正常工作,您需要在文件LoginController.php
的顶部导入use Illuminate\Http\Request;
.
It is important to clarify where to put the code of the Laravel documentation.
In order to make it work, you need to import use Illuminate\Http\Request;
at the top of your LoginController.php
file.
然后,您必须覆盖凭据功能.
Then, you have to override the credentials function.
/**
* @param \Illuminate\Http\Request $request
*/
protected function credentials(Request $request) {
$email = $request->input('email');
$password = $request->input('password');
return ['email' => $email, 'password' => $password, 'active' => 1];
}
这样做之后,获得了预期的结果.
After doing this, the expected results were obtained.
这篇关于Laravel:其他登录条件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!