- Key Features of Laravel Auth Facade
The Auth facade offers a straightforward syntax, allowing developers to quickly implement authentication features.
- Flexibility:
- It supports various authentication drivers and stores, making it adaptable to different project requirements.
- Security:
- Laravel's built-in features, such as password hashing and session management, enhance the security of your web applications.
Following are the list of useful authentication module methods.
Logout user from system use logout();
Get Logged-In user data
The code retrieves the unique identifier (usually the primary key from the database) of the currently authenticated user using Laravel's authentication system. Auth::id() is a convenience method that returns the ID of the logged-in user without needing to load the entire user model. If no user is currently authenticated, it returns null. This is useful for quickly accessing the user's ID for database queries or logic that requires the user's ID.
Authenticate and remember user check
We can also protect routes of our application.
For example:
For further details and information please see the official docs
Logout user from system use logout();
Auth::logout();
Code is used in the Laravel framework to log out the currently authenticated user. It clears the user's session information, effectively ending the user's authentication state. After this method is called, the user will no longer be considered logged in, and any subsequent attempts to access areas requiring authentication will redirect the user to the login page or prompt them to log in again.Auth::user();
Get logged-In user id$id = Auth::id();
Check if user is logged-In or not
if (Auth::check()) {
}
It checks if the current user is logged in using Laravel's authentication system. The Auth::check() method returns true if the user is authenticated, meaning there is a logged-in user session present. If the condition is true, the code inside the curly braces {} will be executed. This is typically used to conditionally display content or perform actions only for authenticated users.Authenticate and remember user check
if (Auth::attempt(['email' => $email, 'password' => $password], $remember))
{
}
Above example for attempting to authenticate a user. It uses the Auth::attempt() method to check if the provided email and password match an existing user in the database. If the credentials are correct, the user is logged in. The $remember boolean indicates whether to remember the user's session so they don't need to log in again on their next visit. If the authentication is successful, the code within the braces {} will be executed.We can also protect routes of our application.
For example:
Route::get('profile', ['middleware' => 'auth', function()
{
}]);
Route::get('products', ['middleware' => 'auth', function()
{
}]);
Route::get('categories', ['middleware' => 'auth', function()
{
}]);
No comments:
Post a Comment