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

Authenicated user is null in singleton #814

Open
John5 opened this issue Apr 24, 2024 · 0 comments
Open

Authenicated user is null in singleton #814

John5 opened this issue Apr 24, 2024 · 0 comments

Comments

@John5
Copy link

John5 commented Apr 24, 2024

I am trying to create a /Me singleton endpoint which displays some information about the user who is currently authenticated. I have already added authentication middleware. When I am not authenticated I get an error message like expected. When I am authenticated I get a response. However, when I try to access \Auth::user() from within the LodataServiceProvider class it always returns null while I would expect an instance of the User model. The documentation for singletons is very limited, an example on how to show information about the current user would be a good addition there, as it is the most common use for a singleton.

Stripped down version of my code that always returns null values for all properties:

class LodataServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Lodata::add($this->meSingleton());
    }

    protected function meSingleton(): Singleton
    {
        $user = \Auth::user();
        $entity = new EntityType('Users');
        $entity->addDeclaredProperty(User::EMAIL, Type::string());
        $entity->addDeclaredProperty(User::FIRST_NAME, Type::string());
        $entity->addDeclaredProperty(User::LAST_NAME, Type::string());
        $singleton = new Singleton('Me', $entity);
        if ($user instanceof User) {
            $singleton[User::EMAIL] = $user->email;
            $singleton[User::FIRST_NAME] = $user->getFirstName();
            $singleton[User::LAST_NAME] = $user->getLastName();
        }
        return $singleton;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant