45 lines
1.5 KiB
Dart
45 lines
1.5 KiB
Dart
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
import 'package:marco/model/user_permission.dart';
|
|
|
|
class PermissionService {
|
|
static Future<List<UserPermission>> fetchPermissions(String token) async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('https://api.marcoaiot.com/api/user/profile'),
|
|
headers: {'Authorization': 'Bearer $token'},
|
|
);
|
|
|
|
// Print the full response for debugging
|
|
print('Status Code: ${response.statusCode}');
|
|
print('Response Body: ${response.body}');
|
|
|
|
if (response.statusCode == 200) {
|
|
final decoded = json.decode(response.body);
|
|
|
|
// Debug the decoded data
|
|
print('Decoded Data: $decoded');
|
|
|
|
// Extract featurePermissions
|
|
final List<dynamic> featurePermissions =
|
|
decoded['data']['featurePermissions'];
|
|
|
|
// Check if the featurePermissions are indeed a List of Strings
|
|
print('FeaturePermissions Type: ${featurePermissions.runtimeType}');
|
|
|
|
// Map the featurePermissions to UserPermission objects
|
|
return featurePermissions
|
|
.map<UserPermission>(
|
|
(permissionId) => UserPermission.fromJson({'id': permissionId}))
|
|
.toList();
|
|
} else {
|
|
final errorData = json.decode(response.body);
|
|
throw Exception('Failed to load permissions: ${errorData['message']}');
|
|
}
|
|
} catch (e) {
|
|
print('Error fetching permissions: $e');
|
|
throw Exception('Error fetching permissions: $e');
|
|
}
|
|
}
|
|
}
|