Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel redirect if logged in

Tags:

i am using laravel 5.1.8. i am making a login/registration system. i made a controller named AdminController and protect it with middleware.

but i am using laravel's default AuthController which methods and classes are located in different locations. where routes are:

Route::Controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController'
]);

get('admin', 'AdminController@index');
get('profile', 'AdminController@profile');
get('article', 'AdminController@article');

users cannot access AdminController without logging in. its redirected to login page. but i want, if a logged in user typed the address of login page or registration on the address bar of browser, the page will redirected to AdminController.

when i try to do this, it looking for '/home' and gives errors. i want to make it '/admin'.

like image 889
smartrahat Avatar asked Aug 20 '15 23:08

smartrahat


People also ask

How to redirect login route in Laravel?

if(Auth::check()) { return View::make('view_page'); } return Redirect::route('login')->withInput()->with('errmessage', 'Please Login to access restricted area. ');

How do I redirect a user to another page after login in laravel?

By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home'; To change that default behaviour, add the following code in App/Http/Controllers/Auth/LoginController. php . This will redirect users (after login) to where ever you want.

How do I change the default redirect in laravel 8?

Laravel 8 Auth Redirection Using redirectTo() Method You can either remove the $redirectTo variable or leave it as it will be simply overridden by the redirectTo() method.

How can I tell if logged in laravel 8?

“check if user is logged in laravel 8” Code Answer'sif (Auth::check()) { // The user is logged in... }


1 Answers

go to App\Http\Middleware\RedirectIfAuthenticated then change it from

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        return redirect('/home');
    }

    return $next($request);
}

to

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        return redirect('/admin');
    }

    return $next($request);
}
like image 98
mdamia Avatar answered Sep 19 '22 17:09

mdamia