Auth intro

Contents
Guards concept
Session guard
Hash passwords
Auth::check

Guards concept

A guard defines how Laravel finds the current visitor on each request. One application can run several guards side by side, for example a web guard for browsers and a token guard for an API.

The Auth facade always talks to the default guard unless the guard method names another one explicitly.

use Illuminate\Support\Facades\Auth; $guard = Auth::guard("web"); $user = $guard->user();

Session guard

The session guard keeps the login state in a cookie backed session. After login it stores the user id, and on every later request it loads the user from that id.

Nothing about passwords lives in the session itself, only the id that points back to the users table.

use Illuminate\Support\Facades\Auth; $user = Auth::user(); $id = Auth::id();

Hash passwords

Passwords are never stored as plain text. The Hash facade turns a password into a one way hash when the account is created, and it verifies a login attempt against that hash later.

Each call produces a different string for the same password, so stolen hashes cannot be compared by eye.

use Illuminate\Support\Facades\Hash; $hash = Hash::make("secret123"); $ok = Hash::check("secret123", $hash);

Auth::check

Controllers and templates ask the Auth facade whether a visitor is logged in right now. The check method answers true for logged in visitors and false for everyone else.

Use the result to show login links to guests and account links to members, or to block actions that need an account.

use Illuminate\Support\Facades\Auth; if (Auth::check()) { echo "Welcome back"; } if (Auth::guest()) { echo "Please log in"; }

Next: Hand-rolled login

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: