У меня есть два отдельных входа в систему с использованием охранников, которые покупатель и администратор. У меня есть промежуточное программное обеспечение checkRole для входа в систему adminuser, и оно отлично работает. Я использовал гостевое промежуточное ПО (RedirectifAuthenticated) для входа в систему. Когда логин клиента проверяется с помощью Auth::guard($guard)->check()
, он всегда возвращает false
. Но Auth::guard('customer')->attempt
возвращает true
. Я не могу найти решение.
вот контроллер входа для клиента
class LoginController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest:customer')->except('customerlogout');
}
//login the customer
public function customerLogin(Request $request)
{
$email = $request->get('email');
$password = $request->get('password');
//Getting user inputs and authenticate the customer
$status = Auth::guard('customer')->attempt([
'email' => $email,
'password' => $password ]
);
//Check login
if ($status) {
//if login pass,redirect to customer account page
return redirect()->intended('/customer_account');
}else{
//if login fail,redirect back to home page
return redirect()->back();
}
}
//logout the customer
public function customerlogout(Request $request)
{
Auth::guard('customer')->logout();
$request->session()->flush();
return redirect('/index');
}
}
вот гостевое промежуточное ПО
public function handle($request, Closure $next, $guard = null)
{
if ($guard === "customer" && Auth::guard($guard)->check()) {
return redirect('/customer_account');
}
if (!Auth::guard($guard)->check()) {
return redirect('/index');
}
return $next($request);
}
Керенель. php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\RealRashid\SweetAlert\ToSweetAlert::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'roles' => \App\Http\Middleware\CheckRole::class,
];
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\Authenticate::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
}
Любая помощь приветствуется, спасибо!