<?php

namespace App\Http\Middleware;

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

class store_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('storeDashboard/login');
        }

        if ( ! in_array( Auth::user()->role, ['super_admin','admin', 'sub_admin','user'] )) {
            Auth::logout();
            return redirect('storeDashboard/login');
        }

        return $next($request);
    }
}
