这是我之前关于表单请求的问题的后续,现在已经解决了。不幸的是,应用程序无法使用正确的凭据登录。我以前遇到过这个问题,解决方案与会话有关-然而,这里的情况不是这样,因为会话被正确设置,以及一个密钥。
当提供正确的凭据时,$auth->attempt()
返回false。
模式:
Schema::create('users', function (Blueprint $t) {
$t->increments('id')->unsigned();
$t->timestamps();
$t->rememberToken();
$t->boolean('system')->default(false);
$t->boolean('activated')->default(true);
$t->string('username')->unique();
$t->string('email')->unique()->nullable();
$t->string('passphrase', 64);
$t->string('first_name', 20)->nullable();
$t->string('last_name', 20)->nullable();
$t->text('meta')->nullable();
});
种子:$adminUser = App'User::create([
'system' => true,
'username' => 'SysAdmin',
'passphrase' => bcrypt('the-password'),
]);
$adminUser->attachRole($administratorRole);
用户模式:namespace App;
use Bican'Roles'Contracts'HasRoleAndPermission as HasRoleAndPermissionContract;
use Bican'Roles'Traits'HasRoleAndPermission;
use Illuminate'Auth'Authenticatable;
use Illuminate'Auth'Passwords'CanResetPassword;
use Illuminate'Contracts'Auth'Authenticatable as AuthenticatableContract;
use Illuminate'Contracts'Auth'CanResetPassword as CanResetPasswordContract;
use Illuminate'Database'Eloquent'Model;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract
{
use Authenticatable, CanResetPassword, HasRoleAndPermission;
protected $table = 'users';
protected $fillable = ['system', 'username', 'email', 'passphrase', 'first_name', 'last_name'];
protected $hidden = ['passphrase', 'remember_token'];
}
AuthController: attemptSignIn()方法:
用户可以使用用户名或电子邮件地址登录。
public function attemptSignIn(SignInRequest $request = null, $type = 'regular')
{
switch ($type) {
case 'regular':
$identifierFieldName = 'account_identifier';
$field = filter_var($this->request->input($identifierFieldName), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
$this->request->merge([$field => $this->request->input($identifierFieldName)]);
$specifics = $this->request->only($field, 'passphrase');
if ($this->auth->attempt($specifics)) {
return redirect($this->redirectPath);
} else {
return redirect($this->signInPath)
->with('authError', "The credentials you've provided are incorrect.")
->with('authErrorType', 'danger')
->withInput($this->request->only($identifierFieldName));
}
break;
case 'oota':
break;
}
}
$specifics
中设置的信息正确,且与数据库中的记录匹配。
也许我错过了一些简单的?
如前所述,password
硬编码到Laravel的认证协议中。因此,我只需要更改发送给attempt
的数组:
$specifics['password'] = $specifics['passphrase'];
unset($specifics['passphrase']);