2016-01-22 86 views
1

我想使用laravel 5.2多個身份驗證但不工作。我創造了2名後衛, adminmhs。但我不能用mhs後衛登錄。當我試圖用mhs警衛登錄時,它總是重定向到登錄表單。我試過下面這How to use multi Auth in laravel 5.2laravel 5.2多個身份驗證不工作

這是我auth.php

衛隊

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

    'mhs' => [ 
    'driver' => 'session', 
    'provider' => 'mhs', 
], 

提供商

'providers' => [ 

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

'mhs' => [ 
    'driver' => 'eloquent', 
    'model' => App\Mahasiswa::class, 
], 
], 

這是我routes.php

Route::group(['middleware' => ['web']], function() {  
Route::get('admin/login', '[email protected]'); 
Route::post('admin/login', '[email protected]'); 
Route::get('/logout', '[email protected]'); 

Route::get('mhs/login', '[email protected]'); 
Route::post('mhs/login', '[email protected]'); 
Route::get('/logout', '[email protected]'); 

}); 

Route::group(['middleware' => ['auth']], function() { 
Route::get('/home', '[email protected]'); 
Route::get('/mhs', '[email protected]'); 
Route::resource('mahasiswa', 'MahasiswaController'); 
}); 

,這是我LoginController

<?php 
namespace App\Http\Controllers; 

use Auth; 
use App\User; 
use App\Mahasiswa; 
use Illuminate\Support\Facades\Input; 

class LoginController extends Controller 
{ 


protected $username = 'username'; 

public function admingetLogin() 
{ 
    return view('auth.login'); 

} 
public function mhsgetLogin() 
{ 
    return view('mahasiswa.login'); 

} 
public function adminLogin(){ 
    $input = Input::all(); 
    if(count($input) > 0){ 
     $auth = auth()->guard('admin'); 

     $credentials = [ 
      'username' => $input['username'], 
      'password' => $input['password'], 
     ]; 

     if ($auth->attempt($credentials)) { 
      if(auth()->guard('admin')->check()){ 
     auth()->guard('admin')->user()->toArray(); 
     return redirect()->intended('/'); 
    } 
     } else { 
      echo 'Error'; 
     } 
    } else { 
     return view('auth.login'); 
    } 
} 


public function mhsLogin(){ 
    $input = Input::all(); 
    if(count($input) > 0){ 
     $auth = auth()->guard('mhs'); 

     $credentials = [ 
      'username' => $input['username'], 
      'password' => $input['password'], 
     ]; 

     if ($auth->attempt($credentials)) { 
      if(auth()->guard('mhs')->check()){ 
     auth()->guard('mhs')->user()->toArray(); 
     return redirect()->intended('/mhs'); 
     }     
     } else { 
      echo 'Error'; 
     } 
    } else { 
     return view('mhs.login'); 
    } 
} 

public function profile(){ 
    if(auth()->guard('admin')->check()){ 
     pr(auth()->guard('admin')->user()->toArray()); 
     return redirect()->intended('/'); 
    }   
    if(auth()->guard('mhs')->check()){ 
     pr(auth()->guard('mhs')->user()->toArray()); 
     return redirect()->intended('/'); 
    } 
} 
public function getLogout() 
{ 
    Auth::logout(); 

    return redirect()->intended('/'); 
} 
} 

我卡到這4天,誰能幫助我? 如果有人能夠解決問題,這將是非常有益的。

回答

0

你的衛兵和提供者看起來不錯。 首先,檢查你的auth在一名警衛。也許你有問題attempt。 默認警衛使用Auth::attempt。您可以檢查這個結果

public function validateCredentials(UserContract $user, array $credentials) 

這是vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php

也許是檢查密碼

+0

我有什麼,當我打開'供應商/ laravel /框架/ src目錄/照亮辦/問題Auth/EloquentUserProvider.php'?對不起,我還是不明白 – AhmadHR

+0

轉儲嘗試功能。它的返回是真是假? – Olbrych