我对中间件RedirectIfAuthenticated
有问题
当我在任何控制器中使用此中间件时,未检测到用户连接(用户连接为NULL)。即,我使用多重身份验证。
RedirectIfAuthenticated
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null){
//dd(Auth::guard($guard)->check()); // when the user is connected the result is null
//dd(Auth::user()); // when the user is connect the result is => Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in /Users/diegoruiz/Desktop/app/XXX/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 123
if($guard == 'admin' && Auth::guard($guard)->check()) {
return redirect('admin/accueil');
}else if($guard == 'employer' && Auth::guard($guard)->check()){
return redirect('/accueil');
} else {
return $next($request);
}
}
}
LoginController
class LoginController extends Controller{
use AuthenticatesUsers;
...
public function __construct(){
$this->middleware('guest')->except('logout');//It's not working.
//$this->middleware('guest:admin')->except('logout');//It's not working.
}
...
}
config / auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'admin-api' =>[
'driver' => 'token',
'provider' => 'admins',
],
'employer'=>[
'driver' => 'session',
'provider' => 'employers',
],
'employer-api'=>[
'driver' => 'token',
'provider' => 'employers',
],
],
'providers' => [
'admins'=> [
'driver' => 'UtilisateurUserProvider',
'model' => App\Admin::class,
],
'employers'=>[
'driver' => 'UtilisateurUserProvider',
'model'=> App\Employer::class
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admin' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'employer' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
UtilisateurUserProvider
<?php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider as UserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;
class UtilisateurUserProvider extends UserProvider{
public function boot()
{
//
}
public function register(){
//
}
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('mdp', $credentials))) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (Str::contains($key, 'uti_mdp')) {
continue;
}
if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
return $query->first();
}
public function validateCredentials(UserContract $user, array $credentials){
$plain = $credentials['mdp'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
}
内核
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,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\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,
'entrepriseDatabase' => \App\Http\Middleware\SetEntrepriseDatabase::class,
];
}
当我在视图中使用Auth::user()
时遇到此错误
答案 0 :(得分:0)
如果您的任务是澄清用户并将他们重定向到其他路由,那么我将提供不使用中间件的另一种方式。enter image description here
答案 1 :(得分:0)
问题出在 config / auth.php 中,当您调用Auth::user()
时,它将在auth.php中看到用户Auth,但是根据您的auth.php,它丢失了: / p>
'providers' => [
'admins'=> [
'driver' => 'UtilisateurUserProvider',
'model' => App\Admin::class,
],
'employers'=>[
'driver' => 'UtilisateurUserProvider',
'model'=> App\Employer::class
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
您必须提供用户,并在用户表中添加另一个包含雇员,管理员等的列角色。您的表关系不正确。