Laravel redirect to other pages after successfull login

Asked
Active3 hr before
Viewed126 times

7 Answers

laravelloginpagesredirect
90%

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.,Want to know how to redirect user after login in your Laravel app? Then this short article will help you out! By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home';,If your users are based on roles, then you can redirect them (after login) to a particular page based on their role such as Admin, Author and Subscriber. Have a look at the code sample below:,By default this method is left empty in AuthenticatesUsers trait. You can apply whatever logic you want to apply inside this method to redirect users. 

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.

// inside LoginController class
use AuthenticatesUsers;

// check if authenticated, then redirect to dashboard
protected
function authenticated() {
   if (Auth::check()) {
      return redirect() - > route('dashboard');
   }
}
load more v
88%

Path Customization (tested in laravel 7) When a user is successfully authenticated, they will be redirected to the /home URI. You can customize the post-authentication redirect path using the HOME constant defined in your RouteServiceProvider:,For newer versions of Laravel, please replace protected $redirectTo = RouteServiceProvider::HOME; with protected $redirectTo = '/newurl'; and replace newurl accordingly., 2 I am glad it helped, my application uses isAdmin() to check wether the user is an Admin before redirection, could'nt have the chance to change according your code just wanted to give you a hint. Thanks – Babagana Feb 12 '17 at 15:00 ,If you look in the AuthenticatesUsers trait you will see that in the sendLoginResponse method that there is a call made to $this->redirectPath(). If you look at this method then you will discover that the redirectTo can either be a method or a variable.

You also need to add the following lines into your LoginController

namespace App\ Http\ Controllers\ Auth;

use App\ Http\ Controllers\ Controller;

use Illuminate\ Foundation\ Auth\ AuthenticatesUsers;

use Illuminate\ Http\ Request;

class LoginController extends Controller {
   /*
   |--------------------------------------------------------------------------
   | Login Controller
   |--------------------------------------------------------------------------
   |
   | This controller handles authenticating users for the application and
   | redirecting them to your home screen. The controller uses a trait
   | to conveniently provide its functionality to your applications.
   |
   */

   use AuthenticatesUsers;

   protected
   function authenticated(Request $request, $user) {
      if ($user - > isAdmin()) { // do your magic here
         return redirect() - > route('dashboard');
      }

      return redirect('/home');
   }
   /**
    * Where to redirect users after login.
    *
    * @var string
    */
   //protected $redirectTo = '/admin';

   /**
    * Create a new controller instance.
    *
    * @return void
    */
   public
   function __construct() {
      $this - > middleware('guest', ['except' => 'logout']);
   }
}
load more v
72%

class RegisterController extends Controller {
   protected $redirectTo = '/home';

   protected
   function redirectTo() {
      if (auth() - > user() - > role_id == 1) {
         return '/admin';
      }
      return '/home';
   }

}
load more v
65%

Laravel uses the url.intented key to store the url of the intended page in the session, so it automatically knows where to redirect after login. This only works though, when we try to access a protected page and we are automatically redirected to the login.,After that change, the users will always be redirected to the url saved in the url.intented session key if it is set.,If you have added the auth middleware to protect specific routes in your routes/web.php, then your users will be redirected to the login page if they try to access them. In that case, they will be redirected back to the intended page automatically after successful authentication.,What it does, is to always store the previous url page in the session if the previous page is from our website but not the login page itself. Now, we can link to the login route from any other page we want and have the auth system redirect users back to the original page after successful login.

You’ve set up your new Laravel 8 project and installed the laravel/ui composer package. Moreover, you’ve added the auth boilerplate along with the vue or react preset and migrated the required database tables for your auth system.

laravel / ui
load more v
75%

Hi, in this post I will dive in the auth scaffold to redirect users to different areas by roles.,If you want to redirect authenticated users on authentication routes by role to different paths, just edit the "RedirectIfAuthenticated" middleware to redirect by different roles.,This means that we can find a RouteServiceProvider const named "HOME", we can override this const and it will change the redirect endpoint after login, but this is not what we need to redirect users by role or by any other condition.,This AuthenticatesUsers trait has another trait "RedirectsUsers" this RedirectsUsers trait has a method "redirectPath":

    public
    function up() {
       Schema::create('users', function(Blueprint $table) {
          $table - > id();
          $table - > string('name');
          $table - > string('email') - > unique();
          $table - > timestamp('email_verified_at') - > nullable();
          $table - > string('password');
          $table - > string('role') - >
             default ('user');
          $table - > rememberToken();
          $table - > timestamps();
       });
    }
load more v
40%

Redirect responses are instances of the Illuminate\Http\RedirectResponse class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper:,When you call the redirect helper with no parameters, an instance of Illuminate\Routing\Redirector is returned, allowing you to call any method on the Redirector instance. For example, to generate a RedirectResponse to a named route, you may use the route method:,You may use the withInput method provided by the RedirectResponse instance to flash the current request's input data to the session before redirecting the user to a new location. Once the input has been flashed to the session, you may easily retrieve it during the next request:,Redirecting to a new URL and flashing data to the session are usually done at the same time. Typically, this is done after successfully performing an action when you flash a success message to the session. For convenience, you may create a RedirectResponse instance and flash data to the session in a single, fluent method chain:

Redirect responses are instances of the Illuminate\Http\RedirectResponse class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper:

Route::get('/dashboard', function() {
   return redirect('/home/dashboard');
});
load more v
22%

And, that’s it – now, after registration user will be redirected to the page they were visiting before ‘auth’ middleware restricted their access.,You visit /posts URL;,The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. ,Look at the last part of redirection. As you can see, it uses simple redirect(), without intended(). So this is the part we need to change.

If you dig deeper into the LoginController logic, it uses Trait AuthenticatesUsers.php from core Laravel’s /vendor folder. And it has this method:

public
function login(Request $request) {
   // ...

   if ($this - > attemptLogin($request)) {
      return $this - > sendLoginResponse($request);
   }

   // ...

}
load more v

Other "laravel-login" queries related to "Laravel redirect to other pages after successfull login"