diff --git a/app/Livewire/Auth/Login.php b/app/Livewire/Auth/Login.php index 08f183c..9084bf6 100644 --- a/app/Livewire/Auth/Login.php +++ b/app/Livewire/Auth/Login.php @@ -21,53 +21,88 @@ class Login extends Component #[Validate('required', attribute: 'kata sandi')] public string $password = ''; - public function mount() + public function mount(): void { - if (request()->has('message')) { - $this->toast(request('message')); - $this->js(" - const url = new URL(window.location); - url.search = ''; - window.history.replaceState({}, '', url); - "); + if (! request()->has('message')) { + return; } + + $this->toast(request('message')); + + $this->js(<<<'JS' + const url = new URL(window.location); + url.search = ''; + window.history.replaceState({}, '', url); + JS); } - public function auth() + public function auth(): void { $this->validate(); - $identifier = filter_var($this->login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; - - if (! auth()->attempt( - [$identifier => $this->login, 'password' => $this->password], - )) { - $this->toast('Nama pengguna, email atau kata sandi salah.', 'Gagal', 'danger'); + if (! $this->attemptLogin()) { + $this->toast('Oops! Nama pengguna, email, atau kata sandi tidak sesuai.', 'Gagal', 'danger'); return; } - if (auth()->user()->status !== UserStatus::ACTIVE) { + $user = auth()->user(); + + if (! $this->userIsActive($user)) { auth()->logout(); + $this->toast( + 'Akun Anda belum aktif. Mohon periksa email untuk mengaktifkan akun, dan hubungi kami jika memerlukan bantuan lebih lanjut.', + 'Gagal', + 'danger' + ); + return; } - $this->toast('Selamat datang, '.auth()->user()?->employee?->full_name.'. Semoga harimu menyenangkan.'); - session()->regenerate(); - $roles = auth()->user()->roles->pluck('name')->toArray(); + $this->toast('Hai, '.($user?->employee?->full_name ?? $user->name).'! Semoga harimu menyenangkan 😊'); + $this->redirectByRole($user->roles->pluck('name')->toArray()); + } + + protected function attemptLogin(): bool + { + $field = filter_var($this->login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; + + return auth()->attempt([ + $field => $this->login, + 'password' => $this->password, + ]); + } + + protected function userIsActive($user): bool + { + return $user?->status === UserStatus::ACTIVE; + } + + protected function redirectByRole(array $roles): void + { if (array_intersect($roles, ['Developer', 'Owner', 'Leader', 'Admin'])) { $this->redirectIntended('/studio/dashboard/overview', navigate: true); - } elseif (in_array('Partner', $roles)) { - $this->redirectIntended('/partner/overview', navigate: true); - } elseif (in_array('Customer', $roles)) { - $this->redirectIntended('/member/overview', navigate: true); - } else { - $this->redirectIntended('/login', navigate: true); + + return; } + + if (in_array('Partner', $roles, true)) { + $this->redirectIntended('/partner/overview', navigate: true); + + return; + } + + if (in_array('Customer', $roles, true)) { + $this->redirectIntended('/member/overview', navigate: true); + + return; + } + + $this->redirectIntended('/login', navigate: true); } public function render() diff --git a/resources/views/livewire/auth/login.blade.php b/resources/views/livewire/auth/login.blade.php index 1444161..c2db9ab 100644 --- a/resources/views/livewire/auth/login.blade.php +++ b/resources/views/livewire/auth/login.blade.php @@ -18,12 +18,16 @@ class="size-8 rounded shrink-0 bg-accent text-accent-foreground flex items-cente
- + + Nama Pengguna atau Email * + + +
- Kata Sandi + Kata Sandi * Lupa kata