Laravel 5 check whether a user is logged in
You can use middleware
in controller
- All actions in controller require to be logged in
public function __construct()
{
$this->middleware('auth');
}
- Or you can check it in action
public function create()
{
if (Auth::user()) { // Check is user logged in
$example= "example";
return View('novosti.create')->with('example', $example);
} else {
return "You can't access here!";
}
}
- Also you can use it on route
Route::get('example/index', ['middleware' => 'auth', 'uses' => 'example@index']);
you can do this directly in your blade code by this way
@if (!Auth::guest())
do this
@else
do that
@endif
You should use the auth
middleware. In your route just add it like this:
Route::get('pages/mainpage', ['middleware' => 'auth', 'uses' => 'FooController@index']);
Or in your controllers constructor:
public function __construct(){
$this->middleware('auth');
}
use
Auth::check()
more here https://laravel.com/docs/5.2/authentication#authenticating-users in Determining If The Current User Is Authenticated