Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UserProvider API #264

Merged
merged 1 commit into from
Mar 9, 2022
Merged

Update UserProvider API #264

merged 1 commit into from
Mar 9, 2022

Conversation

evansims
Copy link
Member

@evansims evansims commented Mar 8, 2022

Changes

This PR includes changes to the UserProvider API to ease host applications in implementing their own User model types. The new default user model provided by the library only includes the normalized user data returned with the authenticated session or provided access token. The StateInstance now keeps track of associated tokens, scopes and expiration states.

To implement your own UserProvider:

  1. Update your Laravel 8+ application's config/auth.php file. Within the Auth0 provider, assign a custom repository value pointing to your user provider class. For example:
    'providers' => [
        //...

        'auth0' => [
            'driver' => 'auth0',
            'repository' => App\Auth\CustomUserRepository::class
        ],
    ],
  1. Your application's custom user provider should look something like this:
<?php

declare(strict_types=1);

namespace App\Auth;

class CustomUserRepository implements \Auth0\Laravel\Contract\Auth\User\Repository
{
    /**
     * @inheritdoc
     */
    public function fromSession(
        array $user
    ): ?\Illuminate\Contracts\Auth\Authenticatable {
        return new \App\Models\User([
            'id' => $user['sub'] ?? $user['user_id'] ?? null,
            'name' => $user['name'],
            'email' => $user['email']
        ]);
    }

    /**
     * @inheritdoc
     */
    public function fromAccessToken(
        array $user
    ): ?\Illuminate\Contracts\Auth\Authenticatable {
        // Simliar to above. Used for stateless application types.
        return null;
    }
}
  1. Your application's user model must implement Illuminate\Contracts\Auth\Authenticatable to be compatible with Laravel's Guard API, and this library. It must also implement either Auth0\Laravel\Contract\Model\Stateful\User or Auth0\Laravel\Contract\Model\Stateless\User depending on your application use case. For example:
<?php

declare(strict_types=1);

namespace App\Models;

use Auth0\Laravel\Contract\Model\Stateful\User as StatefulUser;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;

class User extends \Illuminate\Database\Eloquent\Model implements StatefulUser, 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',
        'name',
        'email',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [];
}

Note: As state request data has been moved out of the returned user model, you should now query the StateInstance from within your application for this data. For example:

$state = app()->make(\Auth0\Laravel\StateInstance::class);
echo $state->getAccessToken();

@evansims evansims marked this pull request as ready for review March 8, 2022 17:15
@evansims evansims requested a review from a team as a code owner March 8, 2022 17:16
@evansims evansims merged commit 58ce97f into main Mar 9, 2022
@evansims evansims deleted the v7/updated-user-provider-api branch March 9, 2022 16:19
@evansims evansims mentioned this pull request Mar 9, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant