2017-04-24 9 views
1

The error when i login as Adminエラーlaravel 5.3

を使用して2つのテーブルに対して複数の認証に私は2つのテーブル1を持って、通常のユーザーであり、他の支店です。私は通常のユーザーとしてログインしていて、エラーなしでログインしています。第二に、ブランチのメールとパスワードで管理者にログインし、分岐テーブルに保存しますが、ログインするとエラーが表示されます。

BranchController:

public function ggetlogin() 
{ 
    return view('branch_admin.badmin'); 
} 

public function authenticateBranchAdmin(Request $request){ 
    $validator = Validator::make($request->all(), [ 
     'email' => 'required|email', 
     'password' => 'required' 
    ]); 

    if($validator->passes()){ 
     if(Auth::guard('admin')->attempt([ 
      'email' => $request->email, 
      'password' => $request->password, 
     ])){ 
      if(Auth::guard('admin')->branch()->email === $request->email){ 
       return redirect('/branch/'.Branch::branch()->id); 
      } 

     }else{ 
      if($this->AdminIsVerified($request->email)){ 
       $request->session()->flash('message', 'Invalid email or password'); 
      } 
      return redirect('/Admin/login'); 
     } 
    }else{ 
     return redirect('/Admin/login')->withErrors($validator)->withInput(); 
    } 
} 

AddBranchView:

<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6"> 
<form class="form-horizontal branch-form" action="/add/branch" method="post"> 
     <div class="form-group"> 
     <label class="col-sm-3 control-label">BranchName</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="name" placeholder="Name"> 
      @if($errors->has('name')) 
       {{$errors->first('name')}} 
      @endif 
     </div> 
    </div> 
    <div class="form-group"> 
     <label class="col-sm-3 control-label">B_Email</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="email" placeholder="Email"> 
      @if($errors->has('email')) 
       {{$errors->first('email')}} 
      @endif 
     </div> 
    </div> 
    <div class="form-group"> 
     <label class="col-sm-3 control-label">Password</label> 
     <div class="col-sm-9"> 
      <input type="text" class="form-control" name="password" placeholder="password"> 
      @if($errors->has('password')) 
       {{$errors->first('password')}} 
      @endif 
     </div> 
    </div> 

     @if(Session::has('message')) 
      <div class="form-group"> 
       <label class="col-sm-3 control-label"></label> 
       <div class="col-sm-9"> 
        <div class="alert alert-info" role="alert">{{Session::get('message')}}</div> 
       </div> 
      </div> 
     @endif 

    <div class="form-group"> 
     <label class="col-sm-3 control-label"></label> 
     <div class="col-sm-9"> 
      <button type="submit" value="add branch" class="btn btn-default">Submit</button> 
     </div> 
    </div> 
    {{csrf_field()}} 
</form> 
</div> 

Auth.php

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 

    'api' => [ 
     'driver' => 'token', 
     'provider' => 'users', 
    ], 

    'admin'=> [ 
     'driver' => 'session', 
     'provider' => 'branch', 
    ], 
], 

プロバイダ:

'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 

    'admin' => [ 
     'driver' => 'eloquent', 
     'model' => App\Branch::class, 
    ], 
], 

LoginView:

<form class="form-horizontal login-form" action="/Admin/login" method="post"> 

    <div class="form-group" > 
     <label class="col-sm-3 control-label">EMAIL</label> 
     <div class="col-sm-9"> 
      <input type="email" class="form-control" name="email" placeholder="Email"> 
      @if($errors->has('email')) 
       {{$errors->first('email')}} 
      @endif 
     </div> 
    </div> 

    <div class="form-group"> 
     <label class="col-sm-3 control-label">PASSWORD</label> 
     <div class="col-sm-9"> 
      <input type="password" class="form-control" name="password" placeholder="Password"> 
      @if($errors->has('password')) 
       {{$errors->first('password')}} 
      @endif 
     </div> 
    </div> 

    <div class="form-group"> 
     <label class="col-sm-3 control-label"></label> 
     <div class="col-sm-9"> 
      <button type="submit" value="login" class="btn btn-default">Login</button> 
     </div> 
    </div> 

    {{csrf_field()}} 
</form> 
+0

auth.phpに 'providers'配列を追加できますか? – hctopcu

+0

はい私はプロバイダーの配列を追加します – Haider

+0

私は見ることができるように、あなたのポストにそれを追加してください。 – hctopcu

答えて

0

guards.adminprovider呼ばブランチ を探しているあなたは、あなたのプロバイダの配列にbranchを持っている必要があります。

だからあなたauth.phpは、次のようになります。あなたの分岐テーブルはidpasswordremember_token、などの同じデシベルcolumsを持っている場合はまた、あなたの支店のモデルが\認証\認証可能 を照らす\契約を実装する必要があります

'guards' => [ 
    'web' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 

    'api' => [ 
     'driver' => 'token', 
     'provider' => 'users', 
    ], 

    'admin'=> [ 
     'driver' => 'session', 
     'provider' => 'branch', // This one says there should be a branch in providers 
    ], 
], 
'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 

    'branch' => [ // This is where guards.admin is looking 
     'driver' => 'eloquent', 
     'model' => App\Branch::class, 
    ], 
], 

。最も簡単な方法はUserモデルと同じです。あなたのブランチオブジェクトを取得するためにuser()を使用する必要が

Auth::guard('admin')->user()->email 

pleseはノート:

namespace App; 
//... 
use Illuminate\Foundation\Auth\User as Authenticatable; 
//... 

class Branch extends Authenticatable 
{ 
    //... 
} 

次に、あなたのようにあなたのブランチを取得することができます。 not branch()


コントローラメソッドでコードの代わりにミドルウェアを使用することをお勧めします。より管理しやすく、はるかに簡単です。その後

routes/web.php

Route::get('/some-page-for-normal-users', "[email protected]")->middleware('auth:web'); 

Route::get('/some-page-for-branch-users', "[email protected]")->middleware('auth:admin'); 

どこでもあなたのビュー内などあなたはユーザー認証できるようになります() - >ユーザー()あなたのミドルウェアに設定されている方、あなたの支店またはユーザーを取得します。