<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Modules\User\Enums\UserRoleEnum;

class Warehouse_Auth
{
    /**
     * Handle an incoming request.
     * Redirect user to admin login page if he has not the previliage to access the admin panel
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ( ! Auth::check() ) {
            return redirect('warehouse/login');
        }

        if ( Auth::user()->role !=  UserRoleEnum::WAREHOUSE->value ) {
            Auth::logout();
            return redirect('warehouse/login');
        }

        return $next($request);
    }
}
