How to redirect the authenticated user to a specific page?

1

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

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

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

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

With this the register page 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');
});
    
asked by anonymous 20.03.2016 / 03:38

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 redirection path if the user visits a unique visitors page (middleware guest ), change this path in the 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 much of a fan of your route / pointing to AuthController . A route to this already exists ( /login ). Maybe it's a redirect for /dashboard to get better. You can also join your Route::group to one, since both are equal (the difference is that the second is generated by php artisan make:auth )

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

I was able to resolve 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 do not know if it would have any better way but it's there.

    
20.03.2016 / 03:51