<?php

namespace App\PackagesAdmin\Middleware;

use App\PackagesAdmin\Manager\Models\Manager;
use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Facades\Auth;
use Redirect;

/**
 * Class AuthenticatedAdmin
 * @package App\PackagesAdmin\Middleware
 */
class AuthenticatedAdmin
{
	/**
	 * @param $request
	 * @param Closure $next
	 * @param null $guard
	 *
	 * @return mixed
	 * @throws AuthenticationException
	 */
	public function handle($request, Closure $next, $guard = null)
	{
		$guard = Auth::guard(Manager::WEB_GUARD);
		if ($guard->check()) {
			return $next($request);
		}

		throw new AuthenticationException(
			'Unauthenticated.',
			[Manager::WEB_GUARD],
			route('admin-auth.action.login')
		);
	}
}
