我最近将我的身份验证更改为Sentinel,除了以下问题之外,一切似乎都有效:当用户未经过身份验证时,我想将其重定向到登录页面,但我总是收到错误&#34 ;试图获得非对象的属性"
我评论了 Kernel.php 中的几个标准中间件:
protected $middlewareGroups = [
'web' => [
// \Illuminate\Session\Middleware\AuthenticateSession::class,
protected $routeMiddleware = [
// 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
// 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
// 'can' => \Illuminate\Auth\Middleware\Authorize::class,
// 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
我自己添加了一个新的中间件:
<?php
namespace App\Http\Middleware;
use Closure;
use Sentinel;
class AdminMiddleware
{
public function handle($request, Closure $next)
{
if (Sentinel::check())
{
if (Sentinel::getUser()->roles()->first()->slug == 'admin' ||
Sentinel::getUser()->roles()->first()->slug == 'superuser' )
{
return $next($request);
}
}
else
{
return redirect('/login')
->with(['error' => "You do not have the permission to enter this site. Please login with correct user."]);
}
}
}
如果用户已登录并返回请求,则一切正常。如果用户未登录或级别太低,则执行重定向时会触发错误:
&#34;试图获得非对象的属性&#34;
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php $响应 - &GT;包头中&GT; setCookie方法(
答案 0 :(得分:3)
不要在那里使用其他人!否则,Sentinel::check()
为真但用户没有正确的slu it的情况根本不被任何返回值覆盖!
public function handle($request, Closure $next)
{
if (Sentinel::check())
{
if (Sentinel::getUser()->roles()->first()->slug == 'admin' ||
Sentinel::getUser()->roles()->first()->slug == 'superuser' )
{
return $next($request);
}
}
return redirect('/login')
->with(['error' => "You do not have the permission to enter this site. Please login with correct user."]);
}
}