Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

santiago.correa.e's avatar

I need A route to be only for authenticated users

The thing is that I have the method store into my session controller that redirects to a route, so I need that the method store stay only for guest but the route should be only for auth. here is the code.

    function __construct()
    {
        $this->middleware('guest', ['except'=>'destroy']);
 }
public function store(){

        $request=request(['usuario','password']);

        if(!auth()->attempt($request)){

                return redirect('/');
        }       

         if(User::getCliente($request['usuario'])==0){

                return redirect('/staff');

        }

        return redirect('/cliente');

    }

so I use middleware because only the guests can log in, but if somebody copy the url 'home/staff' or 'home/cliente' can get into this views.

Is there a way that the routes can be only for auth?

0 likes
1 reply
Chris1904's avatar
Route::group(['middleware' => 'auth'], function () {
    Route::get('upload', ['as' => 'upload', 'uses' => 'MediaController@index']);
});

In this case the upload route would only be accessible for authenticated users. Check out middleware - https://laravel.com/docs/5.4/middleware.

3 likes

Please or to participate in this conversation.