2

I have a Laravel application using API token authentication. By default, the user needs to pass the api_token parameter as part of the URL, but I want to change api_token to a custom name parameter like api_key.

Currently the full URL looks like:

https://www.example.com/api/v2/?api_token=something&action=balance

however I want it to look like following:

https://www.example.com/api/v2?api_key=something&action=balance

OR

https://www.example.com/api/v2?key=something&action=balance

My API routes are using a middleware called auth:api, but I am unable to find this middleware to try and change its configuration.

0

2 Answers 2

0

It's been a while since I've done it, but I believe that the auth.guards.api.input_key settings will allow you to specify it. So your auth.php would look partly like this:

<?php

return [
    "guards" => [
        "api" => [
            "driver" => "token",
            "provider" => "users",         // the database table
            "storage_key" => "api_token",  // the database column
            "input_key" => "api_key",      // the query string component
            "hash" => true,
        ],
    ],
];
Sign up to request clarification or add additional context in comments.

Comments

0

you can simply create your own middleware

public function handle($request, Closure $next)
{
    $token = request('key'); //it can be anything 
    if ($token != 'abc') { // this value can be static or can be get from database
        return response([
            'error' => 2,
            'message' => ["Access Denied"]
        ]);
    }
    return $next($request);
}

here you can match with token and allow them certain request

NOTE: it will work on url paramter not headers token for header you need to get token from header

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.