Customize registration logic

Hi, I want to change the account registration logic to provide a verification code when registering. I have the field itself added and the handling of this field done, but the problem arises when it wants to verify this code. If the user enters a code that is wrong, or the code is not assigned to his username, at that point it rejects the registration and does not make the account. Otherwise, the account should be created. I have such a code:
protected function handleRegistration(array $data): Model {
$username = $data['username'];
$verifyCode = $data['verify_code'];
$password = Hash::make($data['password']);

// Bool
$verifyResult = $this->checkVerifyCode($verifyCode, $username);

if ($verifyResult) {

$user = User::query()
->create([
'uuid' => $this->getUserUUID($username),
'used_verification_code' => $verifyCode,
'username' => $username,
'password' => $password,
]);

$verifyCode = VerifyCode::query()->where('code', $verifyCode)->first();
$verifyCode->delete();

return $user;

}
}
protected function handleRegistration(array $data): Model {
$username = $data['username'];
$verifyCode = $data['verify_code'];
$password = Hash::make($data['password']);

// Bool
$verifyResult = $this->checkVerifyCode($verifyCode, $username);

if ($verifyResult) {

$user = User::query()
->create([
'uuid' => $this->getUserUUID($username),
'used_verification_code' => $verifyCode,
'username' => $username,
'password' => $password,
]);

$verifyCode = VerifyCode::query()->where('code', $verifyCode)->first();
$verifyCode->delete();

return $user;

}
}
But this method requires returning the User model. What if the verification code validation fails? What should I do?
3 Replies
Matthew
Matthew2w ago
Make it so whatever is calling your function can deal with something other than the $user model being returned.
toeknee
toeknee2w ago
So for that you should use a middleware to redirect them to confirm the code until the code has been validated and the session access cleared.
Dennis Koch
Dennis Koch2w ago
What's the reason of that code during registration? Why don't you just use ->emailVerification() feature on the panel?

Did you find this page helpful?