laravel get sender http host with JWTAuth

827 Views Asked by At

I'm using tymondesigns/jwt-auth to create an api. And I need to get the URL of API request sender. So I wrote a middleware and used,

public function handle($request, Closure $next)
    {
        dd(request()->getHttpHost());      
    }

This gets the only api URL. And then I tried,

$request->header('referer');

I get an only a null value. Is there any way to achive this task?

1

There are 1 best solutions below

1
On

I came across a similar question and found a solution. If I call my Laravel 8 API and look for the headers, these may come useful:

host: ["127.0.0.1:8000"]
origin: ["http://localhost:3000"]
referer: ["http://localhost:3000/"]

So then I know that host is where API is hosted, origin and referer, they have some difference tho. Your problem may be caused because you try to access them from middleware, try to check them directly from a controller. Also if you need sender hostname in a clear look, you can use this code:

public function SomeController(Request $request)
    {
        $host = explode('/', $request->header('origin'))[2]
        // Or with the referer
        $host = explode('/', $request->header('referer'))[2]

If you still get null in the response, you can simply debug it, just check for all headers:

$headers = $request->header()