我正在處理客戶數據庫的舊副本,並使新的Laravel應用程序與其現有用戶一起工作。用戶通過Auth :: attempt();失敗Auth :: check('user')
我正在使用'users
'表使用我的用戶模型進行構建和測試,但我試圖將其與'auth_user
'表連接起來。更改後,我的新用戶正在正確創建。登錄是一個問題,但。用戶被路過Auth::attempt($credentials) as expected
,但失敗時
在我的LoginController ...
// post to /login
public function login() {
$input = Request::all();
// Log the user in
if (Auth::attempt(['email'=>$input['username'], 'password'=>$input['password']])) {//Auth::attempt(Auth::attempt("admin", ['email' => $input['username'], 'password' => $input['password'], 'active' => 1])) {
// the user is now authenticated.
return Redirect::to('/welcome')->with('message', 'Successfully authenticated');
}
return Redirect::to('/')
->with('message', 'Login Failed: Your Login Credentials Are Invalid');
}
}
我肯定經過Auth::attempt(...)
,但我不認爲我會被設置爲該用戶。重定向到/歡迎之後,我失敗Auth::check('user')
public function welcome() {
if (!Auth::check('user')) return Redirect::to('/');
// ... Top secret stuff (no returns or redirects though)
return view('user.welcome', [
'user' => Auth::user()
// ...
]);
}
這重定向回我的登錄控制器。
踢球者這是所有的工作,當我用我的'users'
表而不是'auth_user'
。
Users
使用id
作爲主鍵,Auth_user
使用'uid'
作爲主鍵。我很想將uid
更改爲id
,但我必須重複使用可變數量的MYSQL存儲過程,這是我無法更改的。
相關機型:
user.php的:
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
public function rules($scenario = null, $id = null) {
$rules = [];
switch($scenario) {
case 'userAdd':
$rules = [
'password' => 'required|confirmed|min:6'
];
break;
case 'passwordChange':
$rules = [
'password' => 'required|confirmed|min:6'
];
break;
}
return $rules;
}
public function isValid($scenario = null) {
$validation = User::make($this->attributes, User::rules($scenario));
if($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'auth_user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['username', 'name', 'password', 'email', 'expire', 'active', 'organization','role'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
protected $primaryKey = 'uid';
}
Auth.php(用於多用戶類型 - 我知道,我寧願使用角色而不是獨立的模型太)
<?php
return [
'multi' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
'table' => 'auth_user'
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
'table' => 'auth_admin'
]
],
];
我想我涵蓋了所有的主鍵更改的基礎,但我無法讓模型通過Auth::check()
。可以有更多Laravel經驗的人照亮我做錯了什麼?提前致謝!
編輯清除'auth_user'或'auth_users'的不明確性 – QNeville