<?php

namespace Mehrab\User\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Mehrab\User\Models\User;

class CustomAuth
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (auth()->check() && auth()->user()->status != User::BAN) {
            return $next($request);
        }
        if ($request->expectsJson()){
            return response('unauthorized',401);
        }
        return redirect()->route('auth.index');
    }
}
