Laravel 5.4 Redirect after authentication

2019-08-25 02:12发布

问题:

I have the following in my RedirectifAuthenticated middleware:

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('dashboard');
        }

        return $next($request);
    }

But when a user logs in, this is redirecting to /home which I renamed to /dashboard.

I have changed the routes, views etc from home to dashboard.

I have run a search throughout the whole projects and I cannot find a route to /home or a mention of home that is not the title or in unrelated filed such as the yarn.lock file.

I have found many articles on this issue but I do not have an Authenticate.php not do I have the old Auth middleware.

EDIT: Below are my Login Controller and routes:

LoginController:

<?php

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;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    //protected $redirectTo = '/dashboard';
    public function authenticated(Request $request)
    {
        // Logic that determines where to send the user
        if($request->user()->hasRole('Stallhollder')){
            return redirect('/dashboard');
        }
        if($request->user()->hasRole('Manager')){
            return redirect('/dashboard2');
        }
    }
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

Routes:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/dashboard', 'DashboardController@index');
Route::resource('/bookings', 'BookingsController');
Route::get('/dashboard2', function () {
    return view('dashboard2');
});

回答1:

The default Laravel login controller sets the value of $redirectTo to '/home'. You need to update that to the new route as that defines where it gets redirected to after login.



回答2:

After Run The php artisan make:auth you have the following controllers.

  • LoginController
  • RegisterController
  • ResetPasswordController.php
  • ForgotPasswordController.php

Now, In Controller File Change The redirect to dashboard

Example: protected $redirectTo = '/dashboard';

Example ( LoginController )

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

class LoginController extends Controller
{


use AuthenticatesUsers;


protected function authenticated(Request $request)
{

// Logic that determines where to send the user
    if($request->user()->hasRole('Stallhollder')){
        return redirect('/dashboard');
    }
    if($request->user()->hasRole('Manager')){
        return redirect('/dashboard2');
    }

  public function __construct(){
    $this->middleware('guest')->except('logout');
  }

}

Example ( Routes )

<?php

Auth::routes();  // First Line Is `Auth::routes();`

Route::get('/dashboard', 'DashboardController@index');

Route::resource('/bookings', 'BookingsController');

Route::get('/dashboard2', function () {
    return view('dashboard2');
});

?>

Example ( RedirectifAuthenticated middleware )

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/dashboard');
    }

    return $next($request);
}