+
Skip to content

Authenicated user is null in singleton #814

@John5

Description

@John5

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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载