157 lines
4.9 KiB
PHP
157 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use App\Services\ApiService;
|
|
use App\Services\CookieService;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Login extends Component
|
|
{
|
|
public $username = '';
|
|
public $password = '';
|
|
public $isSubmitting = false;
|
|
public $userVerified = false;
|
|
public $isModalVisible = false;
|
|
public $forgotUsername = false;
|
|
public $userEmail = '';
|
|
public $userLogo = '';
|
|
public $mounted = false;
|
|
public $role = null;
|
|
public $errors = [];
|
|
|
|
protected $apiService;
|
|
protected $cookieService;
|
|
|
|
public function boot(ApiService $apiService, CookieService $cookieService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
$this->cookieService = $cookieService;
|
|
}
|
|
|
|
public function mount()
|
|
{
|
|
if (Session::get('isAuthenticated')) {
|
|
return redirect('/my-profile'); // Adjust based on role if needed
|
|
}
|
|
|
|
try {
|
|
$contactResponse = $this->apiService->get('systemPreference/contact_details');
|
|
$logoResponse = $this->apiService->get('systemPreference/logo');
|
|
|
|
$this->userEmail = $contactResponse['data']['value'] ?? '';
|
|
$this->userLogo = $logoResponse['data']['value'] ?? '';
|
|
$this->mounted = true;
|
|
} catch (\Exception $e) {
|
|
Log::error('Failed to load system preferences', ['error' => $e->getMessage(), 'source' => 'LoginComponent']);
|
|
}
|
|
}
|
|
|
|
public function checkUser()
|
|
{
|
|
$this->isSubmitting = true;
|
|
$this->errors = [];
|
|
|
|
try {
|
|
$this->validate([
|
|
'username' => ['required', 'regex:/^[A-Za-z0-9_@. ]+$/'],
|
|
], [
|
|
'username.required' => 'Username is required!',
|
|
'username.regex' => 'Invalid username.',
|
|
]);
|
|
|
|
$response = $this->apiService->post('/login_username', ['username' => $this->username]);
|
|
$this->userVerified = $response['data']['is_verified'] ?? false;
|
|
$this->role = $response['data']['role'] ?? null;
|
|
} catch (\Illuminate\Validation\ValidationException $e) {
|
|
$this->errors = $e->errors();
|
|
} catch (\Exception $e) {
|
|
$this->errors['username'] = [$e->getMessage()];
|
|
Log::error('Username check failed', ['error' => $e->getMessage(), 'source' => 'LoginComponent']);
|
|
} finally {
|
|
$this->isSubmitting = false;
|
|
}
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
$this->isSubmitting = true;
|
|
$this->errors = [];
|
|
|
|
try {
|
|
$this->validate([
|
|
'password' => ['required'],
|
|
], [
|
|
'password.required' => 'Password is required!',
|
|
]);
|
|
|
|
$response = $this->apiService->post('/login_password', [
|
|
'username' => $this->username,
|
|
'password' => $this->password,
|
|
]);
|
|
|
|
if (isset($response['data']['prompt_password']) && $response['data']['prompt_password']) {
|
|
return redirect()->route('change-password', [
|
|
'username' => $this->username,
|
|
'admin_uuid' => $response['data']['admin_uuid'],
|
|
'password' => $this->password,
|
|
]);
|
|
}
|
|
|
|
if (isset($response['data']['token'])) {
|
|
$token = $response['data']['token'];
|
|
$this->cookieService->setCookie(['token' => $token], 'TOKEN');
|
|
Session::put('isAuthenticated', true);
|
|
|
|
$profileResponse = $this->apiService->post('adminProfile');
|
|
$userInfo = $profileResponse['data'];
|
|
Session::put('userInfo', $userInfo);
|
|
|
|
Log::info('Login successful', ['source' => 'LoginComponent']);
|
|
return redirect('/my-profile'); // Adjust based on role if needed
|
|
}
|
|
} catch (\Illuminate\Validation\ValidationException $e) {
|
|
$this->errors = $e->errors();
|
|
} catch (\Exception $e) {
|
|
$this->errors['password'] = [$e->getMessage()];
|
|
Log::error('Login failed', ['error' => $e->getMessage(), 'source' => 'LoginComponent']);
|
|
} finally {
|
|
$this->isSubmitting = false;
|
|
}
|
|
}
|
|
|
|
public function showModalForgotUsername()
|
|
{
|
|
$this->isModalVisible = true;
|
|
$this->forgotUsername = true;
|
|
}
|
|
|
|
public function showModalChangePassword()
|
|
{
|
|
$this->isModalVisible = true;
|
|
$this->forgotUsername = false;
|
|
}
|
|
|
|
public function hideModal()
|
|
{
|
|
$this->isModalVisible = false;
|
|
}
|
|
|
|
public function backToLogin()
|
|
{
|
|
$this->username = '';
|
|
$this->password = '';
|
|
$this->userVerified = false;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
if (!$this->mounted) {
|
|
return null;
|
|
}
|
|
|
|
return view('livewire.login')->layout('layouts.app', ['title' => 'Login Page']);
|
|
}
|
|
} |