1
votes

I have an issue with laravel route. I want that auth register route can be access by only admin or logged users. To achieve this i have removed Route::auth(); from routes.php and create my own route entry in auth middleware.

Effort

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

    // Authentication Routes...
    $this->get('login', 'Auth\AuthController@showLoginForm');
    $this->post('login', 'Auth\AuthController@login');
    $this->get('logout', 'Auth\AuthController@logout');

    // Password Reset Routes...
    $this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    $this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    $this->post('password/reset', 'Auth\PasswordController@reset');

    Route::get('/home', 'HomeController@index');
    Route::post('/ajax/getStates', 'ConfigurationController@getStates');
    Route::post('/ajax/getCities', 'ConfigurationController@getCities');


});


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

    // Registration Routes...
    $this->get('register', 'Auth\AuthController@showRegistrationForm');
    $this->post('register', 'Auth\AuthController@register');

});

Above code working fine. when i tried to access register url it simply redirect me to the login page. Now actual problem start after login.

After login, i tried to access register page, but it does not show up, instead it redirect me to the home like http://localhost/.

Please suggest me the solution.

2
Its because you have set your register route within your auth middleware instead you need to get it within your web middleware only so you can access it without loggingNarendrasingh Sisodia
I want it should be access by logged user, not anonymous user. that is why i put this in auth middleware.Mandy
Then why're you having it within your AuthControllerNarendrasingh Sisodia
This is the requirement that only administrator can register new user or any appointed authorized user can. I am using laravel existing auth register. There is no point how should auth will be used, thing is that you should able to use it there own way.Mandy

2 Answers

2
votes

Check your AuthController's constructor. It has the guest middleware assigned to all methods except logout.

0
votes

I got a simple solution. I am not expert, but I am going by this way and It's working as you want.

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

Route::get('/edit', function () {
    if (Auth::user()) {
        return view('auth.user_edit'); //Page which you want to show for loged user.
    } else {
        return "MM"; //You can redirect from here, if user is not logged in
    }
});
});