Muhammad Muzammil
Muhammad Muzammil

Reputation: 9

redirect to admin route when admin not loged in

I wanted to check if the user logged in and then accessed the dashboard route; otherwise redirected to the admin login page, But it always turned me to the admin page while I entered my email and password.

Middleware

public function handle(Request $request, Closure $next)
{
    if (Auth::check()) {
        // validation successful!
        return redirect('dashboard');
    } else {
        return redirect('admin');
    }

    return $next($request);
}

Routes

Route::group(['middleware'=>['protectPage']],function(){
    Route::view('dashboard','admin_dashboard');
});

Controller

public function admin(Request $req)
{
    $email = $req->input('email');
    $password = $req->input('password');
    $req->session()->put(['email' => $email, 'password' => $password]);
    $user = Admin::where('email', '=', $email)->first();
    
    if ($user && $user->password === $password) {
        return redirect('dashboard');
    } else {
        {
            return redirect('admin')->with('failed', 'Invalid Credentials');
        }
    }
}

Upvotes: 0

Views: 177

Answers (1)

JS TECH
JS TECH

Reputation: 1583

Redirect admin page only when user is not logged in.

public function handle(Request $request, Closure $next)
{
    if (!Auth::check())
    {
        return redirect('admin');
    }  

    return $next($request);
}

Upvotes: 0

Related Questions