[英]Laravel 5.8 redirected you too many times, middleware problem
I have a signup route. 我有一个注册路线。 After it register on step1, it emails to the user to verify his account and have a link on his email.
在第1步注册后,它会通过电子邮件发送给用户以验证其帐户并在其电子邮件中有链接。 After clicking the link, it should redirect to
signup/step2
, and finished and he can access the job-seeker/home
. 点击链接后,它应该重定向到
signup/step2
,并完成,他可以访问job-seeker/home
。
so the logic is after finished the registration, user cannot visit again to signup/step2
cause user already finished fill up the form. 所以逻辑是在完成注册后,用户无法再次访问
signup/step2
导致用户已经完成填写表单。 and before fillup signup/step2
, he can't access also the job-seeker/home
. 在填写
signup/step2
之前,他也无法访问job-seeker/home
。 So it's vice versa. 所以反之亦然。
basically my middleware was first: check if the user completed the step2 and added true on column is_completed
in database. 基本上我的中间件是第一个:检查用户是否完成了step2并在数据库中的
is_completed
列中添加了true。 then on the second middleware is to visit only his route by his role, he can't access other routes from other role and redirect to his home based on his role. 然后在第二个中间件是通过他的角色只访问他的路线,他不能从其他角色访问其他路线并根据他的角色重定向到他的家。
But it throws me too many redirect and switching both side even I still didn't fill up the step2 yet. 但是它引发了我太多的重定向和切换双方,即使我还没有填满step2。 this is my gif below.
这是我的GIF下面。
MyCode mycode的
Kernel.php Kernel.php
class Kernel extends HttpKernel
{
...
protected $routeMiddleware = [
...
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'isCompleted' => \App\Http\Middleware\IsCompleted::class,
];
Middleware/IsCompleted.php 中间件/ IsCompleted.php
class IsCompleted
{
public function handle($request, Closure $next)
{
if(auth()->user()->isCompleted == 1){
return $next($request);
}
// if 0, redirect to step2
return redirect()->route('register.step2');
}
Middleware/RedirectIfAuthenticated.php 中间件/ RedirectIfAuthenticated.php
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
if ( Auth::user()->hasRole('job-seeker') ) {
return redirect()->route('job-seeker.home');
} else if(Auth::user()->hasRole('admin')) {
return redirect()->route('admin.home');
}
}
return $next($request);
Routes/Web.php 路线/ Web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::group(['middleware' => ['verified', 'isCompleted']], function() {
Route::group(['prefix' => 'admin', 'name' => 'admin.'], function() {
Route::get('/home', function(){ return "test"; })->name('admin.home');
});
Route::group(['prefix' => 'job-seeker', 'name' => 'job-seeker.'], function() {
Route::get('/home', 'Jobseeker\HomeController@index')->name('job-seeker.home');
});
});
Auth::routes(['verify' => true, 'register' => false]);
Route::get('signup/{usertype}' , 'Auth\RegisterController@getStep1')->name('register.step1');
Route::post('signup/{usertype}' , 'Auth\RegisterController@postStep1');
Route::group(['middleware' => ['auth']], function() {
Route::get('signup/step2' , 'Auth\RegisterController@getStep2')->name('register.step2');
Route::post('signup/step2' , 'Auth\RegisterController@postStep2');
});
EDIT 1 编辑1
I inspect the page and go to network tab, and this is the result. 我检查页面并转到网络选项卡,这就是结果。
your RedirectIfAuthenticated
keeps redirecting all the time. 您的
RedirectIfAuthenticated
始终保持重定向。 It doesn't ever get to $next($request)
for Authenticated User. 对于Authenticated User,它永远不会获得
$next($request)
。
You need to have some logic like 你需要有一些逻辑
if (route is seeker.home and user can visit seeker.home) {
return $next(request);
}
instead of 代替
return redirect()->route('job-seeker.home');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.