<?php

namespace App\Http\Middleware;

use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Middleware\BaseMiddleware;
class XchengxuRefreshToken extends  BaseMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        $response = $next($request);

        try {
            $newToken = $this->auth->setRequest($request)->parseToken()->getToken();
            $response->headers->set('Authorization', 'Bearer '.$newToken);
        } catch (TokenExpiredException $e) {
            //return response()->error('XCX_TOKEN_ERROR');
        } catch (JWTException $e) {
            //return response()->error('XCX_TOKEN_ERROR');
        }
        return $response;
    }
}
