Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - log out specific user

I know how to log in a user, but how do I log out a specified user from the application? There doesn't seem to be enough coverage on this.

like image 667
Onion Avatar asked Jun 18 '14 09:06

Onion


People also ask

How do I logout of current user in Laravel?

To manually log users out of your application, you may use the logout method provided by the Auth facade.

Where is logout method in Laravel?

Where is logout function Laravel? Step 1: Create a route. Navigate routes/web. php then put the following code below: Route::group(['middleware' => ['auth']], function() { /** * Logout Route */ Route::get('/logout', 'LogoutController@perform')->name('logout.

How do I logout of all devices in Laravel?

As you can see I added Auth::logoutOtherDevices() with the parameter of password. So that we can enable to log out of the other active devices.

What is guard in Laravel?

Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies. Providers define how users are retrieved from your persistent storage.


1 Answers

This problem occures when you are admin and want to block some user. Then when you block user you want to logout him imidietly. For laravel 5.2 (maby for lower versions too) you can create middelware:

Create middelware

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class BockedUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $user = Auth::user();
        if ($user and $user->is_bocked) {
            Auth::logout();
            return redirect('/login');
        }

        return $next($request);
    }
}

And in app/Http/Kernel.php in section $middlewareGroups > 'web' add \App\Http\Middleware\BockedUser::class. I assmue that all your routes are in Route::group(['middleware' => 'web'], function () { .. all your routes ..}

like image 63
Kamil Kiełczewski Avatar answered Oct 18 '22 15:10

Kamil Kiełczewski