登录laravel后页面刷新时419页面过期错误
Posted
技术标签:
【中文标题】登录laravel后页面刷新时419页面过期错误【英文标题】:419 page expired error when page refreshed after login laravel 【发布时间】:2020-04-15 22:03:39 【问题描述】:我有多个角色,登录时工作正常。超级管理员被定向到管理仪表板,普通用户被定向到普通仪表板。但是如果页面被刷新,登录后会显示 419 页面过期错误,路由将再次登录。 登录控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
use App\User;
use App\Roles_Users\Roles_Users;
use App\Roles\Roles;
class LoginController extends Controller
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/';
public function authenticated($request)
$user = Auth::user();
// echo'<pre>';
// print_r($user);
// exit;
$userrole = Roles_Users::where('userid',$user->id)->first();
if(empty($userrole))
return redirect('/welcome');
else
$role = Roles::where('id',$userrole->roleid)->first();
if($role->rolename === 'SuperAdmin')
$userdata = User::all();
$data = Roles::all();
return view('/superadmin.superdash',['userdata'=>$userdata,'data'=>$data]);
elseif($role->rolename === 'Admin')
return view('/admin.admindash');
else
return view('/home');
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('guest')->except('logout');
路线:
Route::get('/', function ()
return view('welcome');
);
Auth::routes();
//Route::get('/home', 'HomeController@index')->name('home');
Route::group(['middleware' => 'WhoIsUser'], function()
Route::resource('roles','Role\RolesController');
Route::resource('/','Users\UsersController');
Route::resource('posts','Posts\PostsController');
Route::resource('userroles','UserRole\UserRolesController');
);
不知道怎么回事。可能是我的 RedirectIfAuthenticated:
public function handle($request, Closure $next, $guard = null)
if (Auth::guard($guard)->check())
return redirect('/');
return $next($request);
【问题讨论】:
【参考方案1】:问题是使用登录控制器的身份验证。我要解决的是创建一个中间件并将用户角色发送到控制器,然后才根据角色重定向。 中间件:
public function handle($request, Closure $next)
$user = Auth::user();
$userrole = Roles_Users::where('userid',$user->id)->first();
if(!empty($userrole))
$role = Roles::where('id',$userrole->roleid)->first();
if ($role->rolename === 'SuperAdmin')
$request->request->add(['roles' => 'superadmin']); //add request
elseif ($role->rolename === 'Admin')
$request->request->add(['roles' => 'admin']); //add request
else
$request->request->add(['roles' => 'normaluser']); //add request
return $next($request);
登录控制器:
protected $redirectTo ='/dashboard';
路线:
Route::get('dashboard', [
'middleware' => 'assignRole',
'uses' => 'AdminController@index'
]);
管理控制器:
public function index(Request $request)
if($request['roles'] === 'superadmin')
$userdata = User::all();
$data = Roles::all();
return view('superadmin.superdash',['userdata'=>$userdata,'data'=>$data]);
elseif($request['roles'] === 'admin')
return view('admin.admindash');
else
return view('/home');
【讨论】:
以上是关于登录laravel后页面刷新时419页面过期错误的主要内容,如果未能解决你的问题,请参考以下文章