laravel

Determining if the Current User is Authenticated

Determining if the Current User is Authenticated

Problem

You want to see if a user is logged in.

You know Laravel automatically keeps the authenticated user in the session. You want to check if the current request has a user logged in and authenticated.

Solution

Use Auth::check().

The Auth::check() method returns true or false.

if (Auth::check())

echo "Yay! You're logged in.";

Discussion

Several things happen behind the scenes when you do this.

First Laravel checks if the current session has the id of a user. If so, then an attempt is made to retrieve the user from the database.

If that fails, then Laravel checks for the “remember me” cookie. If that's present then once again an attempt is made to retrieve the user from the database.

Only if a valid user is retrieved from the database is true returned.

The 'guest' filter uses this method
Laravel provides a default implementation of the guest filter in app/filters.php.

Route::filter('guest', function()

if (Auth::check()) return Redirect::to('/');
);

This default implementation is used when you want to add a filter to a route that is only accessible by guests (aka users who are not logged in). If a user is logged in then they are redirected to the home page.

Gry Top 10 Games to Play on Ubuntu
Top 10 Games to Play on Ubuntu
Windows platform has been one of the dominating platforms for gaming because of the huge percentage of games that are developing today to natively sup...
Gry 5 najlepszych gier zręcznościowych dla systemu Linux
5 najlepszych gier zręcznościowych dla systemu Linux
W dzisiejszych czasach komputery to poważne maszyny używane do gier. Jeśli nie możesz uzyskać nowego wysokiego wyniku, będziesz wiedział, o co mi chod...
Gry Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 wydana w zeszłym miesiącu jest szóstą wersją rozwojową w 1.13.Seria x i zapewnia szereg ulepszeń, w szczególności w interfejsie...