[6.x] Move Laravel auth off of User element into CraftUser interface#19009
Open
riasvdv wants to merge 3 commits into
Open
[6.x] Move Laravel auth off of User element into CraftUser interface#19009riasvdv wants to merge 3 commits into
riasvdv wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Hooking up the User element into Laravel's auth service wasn't the best idea. It causes issues when trying to use Craft in combination with other ecosystem packages.
Using the default eloquent provider, and tying authentication methods and services around a
CraftUsersolves this problem and allows developers to use their own User models and authentication systems, as long as they implement the interface.Filament also does this through one or more interfaces depending on the functionality you need.
Our integration surface is still mostly through the User Element which makes the
asElement()method fairly load-bearing, but at least provides some extension point and separates Authentication from Elements.Main Changes
CraftCms\Cms\Auth\UserProvider; thecraftguard now defaults to Laravel’seloquentprovider usingCraftCms\Cms\User\Models\User.CraftCms\Cms\User\Contracts\CraftUserandCraftUserTrait.CraftUser.Auth::craftUser()/auth('craft')->craftUser()andrequest()->craftUser()as the Craft-safe way to access the authenticated user.CraftUserwhere they only need auth/user identity behavior.currentUseris still normalized back to the user element viacurrentUserElement(), so most templates should not change.Integration Impact
Most plugins/integrations probably do not need changes if they use
currentUser, Craft user elements, or normalGate/can()checks.Code should change if it uses Laravel auth directly and assumes
Auth::user()or$request->user()is aCraftCms\Cms\User\Elements\User. Use:Then use contract methods: