Nobody?
Possible to handle a cookie during registration of a new user in Spark?
I am in the process of building an existing-user-refers-a-new-user system where users are rewarded with credits if they refer new users.
- a user has a unique rerferral key that is used to identify the referring user (e.g. John Doe with UUID 123-4567-abc)
- when a referred user (=new user) uses the referral link (e.g.
mysite.com/referral/123-4567-abc) a cookie is set with a validity of 1 month (this part works) - now, on each $request object of the new user the
$request->cookie('referringUserId')is available during validity of cookie (also works)
When the new user registers I need to handle the rewards for both users. In Spark there is a UserRegistered event but that does not contain the $request->cookie() but only the the $user object and I do not want to change vendor code.
Where can I best handle the cookie?
Registration in Spark is handled here:
vendor\laravel\spark\src\Http\Controllers\Auth\RegisterController.php in register-method.
Ok, I now figured it out.
Here are some general insights into how the swap methods work that I found quite useful:
- https://laratips.io/blog/the-spark-philosophy
- https://laratips.io/blog/a-closer-look-at-the-innards-of-spark
The trick ended up being as follows:
- do not try to intercept the
RegisterController.php's register-method but follow the lifecycle of the request up to theCreateUserhandle-method (which also contains the $request) Then you can do something like this:
Spark::swap('CreateUser@handle', function ($request) {
\Clockwork::info($request->cookie('referringUserId'));
return Spark::interact(UserRepository::class.'@create', [$request->all()]);
});
I applied it within the SparkServiceProvider.php
Please or to participate in this conversation.