How to redirect the authenticated user to a specific page?

Asked

Viewed 2,904 times

1

I am restricting the registration page only to be shown or accessed after the login.

A friend of the stackoverflow group told me that could be doing this way:

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'register', 'showRegistrationForm']]);
    $this->middleware('auth', ['only' => ['register', 'showRegistrationForm']]);
}

But this way when I login the system automatically directs me to page /register and what I would like to be directing to the page /dashboard

With this the page Register would be accessed only if I clicked on the link referring to it.

My routes:

Route::group(['middleware' => ['web']], function () {
    Route::get('/', 'Auth\AuthController@getLogin');
});

Route::group(['middleware' => 'web'], function () {
    Route::Auth();
    Route::get('/dashboard', 'HomeController@index');
});

2 answers

1


In fact, by default Laravel will redirect you after logging in to the root of the site /, which in your case is 'Auth\AuthController@getLogin'.

To change the redirect path if the user visits a unique visitors page (middleware guest), alter that path at middleware RedirectIfAuthenticated

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/o-caminho-novo-que-eu-quiser');
    }

    return $next($request);
}

I’m not a fan of your route / point to the AuthController. There’s already a route to it (/login). Maybe he’s a redirect to /dashboard get better. You can also join your Route::group for only one, because both are equal (the difference is that the second is generated by the php artisan make:auth)

Route::group(['middleware' => ['web']], function () {
    Route::get('/', function () {
        return redirect('/dashboard');
    });
    Route::get('/dashboard', 'HomeController@index');
    Route::Auth();
});
  • Our guy thank you so much I did this way that you passed me got a much cleaner code and I believe it’s now 100%. Thank you so much for your help.

  • You’re welcome @Douglassalomão :) If you like, you can also leave a positive vote (http://meta.pt.stackoverflow.com/questions/2632/vote-hoje-vote-amanh%C3%A3-vote-always-vote-conscious)

0

I was able to solve the problem by including the following route in my Routes.php:

Route::group(['middleware' => ['web']], function () {
   Route::get('/', 'Auth\AuthController@getLogin');
});

Route::group(['middleware' => 'web'], function () {
    Route::Auth();

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

Route::group(['middleware' => ['web', 'auth']], function () {
    Route::get('/register', 'Auth\AuthController@showRegistrationForm');
});

I don’t know if I could do any better.

  • Route::Auth(); already creates the routes of register, login, etc... Try not to overwrite your route definitions. Soon you will get lost from so many places that you are applying your middleware. It is worth taking a good look at the doc of the Laravel on middleware and routes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.