Developer Snippet Diary

auth all functions | auth() | Laravel 10

In Laravel, auth() is a helper function that provides a simple way to access the authentication services provided by the framework.

When a user logs in to a Laravel application, a session is created, and the user's details are stored in the session. The auth() function provides an easy way to access the current authenticated user's information and to perform various authentication-related operations.

First of all use this facade in controller

use Illuminate\Support\Facades\Auth;

Now you can use auth() 

auth()->check()

Returns a Boolean indicating whether the user is currently authenticated or not.

auth()->user()

Returns an instance of the currently authenticated user model, or null if the user is not authenticated.

auth()->id()

Returns the ID of the currently authenticated user, or null if the user is not authenticated.

auth()->guard($name = null)

Returns an instance of the specified authentication guard. If no guard name is provided, it returns the default guard instance.

auth()->attempt($credentials, $remember = false)

Attempts to authenticate a user based on the provided credentials. If the authentication is successful, the user is logged in and a session is created. The $remember parameter is optional and defaults to false.

 auth()->once($credentials)

Attempts to authenticate a user based on the provided credentials for just one request. If the authentication is successful, the user is logged in for the current request only.

auth()->login($user, $remember = false)

Logs in the specified user and creates a session for them. The $remember parameter is optional and defaults to false.

auth()->logout()

Logs out the currently authenticated user and destroys the session.

 auth()->viaRemember()

Returns a Boolean indicating whether the user was authenticated via the "remember me" cookie.

 auth()->loginUsingId($id, $remember = false)

Logs in the user with the specified ID and creates a session for them. The $remember parameter is optional and defaults to false.

auth()->logoutOtherDevices($password)

Logs out the user from all other devices where they are currently logged in, except for the current device. The $password parameter is the user's current password.

 auth()->validate($credentials)

Validates the provided credentials without actually logging in the user.

 auth()->onceUsingId($id)


Logs in the user with the specified ID for just one request.

Posted by: R GONDAL
Email: rizikmw@gmail.com