<?php

namespace App\Http\Middleware;

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

class EnsureUserStatus
{
    public function handle(Request $request, Closure $next)
    {
        if (Auth::check() && !Auth::user()->is_active) {
            Auth::logout();

            if ($request->expectsJson()) {
                return redirect()->back()->with('error', __('Your account is inactive. Please contact the administrator.'));
            }

            return redirect()->back()->with('inactive', __('Your account is inactive. Please contact the administrator.'));
        }

        return $next($request);
    }
}
