XamarinDevil
XamarinDevil

Reputation: 891

how to make url unauthorized - laravel

Below is my authenticate.php which prevents guests from login into my system. But there is one route that i want to make public to my visitors/guests to be able to access. How can i exempt it from the auth.

The function in authenticate.php seems to guard my whole route at the moment.

This is the route I want to make public Route::get('/survey/{survey}', 'SurveyController@detail_survey')->name('detail.survey'); How do i achieve this please?

Authenticate.php

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            }

            return redirect()->guest('login');
        }

        return $next($request);
    }
}

Routes.php

Route::get('/', 'SurveyController@home');
Route::get('/survey/new', 'SurveyController@new_survey')->name('new.survey');
Route::get('/survey/{survey}', 'SurveyController@detail_survey')->name('detail.survey');

Upvotes: 2

Views: 543

Answers (2)

Utkarsh Pandey
Utkarsh Pandey

Reputation: 1716

In routes.php You can group you authenticated with auth middleware. Also put non authenticated route outside from that group.

Route::get('visitors/guests', 'someController@guestsHandler');

Route::group(['middleware' => 'auth'], function() {
  Route::get('/survey/new', 'SurveyController@new_survey')->name('new.survey');
  Route::get('/survey/{survey}', 'SurveyController@detail_survey')->name('detail.survey');
});

Note: Remove auth from global middlewares (Kernel.php)

Upvotes: 1

Techno
Techno

Reputation: 1696

So, first, you need to spot where you register your authentication-logic. Usually you will find it in app\controllers\middleware\kernel.php under the Variable $middlewareGroups and the key: 'web'(providing you are using your web.php routes file as supplied by laravel). It should, however, have a key in the variable $routeMiddleware like so:

['Authenticate' => yourMiddlewareClass::class],

Otherwise you might have registered it somewhere else, maybe trough a serviceprovider in config\app.php. When you unhooked the logic, you should be able to use this structure in your web.php routes file:

//Routes outside group are not protected
Route::get('/', function () {
    return view('welcome');
});
Route::group(['middleware' => 'Authenticate'], function()
{
    //All routes in the group are now protected by your middleware
    Route::get('canyouseeme', function()
    {
        echo 'Yarh';
    });
}); 

Upvotes: 1

Related Questions