marco.pms.mobile/lib/helpers/services/auth_service.dart
Vaibhav Surve 753bfcad8a feat: Implement API service for project and employee management
- Added `ApiService` class for handling API requests related to projects and employees.
- Implemented methods to fetch projects and employees by project ID with JWT authentication.
- Enhanced `AuthService` to store JWT and refresh tokens upon user login.
- Updated `LocalStorage` to manage JWT and refresh tokens.
- Created models for `AttendanceModel`, `EmployeeModel`, and `ProjectModel` to structure data.
- Introduced `AttendanceScreen` for displaying attendance data with a new UI layout.
- Removed deprecated `attendance_screen.dart` and replaced it with `attendanceScreen.dart`.
- Updated routing to reflect the new attendance screen structure.
- Integrated geolocation and permission handling plugins for enhanced functionality.
- Updated dependencies in `pubspec.yaml` and `pubspec.lock` for new packages.
2025-04-21 17:45:54 +05:30

51 lines
1.7 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:marco/helpers/services/storage/local_storage.dart';
class AuthService {
static bool isLoggedIn = false;
static Future<Map<String, String>?> loginUser(Map<String, dynamic> data) async {
try {
final response = await http.post(
Uri.parse('https://api.marcoaiot.com/api/auth/login'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(data),
);
if (response.statusCode == 200) {
isLoggedIn = true;
// Parse the response to get the JWT and refresh tokens
final responseData = jsonDecode(response.body);
// Adjusted for the actual response structure
final jwtToken = responseData['data']['token']; // Ensure this matches your actual response
// Save the JWT token in local storage
await LocalStorage.setJwtToken(jwtToken);
print("JWT Token: $jwtToken");
// Optionally save refresh token if available
final refreshToken = responseData['data']['refreshToken'];
if (refreshToken != null) {
await LocalStorage.setRefreshToken(refreshToken);
print("Refresh Token: $refreshToken");
}
// Save the login state in local storage
await LocalStorage.setLoggedInUser(true);
// Return null to indicate success
return null;
} else if (response.statusCode == 401) {
return {"password": "Invalid email or password"};
} else {
return {"error": "Something went wrong. Please try again."};
}
} catch (e) {
return {"error": "Network error. Please check your connection."};
}
}
}