2.8 KiB
2.8 KiB
Walkthrough - Login API Integration
I have successfully integrated the login API into the Employee Portal application.
Changes Made
API Layer
- Created
lib/api/login/login_api.dartusing thediopackage (the Flutter equivalent of Axios). - Implemented a robust
loginmethod that handles JSON serialization and error states automatically. - Added detailed debug logging for requests (URL, payload, headers) and responses/errors (status code, data, error types) to simplify troubleshooting.
- Integrated
flutter_dotenvto manage the API base URL via an.envfile, removing hardcoded URLs from the code. - Created
lib/api/profile/profile_api.dartfor Profile API Integration, implementing token acquisition and employee detail retrieval. - Fixed a critical URL formatting bug (leading space) in
LoginApi.dartthat caused connection errors. - Enhanced login validation to properly handle successful authentication and store the logged-in NIK in a global
auth_state. - Refactored
ProfilePage.dartto use the dynamicloggedInNik, ensuring it loads relevant employee data automatically after login. - Implemented
lib/utils/dialog_utils.dart, a modular Error Dialog utility that provides premium-styled popups for login failures and validation errors. - Structured the API code to follow the
axiosstyle configuration highly requested by you.
UI Layer
- Modified
lib/pages/login_page.dartto use the newLoginApi. - Added a
CircularProgressIndicatorto the "Sign In" button to indicate an ongoing request. - Implemented error handling to display messages from the API (e.g., "Invalid credentials" or connection issues).
- Maintained the existing CAPTCHA verification before proceeding with the API call.
Configuration
- Added the
httppackage topubspec.yamlto enable network requests.
Verification
Manual Test Steps
- Enter Credentials: Input
nik: "2231487"andpassword: "Eko54r99". - CAPTCHA: Enter the verification code displayed on the screen.
- Submit: Click "Sign In".
- Loading State: Observe the loading indicator on the button.
- Success: Verify that you are redirected to the dashboard.
- Failure: Try with incorrect credentials to verify the error message display.
Troubleshooting CORS (Flutter Web)
If you are running the app on Flutter Web, you will encounter a CORS error because the API server does not allow requests from localhost.
Recommended Fixes:
- Mobile Emulator: Run the app on an Android Emulator or iOS Simulator.
- Disable Web Security: Run the app with the following flag:
flutter run -d chrome --web-browser-flag "--disable-web-security"
Note: Make sure your device has internet access to reach https://ess-api.ebi-ict.my.id.