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