<?php

namespace App\Http\Middleware;

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

class checkRole
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::guest())
        {
            if ($request->ajax())
            {
                return Response::make('Unauthorized', 401);
            }
            else
            {
                return redirect('/');
            }
        }

        if ($request->session()->has('user_agent')){
            if ($request->session()->get('user_agent') != 'Admin'){
                return redirect('/');
            }
        }
        return $next($request); 
    }
}
