具有中间件和auth的多步注册表单导致无限循环

时间:2017-09-12 10:30:42

标签: php laravel laravel-5

我正在尝试编写一个多步骤注册表单,其中第一个表单注册用户,以下表单形成完整的用户详细信息(单独的页面)。我的目标是允许用户(通过错误或故意)在注册第一个表单后退出,允许他们稍后登录并将其设置在他们中断的位置。

我添加了一个数据库表“register_steps”来确定用户已完成的页面,然后我尝试将此数据提取到中间件并进行相应的重定向。

看起来我在Auth的redirectTo和我的Register的重定向中间件之间发生了无限循环冲突。

中间件

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class CompleteRegistration
{
    /**
     * Redirect user if profile is not complete
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::user()){
            $step = Auth::user()->registration_steps;
            switch ($step) {
                case 1:
                    return redirect('/register/profile');
                    break;
                case 2:
                    return redirect('/register/photo');
                    break;
            }
        }
        return $next($request);
    }
}

路线

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationBasic')->name('register.basic');
Route::post('register', 'Auth\RegisterController@registerBasic');
Route::get('register/profile', 'Auth\RegisterController@showRegistrationProfile');
Route::post('register/profile', 'Auth\RegisterController@showRegistrationProfile');
Route::get('register/photo', 'Auth\RegisterController@showRegistrationPhoto');
Route::post('register/photo', 'Auth\RegisterController@showRegistrationPhoto');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

// Website Pages
Route::get('/', 'WelcomeController@index')->middleware('guest');
Route::get('/browse', 'ProfileController@show')->middleware('auth', 'register');
Route::get('/profile', 'ProfileController@index')->middleware('auth', 'register');
Route::get('/profile/edit', 'ProfileController@edit')->middleware('auth', 'register');

内核

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'register' => \App\Http\Middleware\CompleteRegistration::class
];

1 个答案:

答案 0 :(得分:1)

我最终将额外的表单分成另一个控制器而不是RegisterController,因此它不会重定向,然后在我的OP中使用中间件检查数据库以查看表单是否已完成,如果没有,重定向到未完成的表单。