enamul haque
enamul haque

Reputation: 1309

Laravel 8 multi auth not authenticating and redirecting

My admin controller extends the controller class and there I used method to load the view pages and passed the guard. config/auth.php also modified by adding admin guards and providers. In app/Actions/Fortify folder I added AttemptToAuthenticate and RedirectIfTwoFactorAuthenticatable class and changed the namespace also. My admin controller also extends the Guard. RedirectIfAuthenticated middleware for user and adminRedirectIfAuthenticated for admin by passing guard to the redirect in handle mehthod.All middleware are registered. In providers/FortifyServiceProvider.php file I added classed to the register.

    **FortifyServiceProvider.php**
        public function register()
            {          
                $this->app->when([
                    adminController::class,
                    AttemptToAuthenticate::class,
                    RedirectIfTwoFactorAuthenticatable::class
                   
                ])->needs(StatefulGuard::class)->give(function(){
                    return Auth::guard('admin');
                });
            }
    **LoginResponse.php:**      
        public function toResponse($request)
            {
                return $request->wantsJson()
                            ? response()->json(['two_factor' => false])
                            : redirect()->intended('admin/dashboard');
            }
    **RouteServiceProvider.php**   
     public static function redirectTo($gaurd){
            return $guard."/dashboard";
        }
    **Web.php**
        Route::group(['prefix'=>'admin','middleware'=>['admin:admin']],function(){
                Route::get('/login',[adminController::class,'loginform']);
                Route::post('/login',[adminController::class,'store'])->name('admin.store');
            }
        );
        Route::middleware(['auth:sanctum,web', 'verified'])->get('/dashboard', function () {
            return view('dashboard');
        })->name('dashboard');
    
        Route::middleware(['auth:sanctum,admin', 'verified'])->get('/admin/dashboard', function () {
            return view('dashboard');
        })->name('dashboard');
    **Login.blade.php**    
    <form action="{{isset($guard)? url($guard.'/login'):route('login')}}" method='POST'>
                      @csrf

But when I do login there's no error or redirecting. It's just staying in the same page.

Upvotes: 0

Views: 391

Answers (1)

Dyos
Dyos

Reputation: 381

in your RouteServiceProvider.php file, there is a typo error

 **RouteServiceProvider.php**   
     public static function redirectTo($gaurd){
            return $guard."/dashboard";
        }

Must be $guard instead of $gaurd.

Upvotes: 0

Related Questions