feat: Implement robust error handling and logging for user registration, displaying an error toast on failure.

This commit is contained in:
Yoga Pangestu 2026-02-28 15:22:42 +07:00
parent 7df0e16914
commit a12e516b2d
2 changed files with 65 additions and 52 deletions

View File

@ -72,11 +72,13 @@ public function updated(string $propertyName, mixed $value): void
public function register(): void
{
$this->form->auth();
$this->sendRegistrationSuccessToast();
$this->notifyPrivilegedUsersAboutNewMember();
$this->redirectAfterRegistration();
if ($this->form->auth()) {
$this->sendRegistrationSuccessToast();
$this->notifyPrivilegedUsersAboutNewMember();
$this->redirectAfterRegistration();
} else {
$this->toast('Terjadi kesalahan saat mendaftarkan akun. Silakan hubungi admin atau coba beberapa saat lagi.', 'Kesalahan', 'danger');
}
}
public function render(): View

View File

@ -16,6 +16,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Livewire\Form;
@ -92,7 +93,7 @@ public function validationAttributes(): array
];
}
public function auth(): string
public function auth(): ?string
{
$this->validateAll();
@ -101,56 +102,66 @@ public function auth(): string
$referralCode = ReferralCode::where('code', $this->referral_code)->first();
$user = null;
DB::transaction(function () use ($tier, $maxUser, $referralCode, &$user): void {
$user = User::create([
'email' => $this->email,
'username' => $this->username,
'password' => Hash::make($this->password),
'status' => UserStatus::ACTIVE,
]);
Customer::create([
'user_id' => $user->id,
'name' => $this->name,
'phone_number' => $this->phone_number,
'gender' => $this->gender,
]);
Membership::create([
'user_id' => $user->id,
'tier_id' => $tier->id,
'reward_points' => 10,
]);
ReferralCode::create([
'user_id' => $user->id,
'code' => generateReferralCode($this->username, $maxUser),
]);
if ($this->referral_code) {
ReferralUsage::create([
'user_id' => $user->id,
'referral_code_id' => $referralCode->id,
try {
DB::transaction(function () use ($tier, $maxUser, $referralCode, &$user): void {
$user = User::create([
'email' => $this->email,
'username' => $this->username,
'password' => Hash::make($this->password),
'status' => UserStatus::ACTIVE,
]);
$referralCode->increment('uses_count');
}
Customer::create([
'user_id' => $user->id,
'name' => $this->name,
'phone_number' => $this->phone_number,
'gender' => $this->gender,
]);
PointRecord::create([
'user_id' => $user->id,
'description' => 'Pendaftaran',
'change' => 10,
'is_addition' => true,
'type' => PointRecordType::TIER,
Membership::create([
'user_id' => $user->id,
'tier_id' => $tier->id,
'reward_points' => 10,
]);
ReferralCode::create([
'user_id' => $user->id,
'code' => generateReferralCode($this->username, $maxUser),
]);
if ($this->referral_code) {
ReferralUsage::create([
'user_id' => $user->id,
'referral_code_id' => $referralCode->id,
]);
$referralCode->increment('uses_count');
}
PointRecord::create([
'user_id' => $user->id,
'description' => 'Pendaftaran',
'change' => 10,
'is_addition' => true,
'type' => PointRecordType::TIER,
]);
$user->assignRole('Customer');
Auth::login($user);
$user->sendEmailVerificationNotification();
});
return $this->name;
} catch (\Exception $e) {
Log::error('Registration Error: '.$e->getMessage(), [
'trace' => $e->getTraceAsString(),
'email' => $this->email,
'username' => $this->username,
]);
$user->assignRole('Customer');
Auth::login($user);
$user->sendEmailVerificationNotification();
});
return $this->name;
return null;
}
}
}