Laravel 5.1 用于身份验证的额外字段
Laravel 5.1 extra field for authentication
我正在使用 Laravel 5.1
制作我的第一个大项目,我想在用户登录期间添加额外的检查,以查看用户是否已激活他们的帐户。
这是 users
table
的架构
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();
});
我尝试在 AutheticatesUser@postLogin
中的 $credentials = $this->getCredentials($request);
之后添加一个 $credentials['is_active'] = true;
并且它有效,但如果用户的帐户未激活,我想有一个自定义错误,因为默认one(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');
}
我正在使用 Laravel 5.1
制作我的第一个大项目,我想在用户登录期间添加额外的检查,以查看用户是否已激活他们的帐户。
这是 users
table
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();
});
我尝试在 AutheticatesUser@postLogin
中的 $credentials = $this->getCredentials($request);
之后添加一个 $credentials['is_active'] = true;
并且它有效,但如果用户的帐户未激活,我想有一个自定义错误,因为默认one(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');
}