Reputation: 13
I am new to laravel and can't manage to to check whether the user is logged on or not, before accessing page trough URL directly.
I suppose the check should be done in the routes and there is no problem to do so when calling a view from the route:
Route::get('/', function()
{
if (!Session::has('loggedOn'))
{
return view('viewLogin');
}
else
{
return view('viewAllData');
}
});
enter code here
But when I want to call a Controller and check if (!Session::has('loggedOn')), how is this done?
Route::get('/messagesPage', 'MsgsController@messages');
Upvotes: 0
Views: 1444
Reputation: 1270
the below will see if the user is logged in
add
use Illuminate\Support\Facades\Auth;
to your controller and the below to check
if (Auth::check()) {
// The user is logged in...
}
edited to add answer for second question
Route::get('profile', function () {
// Only authenticated users may enter...
})->middleware('auth');
the above will only let a user access the path if they are logged in
Upvotes: 2