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

Laravel Spark multiple errors with User Object #144

Closed
krthush opened this issue Oct 3, 2019 · 5 comments
Closed

Laravel Spark multiple errors with User Object #144

krthush opened this issue Oct 3, 2019 · 5 comments

Comments

@krthush
Copy link

krthush commented Oct 3, 2019

Description

I'm trying to integrate my Laravel Spark project with Auth0, I followed the guide here https://auth0.com/docs/quickstart/webapp/laravel, including the section on Custom User Handling - i.e. implementing a custom user repository succesfully.

Now I run into the problems because Laravel Spark oftern calls functions on the User object (such as hasTeams, etc.)... Now an earlier solution for this was to replace all function calls on the User object with a middle step and create a App\User::current() instead here: #114. This isn't really feasible for my Spark project as I would have to completely take apart Spark to replace all the function calls...

Is there any way of just extending the user provider or override it using the magic of the service container? Would really like to use both Spark + Auth0 without having to dump one of them...

@krthush
Copy link
Author

krthush commented Oct 4, 2019

So I've spent sometime trying to get this to work... I'm currently neck deep in spaghetti code trying to modify the Spark code to accept the Auth0 user object... Really not comfortable with basically breaking apart Spark to make this work.

I feel like all of this would be completely avoided if Auth0 did not replace the laravel "Auth" middleware... so for example "auth->user()" returns an Auth0User model, instead if this was kept as the original "User" model everything would work completely fine Q.Q

Is there a way to keep Auth0 working for the auth->check part, but not everything else?

@krthush
Copy link
Author

krthush commented Oct 4, 2019

So managed to solve this!!! Woop, would have been a pain to rebuild the App from scratch using Laravel and/or not use Auth0 + Spark...

So the method I used was to modify the custom auth0 repository you make in the last step of the guide, instead of returning an new Auth0User, I return the standard User model from $user. The code looks as follows...

app/Repositories/CustomUserRepository.php
@@ -58,6 +58,6 @@ public function getUserByDecodedJWT( $jwt ) {
     */
    public function getUserByUserInfo( $userinfo ) {
        $user = $this->upsertUser( $userinfo['profile'] );
        return $user;
    }
}

@krthush krthush closed this as completed Oct 4, 2019
@lhuria94
Copy link

Hi @krthush,

how come you were able to return the user object.
For me it throws an error:

TypeError
Return value of Auth0\Login\Repository\Auth0UserRepository::getUserByIdentifier() must implement interface Illuminate\Contracts\Auth\Authenticatable or be null, none returned

@krthush
Copy link
Author

krthush commented Aug 25, 2020

Can you provide some additional information? Are you using a custom user repo + modified it like I mentioned?

@gelinger777
Copy link

@krthush @lhuria94 is right. The example wants to be so

    {
        $user = $this->upsertUser( $decodedJwt );
        return new Auth0JWTUser( $user->getAttributes() );
    }

and when I change as you say for Laravel Spark

public function getUserByUserInfo( $userinfo ) {
        $user = $this->upsertUser( $userinfo['profile'] );
        return $user;
    }

I get

Symfony\Component\ErrorHandler\Error\FatalError
Declaration of App\Repositories\CustomUserRepository::getUserByUserInfo(array $userinfo) must be compatible with Auth0\Login\Repository\Auth0UserRepository::getUserByUserInfo(array $userInfo): Illuminate\Contracts\Auth\Authenticatable

I wonder how you have setup config/auth.php too

@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

No branches or pull requests

3 participants