passport: Laravel 7.5.2 (Passport Api) + Vuejs Error : Undefined index: aud , \vendor\laravel\passport\src\Guards\TokenGuard.php

  • Passport Version: 9.0
  • Laravel Version: 7.5.2
  • PHP Version: 7.4.0
  • Database Driver & Version:
  • Database client version: libmysql - mysqlnd 7.4.0

Description:

I am using Laravel 7.5.2 with Vuejs. I am using passport for api authentication. I am getting the following error when sending ajax request to api

{
"message": "Undefined index: aud",
"exception": "ErrorException",
"file": "E:\\laravel\\vendor\\laravel\\passport\\src\\Guards\\TokenGuard.php",
"line": 140,
"trace": [
    {
...
}
]

Steps To Reproduce:

I have followed passport installation instruction on laravel website.

  • Has run composer require laravel/passport, php artisan migrate and php artisan passport:install
  • Has added HasApiTokens in user model
  • Has added Passport::routes() in AuthServiceProvider
  • Has added \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class in App\Http\Kernel web middlewareGroups
  • Has changed Api driver to passport in config/auth.php

The cookie named “laravel_token” is getting generated and sent with ajax request as I can see in developer tool.

In my controller I have included api middleware as

public function __construct(){
    $this->middleware('auth:api');
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 26 (15 by maintainers)

Most upvoted comments

Released v9.0.1 which should fix this.

In my instance I’m using React.

  1. laravel new test --auth
  2. cd test
  3. composer require laravel/passport
  4. php artisan migrate
  5. php artisan passport:install
  6. Add HasApiTokens to User model
  7. Change api guard to passport in config/auth.php
  8. Add Passport::routes() to AuthServiceProvider
  9. Add \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class to web middleware group in app/Http/Kernel.php
  10. php artisan ui react --auth
  11. In home.blade.php add <div id="example"></div>
  12. Replace resources/js/components/Example.js with the following code:
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';

function Example() {
    const load = async () => {
        const data = await axios.get(`api/user`);

        console.log(data);
    }

    useEffect(() => {
        load();
    }, []);

    return null;
}

export default Example;

if (document.getElementById('example')) {
    ReactDOM.render(<Example />, document.getElementById('example'));
}
  1. npm install && npm run dev
  2. Open app in browser, create account, look in network requests and you’ll see a 500 error
{
    "message": "Undefined index: aud",
    "exception": "ErrorException",
    "file": "/Users/joe/Code/Web/test/vendor/laravel/passport/src/Guards/TokenGuard.php",
    "line": 140,
    "trace": [
        {
            "file": "/Users/joe/Code/Web/test/vendor/laravel/passport/src/Guard
...

Thanks to everyone who reported this!

Thanks everyone, the fix works nicely 👍

I suspect it might be due to the removal of old php-jwt versions in this merged PR? #1236

Reverting to 8.x branch resolved the issue for me.

composer require laravel/passport:^8.0

I think I might have found the issue.

Looks like /vendor/laravel/passport/src/ApiTokenCookieFactory.php:77 sets the index sub into the JWT token

But when decoding the cookie and trying to find the corresponding user \Laravel\Passport\Guards\TokenGuard::$clients tries to use the index aud to find the user.

However, the indexes have been like that for a LONG time now, so I’m not sure why this is suddenly a problem?