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

garethdaine's avatar

Adding Properties to The Spark User

Hi Folks,

Can someone give me some advice for adding properties from the server side to the Spark user, please?

We have an Intercom integration with our app, and we're setting up secure messaging, which requires us to send a hash to the JS. My JS for Intercom is contained within a 'defer' Vue component.

So I'd like to be able to generate the hash, with the following on the server side in PHP:

hash_hmac('sha256', $user->email, 'secret')

Then, attach this hash to a Spark user object property, so it's accessible in the JS using:

Spark.state.user.intercom_hash

Would appreciate any advice. Thanks.

0 likes
4 replies
garethdaine's avatar

Sorted it folks.

Just created a View Composer that created the hash if a user is logged in.

Then in 'spark::layouts.app' I use the Spark::scriptVariables() method to add the hash and then access it in Vue using:

Spark.intercom_hash

Would be good to assign it to: Spark.state.user.intercom_hash

EventFellows's avatar

You can Swap the Spark Method for current user in your SparkServiceProvider like this (see the docs for deails on swap approach

        Spark::swap('UserRepository@current', function () {

            if (Auth::check()) {
                $currentUser = Spark::interact(UserRepository::class.'@find', [Auth::id()]);
                $currentUser->intercom_hash = 'whatever you want here';

                
                return $currentUser;
            }
        });

that get's picked up in 'Spark.state.user' then as Spark.state.user.intercom_hash.

2 likes
Cronix's avatar
Cronix
Best Answer
Level 67

In the user model, you can add:

protected $appends = ['IntercomHash'];

Then create an accessor:

public function getIntercomHashAttribute()
{
        return hash_hmac('sha256', $this->email, 'secret');
}

Then you can access Spark.state.user.IntercomHash without doing anything else

4 likes

Please or to participate in this conversation.