2

I am using laravel 5.1, after user logged in, the url for login and register still can be access by user.

My current implementation of the route as follow:

<?php

$s = 'public.';
Route::get('/',         ['as' => $s . 'home',   'uses' => 'PagesController@getHome']);

$a = 'auth.';
Route::get('/login',            [ 'as' => $a . 'login',  'uses' => 'Auth\AuthController@getLogin']);
Route::post('/login',           ['as' => $a . 'login-post',     'uses' => 'Auth\AuthController@postLogin']);
Route::get('/register',         ['as' => $a . 'register',       'uses' => 'Auth\AuthController@getRegister']);
Route::post('/register',        ['as' => $a . 'register-post',  'uses' => 'Auth\AuthController@postRegister']);
Route::get('/password',         ['as' => $a . 'password',       'uses' => 'Auth\PasswordResetController@getPasswordReset']);
Route::post('/password',        ['as' => $a . 'password-post',  'uses' => 'Auth\PasswordResetController@postPasswordReset']);
Route::get('/password/{token}', ['as' => $a . 'reset',          'uses' => 'Auth\PasswordResetController@getPasswordResetForm']);
Route::post('/password/{token}',['as' => $a . 'reset-post',     'uses' => 'Auth\PasswordResetController@postPasswordResetForm']);

$s = 'social.';
Route::get('/social/redirect/{provider}',   ['as' => $s . 'redirect',   'uses' => 'Auth\AuthController@getSocialRedirect']);
Route::get('/social/handle/{provider}',     ['as' => $s . 'handle',     'uses' => 'Auth\AuthController@getSocialHandle']);

Route::group(['prefix' => 'admin', 'middleware' => 'auth:administrator'], function()
{
    $a = 'admin.';
    Route::get('/', ['as' => $a . 'home', 'uses' => 'AdminController@getHome']);
});

Route::group(['prefix' => 'user', 'middleware' => 'auth:user'], function()
{
    $a = 'user.';
    Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController@getHome']);
});

Route::group(['middleware' => 'auth:all'], function()
{
    $a = 'authenticated.';
    Route::get('/logout', ['as' => $a . 'logout', 'uses' => 'Auth\AuthController@getLogout']);
});

I tried the following method but it doesn't work out:

Route::get('/login',            ['before'=> 'auth', 'as' => $a . 'login',  'uses' => 'Auth\AuthController@getLogin']);

Thanks!!

6
  • What do you want to achieve? What's the problem here? Commented Sep 22, 2015 at 15:27
  • Even after the user logged-in, he/she still can access the login page. Need to avoid this Commented Sep 22, 2015 at 15:28
  • Why is that a problem? That's how it works on almost every website, including Stack Overflow. It allows you to login with another username. Commented Sep 22, 2015 at 15:29
  • So it's normal for a user to be able to access a login/register form even after he/she already LOGGED-IN? Commented Sep 22, 2015 at 15:31
  • 2
    I disagree with @bernie. It makes sense at a place like stack overflow where you may want to allow multiple logins, there are a lot of times where it makes a lot of sense to only allow one user. Luckily Laravel ships with a fix for this. Just add the RedirectIfAuthenticated middleware to the login route Commented Sep 22, 2015 at 16:11

1 Answer 1

2

You need to implement a helper(Middleware) for the login to do the redirect. Check app/Http/Middleware/RedirectIfAuthenticated.php its a good example to start from. No need to create routes for this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.