diff --git a/app/Livewire/LoginForm.php b/app/Livewire/LoginForm.php index 3954753..70b9d1a 100644 --- a/app/Livewire/LoginForm.php +++ b/app/Livewire/LoginForm.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use console; use Livewire\Component; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Session; @@ -9,7 +10,7 @@ use Illuminate\Support\Facades\Session; class LoginForm extends Component { public $username, $password; - + public function submit() { $this->validate([ @@ -18,34 +19,29 @@ class LoginForm extends Component ]); try { - $response = Http::post(env('BACKEND_API_URL') . '/api/login_password', [ + $response = Http::post(config('services.backend_api.url') . '/api/cms/login_password', [ 'username' => $this->username, 'password' => $this->password, ]); - + + $json = $response->json(); + if ($response->successful()) { - $json = $response->json(); - if ($json['code'] === 200) { - // Store UUID in session if needed - Session::put('admin_uuid', $json['data']['admin_uuid'] ?? null); - - // Redirect if user must change password - if (!empty($json['data']['prompt_password'])) { - return redirect('/api/login_changePassword'); - } - - // Redirect to profile after successful login + Session::put('user', $json['data']['user'] ?? null); + return redirect('/main/profile'); } else { $this->addError('username', $json['message'] ?? 'Login failed.'); } } else { - $this->addError('username', 'Login request failed. Please try again.'); + $message = $json['message'] ?? 'Login request failed. Please try again.'; + $this->addError('username', $message); } } catch (\Exception $e) { $this->addError('username', 'An error occurred: ' . $e->getMessage()); } + } public function render() @@ -53,4 +49,59 @@ class LoginForm extends Component return view('livewire.auth.login-form'); // This will point to the resource/views/livewire/auth/login-form.blade.php component } -} \ No newline at end of file +} + + + +// validate([ +// 'username' => 'required|string', +// 'password' => 'required|string', +// ]); + +// try { +// $response = Http::post(config('services.backend_api.url') . '/api/cms/login_password', [ +// 'username' => $this->username, +// 'password' => $this->password, +// ]); + +// $json = $response->json(); + +// if ($response->successful()) { +// if ($json['code'] === 200) { +// Session::put('admin_uuid', $json['data']['admin_uuid'] ?? null); + +// return redirect('/main/profile'); +// } else { +// $this->addError('username', $json['message'] ?? 'Login failed.'); +// } +// } else { +// $message = $json['message'] ?? 'Login request failed. Please try again.'; +// $this->addError('username', $message); +// } +// } catch (\Exception $e) { +// $this->addError('username', 'An error occurred: ' . $e->getMessage()); +// } + +// } + +// public function render() +// { +// return view('livewire.auth.login-form'); // This will point to the resource/views/livewire/auth/login-form.blade.php component +// } + +// } \ No newline at end of file diff --git a/app/Livewire/Profile.php b/app/Livewire/Profile.php index 38ecae2..f301aeb 100644 --- a/app/Livewire/Profile.php +++ b/app/Livewire/Profile.php @@ -3,11 +3,24 @@ namespace App\Livewire; use Livewire\Component; +use Livewire\Attributes\On; +use Illuminate\Support\Facades\Session; class Profile extends Component { + public $user; + + public function mount() + { + $this->user = Session::get('user'); + + if (!$this->user) { + return $this->redirect('/login'); // Livewire 3 way + } + } + public function render() { return view('livewire.profile.profile'); } -} +} \ No newline at end of file diff --git a/config/services.php b/config/services.php index 27a3617..53e35bf 100644 --- a/config/services.php +++ b/config/services.php @@ -34,5 +34,9 @@ return [ 'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'), ], ], + + 'backend_api' => [ + 'url' => env('BACKEND_API_URL'), +], ]; diff --git a/docker-compose.yml b/docker-compose.yml index 1f91032..aa15443 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -64,5 +64,4 @@ volumes: networks: app_network: - driver: bridge - external: true \ No newline at end of file + driver: bridge \ No newline at end of file diff --git a/resources/views/livewire/profile/profile.blade.php b/resources/views/livewire/profile/profile.blade.php index 8d879df..45ea6c3 100644 --- a/resources/views/livewire/profile/profile.blade.php +++ b/resources/views/livewire/profile/profile.blade.php @@ -1,25 +1,34 @@ -
Role: System Admin
-+ Role: + {{ $user['role'] === 1 ? 'System Admin' : 'User' }} +