validate([ 'username' => 'required|string', 'password' => 'required|string', ]); $username = $request->input('username'); $password = $request->input('password'); // Step 1: Verify the username $usernameApiUrl = 'http://192.168.56.1:8080/api/cms/username_login'; try { $usernameResponse = Http::timeout(30)->post($usernameApiUrl, [ 'username' => $username, ]); $usernameData = $usernameResponse->json(); // Check if username verification failed if (!$usernameResponse->successful() || !isset($usernameData['data']['is_verified']) || !$usernameData['data']['is_verified']) { return redirect()->back()->with('error', $usernameData['message'] ?? 'Username does not exist.'); } // Step 2: Authenticate with password $loginApiUrl = 'http://192.168.56.1:8080/api/cms/login_password'; $loginResponse = Http::timeout(30)->post($loginApiUrl, [ 'username' => $username, 'password' => $password, ]); $loginData = $loginResponse->json(); if (!$loginResponse->successful()) { return redirect()->back()->with('error', $loginData['message'] ?? 'Login failed.'); } if (!$loginData['success']) { $message = $loginData['message'] ?? 'Login failed'; if ($message === 'User must change password') { // Store admin_uuid in session for password change session(['admin_uuid' => $loginData['data']['admin_uuid']]); return redirect()->route('change-password')->with('error', 'You must change your password.'); } return redirect()->back()->with('error', $message); } if (isset($loginData['data']['token'])) { session(['authToken' => $loginData['data']['token']]); } return redirect()->route('my-profile')->with('success', 'Login successful!'); } catch (\Exception $e) { return redirect()->back()->with('error', 'Error connecting to API: ' . $e->getMessage()); } } // Add a method to handle password change (optional, if you want to implement the change-password route) public function changePassword(Request $request) { $request->validate([ 'password' => 'required|string|min:8|confirmed', ]); $adminUuid = session('admin_uuid'); if (!$adminUuid) { return redirect()->route('login')->with('error', 'Invalid session. Please login again.'); } $changePasswordApiUrl = 'http://192.168.56.1:8080/api/cms/change_password'; try { $response = Http::timeout(30)->post($changePasswordApiUrl, [ 'admin_uuid' => $adminUuid, 'password' => $request->input('password'), ]); $data = $response->json(); if (!$response->successful() || !$data['success']) { return redirect()->back()->with('error', $data['message'] ?? 'Failed to change password.'); } if (isset($data['data']['token'])) { session(['authToken' => $data['data']['token']]); } // Clear admin_uuid from session session()->forget('admin_uuid'); return redirect()->route('my-profile')->with('success', 'Password changed successfully!'); } catch (\Exception $e) { return redirect()->back()->with('error', 'Error connecting to API: ' . $e->getMessage()); } } }