Hey this is the way I did it.
First, when you use the CanJoinTeam on your user class, the team registration is hard coded in the namespace Laravel\Spark\Interactions\Auth\CreateUser class
class CreateUser implements Contract
{
/**
* {@inheritdoc}
*/
public function validator($request)
{
$validator = $this->baseValidator($request);
// here
$validator->sometimes('team', 'required|max:255', function ($input) {
return Spark::usesTeams() && ! isset($input['invitation']);
});
return $validator;
}
To disable this, I extended the class and overwrite this method like this :
use Laravel\Spark\Interactions\Auth\CreateUser;
class CreateUserExtended extends CreateUser
{
/**
* Remove the team validation force registration
*/
public function validator($request)
{
return $this->baseValidator($request);
}
}
Then I used the Spark::swap to replace the original behaviour with my custom method in SparkServiceProvider
// don't force team registration on user creation
Spark::swap('CreateUser@validator', function ($request) {
$user = new CreateUserExtended();
return $user->validator($request);
});
This way, if the baseValidator method is updated in the futur, you wont loose the changes.
After that, you will passed the Validation part, but your user will have an empty team assigned to it, if you want to disabled this, you have to apply the same logic to overwrite the handle method on the namespace Laravel\Spark\Interactions\Auth\Register method.
Basically because, like in the validator, the condition if the user uses teams is hard coded
/**
* Create the user for the new registration.
*
* @param RegisterRequest $request
* @return \Illuminate\Contracts\Auth\Authenticatable
*/
protected function createUser(RegisterRequest $request)
{
$user = Spark::interact(CreateUserContract::class, [$request]);
// here
if (Spark::usesTeams()) {
$this->configureTeamForNewUser($request, $user);
}
return $user;
}
Since the logic is similar, I'll just paste the code.
use Laravel\Spark\Spark;
use Laravel\Spark\Interactions\Auth\Register;
use Laravel\Spark\Contracts\Http\Requests\Auth\RegisterRequest;
use Laravel\Spark\Contracts\Interactions\Auth\CreateUser as CreateUserContract;
class RegisterExtended extends Register
{
/**
* Create the user for the new registration.
*
* @param RegisterRequest $request
* @return \Illuminate\Contracts\Auth\Authenticatable
*/
public function createUser(RegisterRequest $request)
{
return Spark::interact(CreateUserContract::class, [$request]);
}
/**
* Subscribe the given user to a subscription plan.
* Parent method visibility is protected
* You need to make it public to call it in SparkServiceProvider
*
* @param RegisterRequest $request
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return \Illuminate\Contracts\Auth\Authenticatable
*/
public function subscribe($request, $user)
{
return parent::subscribe($request, $user);
}
}
class SparkServiceProvider extends ServiceProvider {
public function register() {
parent::register();
// don't force team registration on user creation
Spark::swap('Register@handle', function(RegisterRequest $request) {
return DB::transaction(function () use ($request) {
$register = new RegisterExtended();
return $register->subscribe($request, $register->createUser($request));
});
});
Spark::swap('CreateUser@validator', function ($request) {
$user = new CreateUserExtended();
return $user->validator($request);
});
}
After that, disable the hasTeam middleware like you said and remove/comment the team input field in the register-common-form.blade.php and you should be good to go.
Hope it helps you, I personally prefer this option, it feels more natural to me.