<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
class CheckSubscribeUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {

        if (! $request->user() ||
        ($request->user() instanceof MustVerifyEmail &&
        ! $request->user()->hasVerifiedEmail())) {

        return $request->expectsJson()
                ? abort(403, 'Ваш адрес электронной почты не подтвержден.')
                : Redirect::guest(URL::route('verification.notice'));
        }

        // if($request->path() === '/' && empty(auth()->user())){
        //     return $next($request);
        // }

        // abort_if(empty(auth()->user()), 403);

        // if (auth()->user()->checkClosedAccess()) {
        //     return redirect(route('setting.tarif'))->with('error', 'Оплатите подписку!');
        // }

        return $next($request);
    }
}
