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 public function register(): void
{ {
$this->form->auth(); if ($this->form->auth()) {
$this->sendRegistrationSuccessToast();
$this->sendRegistrationSuccessToast(); $this->notifyPrivilegedUsersAboutNewMember();
$this->notifyPrivilegedUsersAboutNewMember(); $this->redirectAfterRegistration();
$this->redirectAfterRegistration(); } else {
$this->toast('Terjadi kesalahan saat mendaftarkan akun. Silakan hubungi admin atau coba beberapa saat lagi.', 'Kesalahan', 'danger');
}
} }
public function render(): View public function render(): View

View File

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