Multi-Auth Auth :: user () Показывает ноль Laravel 5.8 - PullRequest
0 голосов
/ 18 мая 2019

Я использую множественную аутентификацию, поскольку у меня есть 3 разные группы пользователей.Я пытаюсь следующие шаги.

LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

use Auth;

class LoginController extends Controller
{
    use AuthenticatesUsers;


    protected $redirectTo = '/home';


    public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->middleware('guest:admin')->except('logout');
        $this->middleware('guest:manager')->except('logout');
        $this->middleware('guest:vendor')->except('logout');
    }

    public function showAdminLoginForm()
    {
        return view('auth.login', ['url' => 'admin']);
    }

    public function adminLogin(Request $request)
    {
        $this->validate($request, [
            'email'   => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {

            return redirect()->intended('/admin');
        }
        return back()->withInput($request->only('email', 'remember'));
    }

    public function showManagerLoginForm()
    {
        return view('auth.login', ['url' => 'manager']);
    }

    public function managerLogin(Request $request)
    {
        $this->validate($request, [
            'email'   => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('manager')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {

            return redirect()->intended('/manager');
        }
        return back()->withInput($request->only('email', 'remember'));
    }

    public function showVendorLoginForm()
    {
        return view('auth.login', ['url' => 'vendor']);
    }

    public function vendorLogin(Request $request)
    {
        $this->validate($request, [
            'email'   => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('vendor')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {

            return redirect()->intended('/vendor');
        }
        return back()->withInput($request->only('email', 'remember'));
    }
}

AdminController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest:admin');   
    }

    public function dashboard()
    {
        dd(auth()->user());
        return view('admin');
    }
}

ManagerController и VendorController совпадает с AdminController .

Web.php

Route::view('/', 'welcome');
Auth::routes();

Route::prefix('admin')->group(function () {
    Route::get('/login', 'Auth\LoginController@showAdminLoginForm');
    Route::post('/login', 'Auth\LoginController@adminLogin');
    Route::get('/register', 'Auth\RegisterController@showAdminRegisterForm');
    Route::post('/register', 'Auth\RegisterController@createAdmin');
});

Route::prefix('manager')->group(function () {
    Route::get('/login', 'Auth\LoginController@showManagerLoginForm');
    Route::post('/login', 'Auth\LoginController@managerLogin');
    Route::get('/register', 'Auth\RegisterController@showManagerRegisterForm');
    Route::post('/register', 'Auth\RegisterController@createManager');
});

Route::prefix('vendor')->group(function () {
    Route::get('/login', 'Auth\LoginController@showVendorLoginForm');
    Route::post('/login', 'Auth\LoginController@vendorLogin');
    Route::get('/register', 'Auth\RegisterController@showVendorRegisterForm');
    Route::post('/register', 'Auth\RegisterController@createVendor');
});


Route::view('/home', 'home')->middleware('auth');

Route::get('/admin', 'AdminController@dashboard');
Route::get('/manager', 'ManagerController@dashboard');
Route::get('/vendor', 'VendorController@dashboard');

RedirectIfAuthenticated.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        if ($guard == "admin" && Auth::guard($guard)->check()) {
                return redirect('/admin');
        }
        if ($guard == "manager" && Auth::guard($guard)->check()) {
            return redirect('/manager');
        }
        if ($guard == "vendor" && Auth::guard($guard)->check()) {
            return redirect('/vendor');
        }
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }
}

config.auth.php

<?php

return [

    'defaults' => [
        'guard' => 'admin',
        'passwords' => 'admins',
    ],


    'guards' => [

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

        'manager' => [
            'driver' => 'session',
            'provider' => 'managers',
        ],

        'vendor' => [
            'driver' => 'session',
            'provider' => 'vendors',
        ],

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

    'providers' => [

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        'managers' => [
            'driver' => 'eloquent',
            'model' => App\Manager::class,
        ],


        'vendors' => [
            'driver' => 'eloquent',
            'model' => App\Vendor::class,
        ],
    ],

    'passwords' => [
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'managers' => [
            'provider' => 'managers',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'vendors' => [
            'provider' => 'vendors',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

Когда я пытаюсь получить Auth :: user (), выполнив dd () в AdminController, он показывает мне ноль из-за множественной аутентификации.Хотя он вошел в систему успешно.Я предполагаю, что я не использую надлежащую охрану для администратора.Но в то же время я понятия не имею, как это исправить.Пожалуйста, помогите мне.

...