I am building an admin panel. Where i give the option to blacklist a user. If the user is logged in at the time of blacklisting, how can i destroy his session. his session has the following keys- user_id, username, is_login. I want to set the is_login to false. I am working with php
CodePudding user response:
you can do that by :
- add field at users table
blocked
- add
middleware
that checks forblocked
value of user - if true fire
Auth::logout();
for more info kindly check the following article 3 Ways to Delete User in Laravel: Block, Hide or Hard-Delete?
CodePudding user response:
Step 1: Create banned_at column at users table.
Step 2: Create middleware for this For example: CheckBanned
In CheckBanned.php Middleware
public function handle ( $request, Closure $next )
{
if ( auth ()->check () && auth ()->user ()->banned_at )
{
auth ()->logout ();
$message = __ ( 'auth.banned_error' );
return redirect ()->route ( 'login' )->with ( 'error',$message );
}
return $next( $request );
}
Step 3: Add middleware to app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
....
....
\App\Http\Middleware\CheckBanned::class,
....
],
....
....
],