[英]Laravel 5.1 extra field for authentication
我正在使用Laravel 5.1
创建我的第一个大项目,我想在用户登录期间添加额外的检查以查看用户是否已激活他们的帐户。
这是users
表的模式
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->boolean('is_admin')->default(true);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
我已经尝试添加$credentials['is_active'] = true;
在$credentials = $this->getCredentials($request);
在AutheticatesUser@postLogin
,它可以工作,但是如果用户的帐户不活动,我想要有一个自定义错误,因为默认帐户( These credentials do not match our records.
)对用户来说并不直观。
实现这一目标的任何建议? 谢谢!
您可以覆盖AuthController中的postLogin方法,并检查用户是否处于活动状态。
class AuthController extends Controller
{
public function postLogin(Request $request){
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $this->getCredentials($request);
// This section is the only change
if (Auth::validate($credentials)) {
$user = Auth::getLastAttempted();
if ($user->is_active) {
Auth::login($user, $request->has('remember'));
return redirect()->intended($this->redirectPath());
} else {
return redirect($this->loginPath()) // Change this to redirect elsewhee
->withInput($request->only('email', 'remember'))
->withErrors([
'active' => 'Please active your account'
]);
}
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
}
}
你可以检查以下方式
if(Auth::attempt(['email'=>$email,'password'=>$password,'is_admin'=>1]))
{
return redirect()->intended('admin/dashboard');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.