API Keys Show Never Used

Solved Posted In Wave
  • nick-jensen 1 year ago
    ![](https://preview.ibb.co/eEvRpf/Screen-Shot-2018-11-16-at-9-13-25-AM.png)
    
    These API keys have been used but they are showing Never Used. Where is this being updated?

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

  • Best Answer

    (As selected by nick-jensen)

    tnylea 1 year ago
    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 :)

    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 :)

  • tnylea 1 year ago
    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 :)

    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 1 year ago
    Thanks Tony. 
    
    This really helped. 
    
    I went ahead and added that to my API Middleware since we bypassing the JWT calls.

    Thanks Tony.

    This really helped.

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

  • nick-jensen 1 year ago
    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
     <td>@if(is_null($apiKey->last_used_at)){{ 'Never Used' }}@else{{ $apiKey->last_used_at }}@endif</td>

    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

  • tnylea 1 year ago
    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!

    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 1 year ago
    They key in the database is `last_used_at`
    
    		@if(is_null($apiKey->last_used_at)){{ 'Never Used' }}@else{{ $apiKey->last_used_at }}@endif
    

    They key in the database is last_used_at

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

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!