<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class AdminMiddleware
{
    /**
     * @var
     */
    protected $auth;

    /**
     * AdminMiddleware constructor.
     * @param Guard $auth
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // If the user is a guest, or doesn't have permissions
        if ($this->auth->guest() || $this->auth->user()->role != 1) {
            return redirect()->route('home');
        }

        return $next($request);
    }
}
