Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

garethdaine's avatar

Spark User

Guys, could one of you Spark gurus give me some pointers on what to look at to find out how Spark retrieves and exposes the currently logged in user to the frontend API, please?

I'm looking to learn how this is achieved.

0 likes
5 replies
ejdelmonico's avatar

That is inconsistent as far as my opinion goes. However, if you pass :user or :team to your page or component, you can access the Spark user. I am currently using Auth::user()->name and a few others that are available in the object. You can check the object using the vuejs tools plugin for Chrome.

garethdaine's avatar

Yes, thanks. I am already aware of that, though. I'd really like to get some advice on where this is created and then passed to the global Spark object.

I've checked a lot of the bootstrapping and components, but can't seem to see where the user data is actually being retrieved from and how, and how it is then sent to the global object.

ejdelmonico's avatar

I am not sure if I am misreading your question but the data for the user is pulled in at /spark/resources/assets/js/spark.js. In the data and created section. You can see that the Spark user is spark.state.user. I'm sure you already saw all of that and looked at the UserRepository. The instance comes from ManagesModelOptions I think. Also, have a look at ProvidesScriptVariables as well.

garethdaine's avatar

Nope, not that I can tell. I can see the user being set there via the Spark global object, but where and how is the user created in the Spark global object? As this Spark.state.user doesn't really tell me anything about how the data is retrieved or how the user is created.

In terms of the created() method, the only thing related to the user is the this.loadDataForAuthenticatedUser() method, but all that does is retrieve notifications for the authenticated user.

Now, I've figured out that the global Spark object is created in the Spark layout file app.blade.php and that the method scriptVariables() pulls in the data for it via a ProvidesScriptVariables trait. The state is set there using Spark::call(InitialFrontendState::class.'@forUser', [Auth::user()]).

The class InitialFrontendState implements a contract of the same name and the forUser() method retrieves the user data which is then set on the global Spark object via the call in the primary layout file.

ejdelmonico's avatar

Well, I am not that sure but since the SparkUser is extended by the User model, I assume that SparkUser through the Authenticatable Trait.

Please or to participate in this conversation.