Which filter is faster/better
I used this API as a starter pack:
https://github.com/akuzemchak/laracon-todo-api
The filter for the basic auth looked like this:
Route::filter('api.auth', function()
{
if (!Request::getUser())
{
App::abort(401, 'A valid API key is required');
}
$user = User::where('api_key', '=', Request::getUser())->first();
if (!$user)
{
App::abort(401);
}
Auth::login($user);
});
Which would authenticate a user like: curl -u "youruserapikeygoeshere:whatever" http://localapidomain.dev/v1/lists
But wont it be slow to check the api key against the varchar field on the DB?
I modified my filter so that I send the users id as username then the password as the api key.
Then my query will select the user from the primary key and then check if the API key match.
Coz if I understand it right it will be faster to make a lookup query against a primary key instead of " WHERE api_key = key "
Route::filter('api.auth', function()
{
if (!Request::getUser())
{
App::abort(401, 'A valid user ID and API key is required');
}
$user = User::where('user_id', '=', Request::getUser())->first(['api_key']);
if (!$user)
{
App::abort(401);
}
if ($user->api_key == Request::getPassword())
{
Auth::login($user);
}
else
{
App::abort(401);
}
});
So is my second filter better/faster?
Please or to participate in this conversation.