Yokida
Yokida

Reputation: 5

Laravel 5.2 : Restrict routes for guest and auth

I want to let guest access this route :

Route::group(['middleware' => ['guest']], function () {
    Route::get('explore', 'FormController@formsList');
});

But I also want people who are artist to access this route but not the labels :

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

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

    });

    Route::group(['middleware' => 'isArtist'],function()
    {
        Route::get('explore', 'FormController@formsList');
    });

});

The thing is that guests can't access the route but artists can and it's not what I want.

Label middleware

public function handle($request, Closure $next)
    {
        if (!Auth::guest() && Auth::user()->type->idtype ===1) {
            return $next($request);

        }
        return redirect('/');

    }

Artist Middleware

public function handle($request, Closure $next)
    {
        if (!Auth::guest() && Auth::user()->type->idtype ===2) {
            return $next($request);

        }
        return redirect('/');

    }

Upvotes: 0

Views: 1147

Answers (1)

TheAlexLichter
TheAlexLichter

Reputation: 7289

You cannot "define" or group a route twice. That's why your current code does not work

I'd suggest creating a new middleware called ArtistAndGuestMiddleware where you define that guests and artists can pass.

public function handle($request, Closure $next)
    {
        if (Auth::guest() || Auth::user()->type->idtype ===2 ) {
            return $next($request);

        }
        return redirect('/');

    }

Upvotes: 2

Related Questions