<?php

namespace App\Http\Middleware;

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

class Roles{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next){
       // $routeName = Route::getFacadeRoot()->current()->uri();

     //   $route = explode('/', $routeName);
        if (!auth(activeGuard())->check()){
            //si c'est une route sur le back-office
            if ($request->is("admin") or $request->is("admin/*")) {
                return redirect()->intended(route('admin.auth.login'));
            }
            return redirect()->intended(route('login'));
        }

        if (!auth(activeGuard())->user()->isCustomer()) {
            return $next($request);
        }

        return redirect()->route('home');
    }
}
