API Keys Show Never Used

Solved
nick-jensen

Nov 16th, 2018 01:15 PM

These API keys have been used but they are showing Never Used. Where is this being updated?

tnylea

Nov 19th, 2018 02:48 PM

Best Answer

Hey Nick,

Thanks for pointing that out. I've added a fix to make this update when the key is used. You can make this modification inside of: wave\src\Http\Controllers\API\AuthController.php

on line 53, you should find the token() function:

public function token(){
    $request = app('request');

    if(isset($request->key)){

        $key = \Wave\ApiKey::where('key', '=', $request->key)->first();

        if(isset($key->id)){
            return response()->json(['access_token' => JWTAuth::fromUser($key->user, ['exp' => config('wave.api.key_token_expires', 1)])]);
        } else {
            abort('400', 'Invalid Api Key');
        }

    } else {
        abort('401', 'Unauthorized');
    }

}

You'll need to update that function with the following:

public function token(){
    $request = app('request');

    if(isset($request->key)){

        $key = \Wave\ApiKey::where('key', '=', $request->key)->first();

        if(isset($key->id)){
            $key->last_used_at = \Carbon\Carbon::now()->toDateTimeString();
            $key->save();
            return response()->json(['access_token' => JWTAuth::fromUser($key->user, ['exp' => config('wave.api.key_token_expires', 1)])]);
        } else {
            abort('400', 'Invalid Api Key');
        }

    } else {
        abort('401', 'Unauthorized');
    }

}

You'll see that if the key is valid, we will then update the last_used_at, with the following code:

 $key->last_used_at = \Carbon\Carbon::now()->toDateTimeString();
 $key->save();

Let me know if that helps and if you have any other questions.

Appreciate it :)

nick-jensen

Nov 21st, 2018 11:58 AM

Thanks Tony.

This really helped.

I went ahead and added that to my API Middleware since we bypassing the JWT calls.

nick-jensen

Nov 21st, 2018 12:19 PM

Ok I think there is also in issue with the Blade on both themes.

  1. UIKit, I can not update the name of the keys. I also see this error wave.test/settings/type= 500 (Internal Server Error)

  2. While this is now updating the database, it is still showing Never Used. This is solved by editing line 34 of partials/api.blade.php

@if(is_null($apiKey->last_used_at)){{ 'Never Used' }}@else{{ $apiKey->last_used_at }}@endif
tnylea

Nov 24th, 2018 03:09 PM

Thanks @nick-jensen,

Would you mind sharing what you have modified. I see the code here:

@if(is_null($apiKey->last_used)){{ 'Never Used' }}@else{{ $apiKey->last_used }}@endif

Which will display Never Used if it is null, otherwise, it should show the timestamp that it was last used. Let me know and I'll get this updated in the next release.

Appreciate it. Thanks!

nick-jensen

Nov 24th, 2018 09:33 PM

They key in the database is last_used_at

	@if(is_null($apiKey->last_used_at)){{ 'Never Used' }}@else{{ $apiKey->last_used_at }}@endif