auth0 / laravel-auth0

Laravel SDK for Auth0 Authentication and Management APIs.
MIT License
246 stars 134 forks source link

Stateless examples #356

Closed vlourme closed 1 year ago

vlourme commented 1 year ago

SDK Version

7.5

PHP Version

PHP 8.2

Composer Version

2.x

What happened?

I'm trying to setup a backend CRUD in stateless but the CustomUserRepository does not work correctly.

I have this:

class CustomUserRepository implements \Auth0\Laravel\Contract\Auth\User\Repository
{
    public function fromSession(
        array $user
    ): ?Authenticatable {
        return null;
    }

    public function fromAccessToken(
        array $user
    ): ?Authenticatable {
        return User::firstOrCreate([
            'id' => $user['sub'],
        ]);

       // Note: Should we create the user here if it does not exists or 
       // just return an unsaved model like in the EXAMPLES.md?
    }
}

Then, I want to have a UserController that will allow the user to see and manage its profile, like this:

class UserController extends Controller
{
    public function index(Request $request)
    {
        return $request->user();
    }

    // [...] Other API resources [...]
}

When I make a call to my API (e.g.: /api/v1/users/), I get this:

{
    "id": 0,
    "name": null,
    "avatar": null,
    "created_at": "2023-04-05T14:20:28.000000Z",
    "updated_at": "2023-04-05T14:20:28.000000Z"
}

But in the database, the ID is correctly saved. This also happen if I use Auth::id().

Here is my User model:

class User extends \Illuminate\Database\Eloquent\Model implements StatelessUser, AuthenticatableUser
{
    use HasFactory, Notifiable, Authenticatable;

    /**
     * The primary identifier for the user.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'id', // this is a string and a primary key in the migration
        'name', // nullable actually in the migration
        'avatar' // nullable actually in the migration
    ];
}

How can we reproduce this issue?

I can't tell if this is a real issue, I'm quite a beginner with Auth0. Unfortunately, I think this repository (and the official website) lacks of stateless examples.

Thanks for helping :)

Additional context

App context: Stateless application using Laravel 10.x and frontend is VueJS 3.x (using auth0/auth0-vue). The authentication is done via universal login and an access_token is sent in the header for each API request.

alinmiron commented 1 year ago

Hey. I had an binding error regarding the CustomUserRepository. A simple 'composer update' did the trick, It seems there was a bug related that was fixed yesterday https://github.com/auth0/laravel-auth0/commits/main. Ensure you have at least the version 7.5.1 I lost few hours myself because of this bug.

also, double check the return User::firstOrCreate( line. You have an extra pair of []. https://laravel-news.com/firstornew-firstorcreate-firstor-updateorcreate

Good luck!

vlourme commented 1 year ago

Well, I finally found out. Laravel consider a primary key as an integer by default, so I had to add this to the Model:

protected $keyType = 'string';

also, double check the return User::firstOrCreate( line. You have an extra pair of []. https://laravel-news.com/firstornew-firstorcreate-firstor-updateorcreate

Just for the information, this is not mandatory because Laravel merges the attributes and values here: https://github.com/laravel/framework/blob/9.x/src/Illuminate/Database/Eloquent/Builder.php#L559. But thank you for trying to help me, I appreciate 😃

Before closing this issue, I would still be interested in two things:

evansims commented 1 year ago

Hi @vlourme 👋

Thanks for creating this!

Hope that helps!