<?php namespace App\Http\Middleware;

use App\Models\Employee;
use Closure;

class StaffTokenMiddleware {

	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next)
	{
        if($payload = $request->header('X-Auth-Token'))
        {
            $token = Employee::tokenValid()->where('api_token',$payload)
                ->where('id',$request->get('id'))
                ->first();
            if($token)
                return $next($request);
        }
        return response('Unauthorized.', 401);
	}

}
