JeffBeltran / sanctum-tokens

Simple Nova Plugin to generate a Laravel Sanctum Personal Access Token
https://novapackages.com/packages/jeffbeltran/sanctum-tokens
51 stars 25 forks source link

Tokens list not displayed #85

Open juanr-yes opened 1 year ago

juanr-yes commented 1 year ago

Hi! thanks in advance for this cool package and for the hard work.

I've trying to use it but seems like its having some issues:

Once i'm on the User resource and after inserting SanctumTokens::make()

When i'm on the detail page and after creating a token, nothing gets displayed while i get a js error on the console: TypeError: Cannot read properties of undefined (reading 'length') even when the token has been successfully created.

I'm on Laravel 8, Laravel Nova 4 and latest sanctum-tokens.

Looking forward to hear from you! Thanks again.

image

JeffBeltran commented 1 year ago

if i get time i'll try and duplicate this but typically i have found is that if there are issues it is normally related to the sanctum configuration. if you can get me more details it might help me point you in the right direction

juanr-yes commented 1 year ago

Hi Jeff! Thanks for answering me.

I believe i have a very straight-forward sanctum configuration but i'll gladly share whatever you think might guide us into a solution!

Have a great weekend

kostamilorava commented 1 year ago

Hey @juanr-yes,

Too late for party, but if anyone else will have this issue, check your network tab in developer tools - you should have something like: /nova-vendor/sanctum-tokens/tokens/users/ID_OF_USER

Check what is being returned there (I had 302 redirect), It should return JSON. If in routes configuration you have something like:

Route::get('{any}', function () {
    return redirect()->route('home');
})->where('any', '.*');

It overrides routes that are needed for this package (at least it was case for me).

juanr-yes commented 1 year ago

hi @kostamilorava , hope you are doing great. Thanks for your input, although im not sure we are talking about the same issues; i am looking at the network tab and the "sanctum-tokens" request and got a 200 response and, as stated in the beginning, the typeerror continues

kostamilorava commented 1 year ago

hi @kostamilorava , hope you are doing great. Thanks for your input, although im not sure we are talking about the same issues; i am looking at the network tab and the "sanctum-tokens" request and got a 200 response and, as stated in the beginning, the typeerror continues

Alright, on fresh installed project, it seems to work properly. I checked the line where Vue throws that error, and I am pretty sure we are talking about same error, but just reason is different. Maybe you have some middleware that changes responses or wraps them.

Vue is not able to iterate through tokens and produces that error. Do you have same structure as mine? Here is correct response (copied and pasted whole response) that works and may help you diagnose your problem:

{
    "id": 2,
    "name": "IOT Device",
    "email": "iot@example.com",
    "email_verified_at": null,
    "created_at": "2023-08-20T20:37:57.000000Z",
    "updated_at": "2023-08-20T20:37:57.000000Z",
    "tokens": [
        {
            "id": 1,
            "tokenable_type": "App\\Models\\User",
            "tokenable_id": 2,
            "name": "Name of token",
            "abilities": [
                "ability-name"
            ],
            "last_used_at": "2023-08-22T10:11:08.000000Z",
            "expires_at": null,
            "created_at": "2023-08-21T18:36:33.000000Z",
            "updated_at": "2023-08-22T10:11:08.000000Z"
        }
    ]
}
juanr-yes commented 1 year ago

hey @kostamilorava , thanks a lot!! you're completely right about beign related to Vue not beign able to iterate the "tokens" property, which i whas hiding by not declaring it on $visible while on the model. Thanks to the repo owner as well.

stevelacey commented 11 months ago

@JeffBeltran I ran into this issue, the reason was because I have a $visible = [...] definition in my user model that doesn't include tokens so maybe it'd be a good idea for you to call makeVisible('tokens') in addition to with('tokens') from your controller

For now I've put this in my User::booted 🤢

static::retrieved(function ($user) {
    if (Request::is('nova-vendor/sanctum-tokens/*')) {
        $user->makeVisible('tokens');
    }
});