@andersb I had a need to create a sort of default api token for each user and this is how I've done it... Would like some other peoples opinion about a better way to do this.
In your listener class:
use Laravel\Spark\Repositories\TokenRepository;
use Laravel\Spark\Spark;
use Laravel\Spark\Events\Auth\UserRegistered;
Inject the TokenRepository
private $tokenRepository;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(TokenRepository $tokenRepository)
{
//
$this->tokenRepository = $tokenRepository;
}
Then create that token...
public function handle(UserRegistered $event)
{
$this->tokenRepository->createToken($event->user, 'default-mobile-token', [
'abilities' => [
'read-servers',
'create-servers',
'delete-servers',
]
]);
}
Also, this solution is very tightly coupled to registering a user.. what if they user deletes the key via settings, or we need to re-create it, or the team is switched or need to implement a guard on it. I think this action under handle should really be delegated to a class that can itself implement the token repository and handle a full suite of checks and balances.