Home > Blockchain >  how to get auth information in AppService provider?
how to get auth information in AppService provider?

Time:01-19

I'm trying to give a condition is user suspend, user should redirect to login page if user in login state.

I have tried to give a check in AppServiceProvider like below

public function boot()
{
    if(auth()->user()->is_deleted === true) return redirect('/logout');
}

Problem is in AppServiceProvider I'm not getting any auth information. How I can solve this problem ?

CodePudding user response:

You can do this better with Middlewares,

Create a middleware CheckIfDeleted

php artisan make:middleware CheckDeleted

Open the CheckIfDeleted and update the handle method:

public function handle(Request $request, Closure $next)
{
    if(auth()->check() && auth()->user()->is_deleted === true){
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('some error page?');
    }
    return $next($request);
}

Open the Kernel.php (app/http folder), then add the new middleware in the web section (protected $middlewareGroups):

\App\Http\Middleware\CheckIfDeleted::class,

I also recommend you implement the softDelete of laravel, you can use this guide from stack: Laravel Soft Delete posts

In this case if the user is deleted, they will get an error message, and you will still keep the record of the user :)

  • Related