laravel 5.2多认证无法正常工作

时间:2016-01-22 02:26:12

标签: php laravel authentication laravel-5.2

我正在尝试使用laravel 5.2 multi auth但不能正常工作。我创造了2个守卫, adminmhs。但我无法使用mhs后卫登录。当我尝试使用mhs guard登录时,它总是重定向到该登录表单。我尝试了这个How to use multi Auth in laravel 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', 'LoginController@admingetLogin');
Route::post('admin/login', 'LoginController@adminLogin');
Route::get('/logout', 'LoginController@getLogout');

Route::get('mhs/login', 'LoginController@mhsgetLogin');
Route::post('mhs/login', 'LoginController@mhsLogin');
Route::get('/logout', 'LoginController@getLogout');

});

Route::group(['middleware' => ['auth']], function () {
Route::get('/home', 'MahasiswaController@index');
Route::get('/mhs', 'MahasiswaController@mhs');
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天内坚持这一点,任何人都可以帮助我吗? 如果有人能够解决问题,那将非常有帮助。

1 个答案:

答案 0 :(得分:0)

你的警卫和提供者看起来很好。 首先,检查一个警卫中的auth。也许你对attempt有疑问。 使用Auth::attempt作为默认防护。您可以在

中查看结果
 public function validateCredentials(UserContract $user, array $credentials)

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

检查密码可能有问题