refactor(login): memecah fungsi login menjadi beberapa bagian untuk clean code
- menyesuaikan pesan toast - menambahkan tanda required di form label
This commit is contained in:
parent
67c060476d
commit
ebf6cd78ea
@ -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()
|
||||
|
||||
@ -18,12 +18,16 @@ class="size-8 rounded shrink-0 bg-accent text-accent-foreground flex items-cente
|
||||
</flux:text>
|
||||
|
||||
<div class="flex flex-col gap-6">
|
||||
<flux:input wire:model="login" label="Nama pengguna atau email" type="text"
|
||||
placeholder="johndoe / info.pangestuyoga@gmail.com" autocomplete="off" />
|
||||
<flux:field>
|
||||
<flux:label>Nama Pengguna atau Email <span class="text-red-500 ms-1">*</span></flux:label>
|
||||
<flux:input wire:model="login" type="text" placeholder="johndoe / info.pangestuyoga@gmail.com"
|
||||
autocomplete="off" />
|
||||
<flux:error name="login" />
|
||||
</flux:field>
|
||||
|
||||
<flux:field>
|
||||
<div class="mb-3 flex justify-between">
|
||||
<flux:label>Kata Sandi</flux:label>
|
||||
<flux:label>Kata Sandi <span class="text-red-500 ms-1">*</span></flux:label>
|
||||
|
||||
<flux:link href="{{ route('password.request') }}" variant="subtle" class="text-sm"
|
||||
wire:navigate.hover>Lupa kata
|
||||
|
||||
Loading…
Reference in New Issue
Block a user