php - Multi-step registration form with middleware and auth causing an infinite loop -
i trying write multi-step registration form first form registers user , following forms complete user details (separate pages). goal allow user (by error or on purpose) exit after registering first form allowing them login later , set them left off.
i added database table "register_steps" determine page user has completed trying pull data middleware , redirect appropriately.
it looks getting infinite loop conflict between auth's redirectto , register's redirect middleware.
middleware
namespace app\http\middleware; use closure; use illuminate\support\facades\auth; class completeregistration { /** * redirect user if profile 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 ];
i ended separating out forms controller rather registercontroller wouldn't redirect , used middleware in op check db see if forms have been completed, if not, redirect uncompleted form.
Comments
Post a Comment