How Auth User Can Log Out In Laravel With Examples

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

Authentication is the process of checking specified user credentials. In web applications,Laravel's authentication facilities are made up of "guards" and "providers". Guards define how users are authenticated for each request.authentication is managed by sessions which take the input parameters such as email or username and password,for user identification. If these parameters match, the user is said to be authenticated.

How Auth User Can Log Out In Laravel With Examples

To manually log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session:
 

use Illuminate\Support\Facades\Auth;

Auth::logout();


Example(1)

1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:

Implement below code in web.php file

<a href="{{ url('/logout') }}"> logout </a>
 
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');


Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run
 

php artisan make:auth 



Example(2)

add this to your navigation bar:

<a href="{{ url('/logout') }}"> logout </a>


then add this in your web.php file

Route::get('/logout', 'YourController@logout');


then in the YourController.php file, add this

public function logout () {
    //logout user
    auth()->logout();
    // redirect to homepage
    return redirect('/');
}

 

Related Post