mardi 12 septembre 2017

Multi-step registration form with middleware and auth causing an infinite loop

I am trying to write a multi-step registration form where the first form registers the user and the following forms complete user details (separate pages). My goal is to allow the user (by error or on purpose) to exit after registering the first form allowing them to login later and set them where they left off.

I added a database table "register_steps" to determine which page the user has completed then I am trying to pull this data into a middleware and redirect appropriately.

It looks like I am getting an infinite loop conflict between Auth's redirectTo and my Register's redirect middleware.

Middleware

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);
    }
}

Routes

// 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');

Kernel

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
];



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire