hkguile
hkguile

Reputation: 4369

laravel 5.2 how to protect route only for authenticated user

i'm new to laravel, i had installed auth scaffold, here is the route up to now.

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

    Route::auth();
    Route::get('/home', 'HomeController@index');    


    Route::get('addthreadhtml', function()
    {
        return View::make('addThreadForm');
    });
    Route::post('thread/add', 'ThreadController@addthread');
    Route::get('thread/showall', 'ThreadController@showallthread');

});

i want to protect addthreadhtml from non-authenticated user access, if the user don't login, they will be redirected to another view.

How can i do that?

Upvotes: 3

Views: 7551

Answers (2)

Adam
Adam

Reputation: 78

if you want the auth for the function only, you can put this on the top code

if(!Auth::user('id')){
   //redirect to any view not require auth
}

or this code

if (Auth::check()) {
    // The user is logged in...
}

Upvotes: 1

Frank Martin
Frank Martin

Reputation: 1851

Add the auth middleware to the route you want to protect:

Route::get('addthreadhtml', ['middleware' => 'auth', function () {
    return View::make('addThreadForm');
}]);

Upvotes: 5

Related Questions