2016-12-14 134 views
0

我正在处理客户数据库的旧副本,并使新的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经验的人照亮我做错了什么?提前致谢!

+0

编辑清除'auth_user'或'auth_users'的不明确性 – QNeville

回答

0

这不是一个完整的解决方案。我仍然找不到任何可以告诉Laravel/Guard不要看ID栏的内容,所以我通过添加ID栏和$user->id = $user->uid;来帮助它。我不为此感到自豪,但它的工作原理。

0

在你的User.php脚本中,你需要放置这段代码,这段代码让这张表进行登录检查。

protected $table='auth_users'; 

在你的User.php代码中,在初始阶段(类函数之后)保护表代码。

+0

感谢Sankar,但遗憾的是它已经在我的'User.php'脚本中。 – QNeville

相关问题