2016-12-14 17 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() 
           // ... 
           ]); 
} 

に失敗します。

キッカーは、'auth_user'の代わりに'users'テーブルを使用していたときにすべて動作していました。

Usersは、プライマリキーとしてidを使用します。Auth_userは、プライマリキーとして'uid'を使用します。 uididに変更したいのですが、変更できない無数の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スクリプトでは、このコードを配置する必要があり、このコードはログインチェックのためにテーブルを作成します。 (クラス関数の後に)初期段階でテーブルコードを保護し、あなたのUser.phpコード代わりに

protected $table='auth_users'; 

+0

ありがとうございました。しかし、悲しいことに、それは既に私の 'User.php'スクリプトにあります。 – QNeville

関連する問題