You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}
The text was updated successfully, but these errors were encountered:
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:
The text was updated successfully, but these errors were encountered: