LifeMate — Personal, Family & Learning Companion
phase-3/planner-school-family-core
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:http/http.dart' as http; |
| 4 | |
| 5 | abstract class IdentityApi { |
| 6 | String? get accessToken; |
| 7 | |
| 8 | Future<void> register({ |
| 9 | required String displayName, |
| 10 | required String email, |
| 11 | required String password, |
| 12 | }); |
| 13 | |
| 14 | Future<void> verifyEmail(String token); |
| 15 | Future<void> login(String email, String password); |
| 16 | Future<void> forgotPassword(String email); |
| 17 | Future<void> resetPassword(String token, String newPassword); |
| 18 | Future<void> changePassword(String currentPassword, String newPassword); |
| 19 | Future<Map<String, dynamic>> getProfile(); |
| 20 | Future<Map<String, dynamic>> updateProfile({ |
| 21 | String? displayName, |
| 22 | String? birthDate, |
| 23 | String? themePreference, |
| 24 | }); |
| 25 | Future<List<Map<String, dynamic>>> listFamilies(); |
| 26 | Future<List<Map<String, dynamic>>> listFamilyMembers(String familyId); |
| 27 | Future<Map<String, dynamic>> createFamily(String name, {String? role}); |
| 28 | Future<void> inviteMember({ |
| 29 | required String familyId, |
| 30 | required String email, |
| 31 | required String role, |
| 32 | String? themePreference, |
| 33 | }); |
| 34 | Future<void> acceptInvitation(String token); |
| 35 | Future<void> setGuardian({ |
| 36 | required String familyId, |
| 37 | required String guardianUserId, |
| 38 | required String minorUserId, |
| 39 | }); |
| 40 | |
| 41 | Future<List<Map<String, dynamic>>> getToday({ |
| 42 | required DateTime from, |
| 43 | required DateTime to, |
| 44 | }); |
| 45 | Future<List<Map<String, dynamic>>> listPlanItems({ |
| 46 | DateTime? from, |
| 47 | DateTime? to, |
| 48 | String? kind, |
| 49 | }); |
| 50 | Future<Map<String, dynamic>> createPlanItem(Map<String, dynamic> data); |
| 51 | Future<Map<String, dynamic>> updatePlanItem( |
| 52 | String itemId, |
| 53 | Map<String, dynamic> data, |
| 54 | ); |
| 55 | Future<List<Map<String, dynamic>>> listLifeContexts(); |
| 56 | Future<Map<String, dynamic>> createLifeContext({ |
| 57 | required String kind, |
| 58 | required String title, |
| 59 | }); |
| 60 | Future<Map<String, dynamic>> createAcademicYear(Map<String, dynamic> data); |
| 61 | Future<Map<String, dynamic>> createAcademicTerm( |
| 62 | String yearId, |
| 63 | Map<String, dynamic> data, |
| 64 | ); |
| 65 | Future<Map<String, dynamic>> createSubject( |
| 66 | String termId, |
| 67 | Map<String, dynamic> data, |
| 68 | ); |
| 69 | Future<Map<String, dynamic>> createClassSession( |
| 70 | String subjectId, |
| 71 | Map<String, dynamic> data, |
| 72 | ); |
| 73 | Future<Map<String, dynamic>> getSchoolOverview(String studentUserId); |
| 74 | Future<List<Map<String, dynamic>>> getTimetable(String studentUserId); |
| 75 | Future<Map<String, dynamic>> setGrade( |
| 76 | String itemId, { |
| 77 | required num points, |
| 78 | required num outOf, |
| 79 | }); |
| 80 | Future<List<Map<String, dynamic>>> claimDueReminders(); |
| 81 | Future<List<Map<String, dynamic>>> getFamilyCalendar(String familyId); |
| 82 | Future<Map<String, dynamic>> getChildSupportSummary( |
| 83 | String familyId, |
| 84 | String studentUserId, |
| 85 | ); |
| 86 | Future<Map<String, dynamic>> submitSyncMutations( |
| 87 | List<Map<String, dynamic>> mutations, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | class ApiException implements Exception { |
| 92 | const ApiException(this.statusCode, this.code); |
| 93 | final int statusCode; |
| 94 | final String code; |
| 95 | |
| 96 | @override |
| 97 | String toString() => 'ApiException($statusCode, $code)'; |
| 98 | } |
| 99 | |
| 100 | class HttpIdentityApi implements IdentityApi { |
| 101 | HttpIdentityApi({ |
| 102 | String? baseUrl, |
| 103 | http.Client? client, |
| 104 | }) : baseUrl = baseUrl ?? |
| 105 | const String.fromEnvironment( |
| 106 | 'LIFEMATE_API_URL', |
| 107 | defaultValue: 'http://localhost:8080', |
| 108 | ), |
| 109 | _client = client ?? http.Client(); |
| 110 | |
| 111 | final String baseUrl; |
| 112 | final http.Client _client; |
| 113 | |
| 114 | @override |
| 115 | String? accessToken; |
| 116 | String? _refreshToken; |
| 117 | |
| 118 | Uri _uri(String path) => Uri.parse('$baseUrl$path'); |
| 119 | |
| 120 | Map<String, String> _headers({bool auth = false}) => { |
| 121 | 'content-type': 'application/json', |
| 122 | if (auth && accessToken != null) 'authorization': 'Bearer $accessToken', |
| 123 | }; |
| 124 | |
| 125 | Future<Map<String, dynamic>> _json( |
| 126 | String method, |
| 127 | String path, { |
| 128 | Map<String, dynamic>? body, |
| 129 | bool auth = false, |
| 130 | bool retry = true, |
| 131 | }) async { |
| 132 | final request = http.Request(method, _uri(path)) |
| 133 | ..headers.addAll(_headers(auth: auth)); |
| 134 | if (body != null) request.body = jsonEncode(body); |
| 135 | |
| 136 | final streamed = await _client.send(request); |
| 137 | final response = await http.Response.fromStream(streamed); |
| 138 | |
| 139 | if (response.statusCode == 401 && auth && retry && _refreshToken != null) { |
| 140 | await _refresh(); |
| 141 | return _json(method, path, body: body, auth: auth, retry: false); |
| 142 | } |
| 143 | |
| 144 | if (response.statusCode < 200 || response.statusCode >= 300) { |
| 145 | var code = 'request_failed'; |
| 146 | if (response.body.isNotEmpty) { |
| 147 | try { |
| 148 | code = (jsonDecode(response.body) as Map<String, dynamic>)['error'] |
| 149 | ?.toString() ?? |
| 150 | code; |
| 151 | } catch (_) {} |
| 152 | } |
| 153 | throw ApiException(response.statusCode, code); |
| 154 | } |
| 155 | |
| 156 | if (response.body.isEmpty) return <String, dynamic>{}; |
| 157 | return jsonDecode(response.body) as Map<String, dynamic>; |
| 158 | } |
| 159 | |
| 160 | Future<void> _refresh() async { |
| 161 | final token = _refreshToken; |
| 162 | if (token == null) throw const ApiException(401, 'session_expired'); |
| 163 | final result = await _json( |
| 164 | 'POST', |
| 165 | '/v1/auth/refresh', |
| 166 | body: {'refreshToken': token}, |
| 167 | retry: false, |
| 168 | ); |
| 169 | accessToken = result['accessToken']?.toString(); |
| 170 | _refreshToken = result['refreshToken']?.toString(); |
| 171 | } |
| 172 | |
| 173 | @override |
| 174 | Future<void> register({ |
| 175 | required String displayName, |
| 176 | required String email, |
| 177 | required String password, |
| 178 | }) async { |
| 179 | await _json('POST', '/v1/auth/register', body: { |
| 180 | 'displayName': displayName, |
| 181 | 'email': email, |
| 182 | 'password': password, |
| 183 | }); |
| 184 | } |
| 185 | |
| 186 | @override |
| 187 | Future<void> verifyEmail(String token) async { |
| 188 | await _json('POST', '/v1/auth/verify-email', body: {'token': token}); |
| 189 | } |
| 190 | |
| 191 | @override |
| 192 | Future<void> login(String email, String password) async { |
| 193 | final result = await _json('POST', '/v1/auth/login', body: { |
| 194 | 'email': email, |
| 195 | 'password': password, |
| 196 | }); |
| 197 | accessToken = result['accessToken']?.toString(); |
| 198 | _refreshToken = result['refreshToken']?.toString(); |
| 199 | } |
| 200 | |
| 201 | @override |
| 202 | Future<void> forgotPassword(String email) async { |
| 203 | await _json('POST', '/v1/auth/forgot-password', body: {'email': email}); |
| 204 | } |
| 205 | |
| 206 | @override |
| 207 | Future<void> resetPassword(String token, String newPassword) async { |
| 208 | await _json('POST', '/v1/auth/reset-password', body: { |
| 209 | 'token': token, |
| 210 | 'newPassword': newPassword, |
| 211 | }); |
| 212 | } |
| 213 | |
| 214 | @override |
| 215 | Future<void> changePassword( |
| 216 | String currentPassword, |
| 217 | String newPassword, |
| 218 | ) async { |
| 219 | await _json('POST', '/v1/auth/change-password', auth: true, body: { |
| 220 | 'currentPassword': currentPassword, |
| 221 | 'newPassword': newPassword, |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | @override |
| 226 | Future<Map<String, dynamic>> getProfile() => |
| 227 | _json('GET', '/v1/profile', auth: true); |
| 228 | |
| 229 | @override |
| 230 | Future<Map<String, dynamic>> updateProfile({ |
| 231 | String? displayName, |
| 232 | String? birthDate, |
| 233 | String? themePreference, |
| 234 | }) => |
| 235 | _json('PATCH', '/v1/profile', auth: true, body: { |
| 236 | if (displayName != null) 'displayName': displayName, |
| 237 | if (birthDate != null) 'birthDate': birthDate, |
| 238 | if (themePreference != null) 'themePreference': themePreference, |
| 239 | }); |
| 240 | |
| 241 | @override |
| 242 | Future<List<Map<String, dynamic>>> listFamilies() async { |
| 243 | final result = await _json('GET', '/v1/families', auth: true); |
| 244 | final families = (result['families'] as List<dynamic>? ?? const []); |
| 245 | return families |
| 246 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 247 | .toList(); |
| 248 | } |
| 249 | |
| 250 | @override |
| 251 | Future<List<Map<String, dynamic>>> listFamilyMembers(String familyId) async { |
| 252 | final result = await _json( |
| 253 | 'GET', |
| 254 | '/v1/families/$familyId/members', |
| 255 | auth: true, |
| 256 | ); |
| 257 | final members = (result['members'] as List<dynamic>? ?? const []); |
| 258 | return members |
| 259 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 260 | .toList(); |
| 261 | } |
| 262 | |
| 263 | @override |
| 264 | Future<Map<String, dynamic>> createFamily( |
| 265 | String name, { |
| 266 | String? role, |
| 267 | }) => |
| 268 | _json('POST', '/v1/families', auth: true, body: { |
| 269 | 'name': name, |
| 270 | if (role != null) 'role': role, |
| 271 | }); |
| 272 | |
| 273 | @override |
| 274 | Future<void> inviteMember({ |
| 275 | required String familyId, |
| 276 | required String email, |
| 277 | required String role, |
| 278 | String? themePreference, |
| 279 | }) async { |
| 280 | await _json( |
| 281 | 'POST', |
| 282 | '/v1/families/$familyId/invitations', |
| 283 | auth: true, |
| 284 | body: { |
| 285 | 'email': email, |
| 286 | 'role': role, |
| 287 | if (themePreference != null) 'themePreference': themePreference, |
| 288 | }, |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | @override |
| 293 | Future<void> acceptInvitation(String token) async { |
| 294 | await _json( |
| 295 | 'POST', |
| 296 | '/v1/invitations/accept', |
| 297 | auth: true, |
| 298 | body: {'token': token}, |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | @override |
| 303 | Future<void> setGuardian({ |
| 304 | required String familyId, |
| 305 | required String guardianUserId, |
| 306 | required String minorUserId, |
| 307 | }) async { |
| 308 | await _json( |
| 309 | 'POST', |
| 310 | '/v1/families/$familyId/guardians', |
| 311 | auth: true, |
| 312 | body: { |
| 313 | 'guardianUserId': guardianUserId, |
| 314 | 'minorUserId': minorUserId, |
| 315 | }, |
| 316 | ); |
| 317 | } |
| 318 | @override |
| 319 | Future<List<Map<String, dynamic>>> getToday({ |
| 320 | required DateTime from, |
| 321 | required DateTime to, |
| 322 | }) async { |
| 323 | final result = await _json( |
| 324 | 'GET', |
| 325 | '/v1/today?from=${Uri.encodeQueryComponent(from.toUtc().toIso8601String())}&to=${Uri.encodeQueryComponent(to.toUtc().toIso8601String())}', |
| 326 | auth: true, |
| 327 | ); |
| 328 | return (result['items'] as List<dynamic>? ?? const []) |
| 329 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 330 | .toList(); |
| 331 | } |
| 332 | |
| 333 | @override |
| 334 | Future<List<Map<String, dynamic>>> listPlanItems({ |
| 335 | DateTime? from, |
| 336 | DateTime? to, |
| 337 | String? kind, |
| 338 | }) async { |
| 339 | final params = <String, String>{}; |
| 340 | if (from != null) params['from'] = from.toUtc().toIso8601String(); |
| 341 | if (to != null) params['to'] = to.toUtc().toIso8601String(); |
| 342 | if (kind != null) params['kind'] = kind; |
| 343 | final suffix = params.isEmpty ? '' : '?${Uri(queryParameters: params).query}'; |
| 344 | final result = await _json('GET', '/v1/plan-items$suffix', auth: true); |
| 345 | return (result['items'] as List<dynamic>? ?? const []) |
| 346 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 347 | .toList(); |
| 348 | } |
| 349 | |
| 350 | @override |
| 351 | Future<Map<String, dynamic>> createPlanItem(Map<String, dynamic> data) => |
| 352 | _json('POST', '/v1/plan-items', auth: true, body: data); |
| 353 | |
| 354 | @override |
| 355 | Future<Map<String, dynamic>> updatePlanItem( |
| 356 | String itemId, |
| 357 | Map<String, dynamic> data, |
| 358 | ) => |
| 359 | _json('PATCH', '/v1/plan-items/$itemId', auth: true, body: data); |
| 360 | |
| 361 | @override |
| 362 | Future<List<Map<String, dynamic>>> listLifeContexts() async { |
| 363 | final result = await _json('GET', '/v1/life-contexts', auth: true); |
| 364 | return (result['contexts'] as List<dynamic>? ?? const []) |
| 365 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 366 | .toList(); |
| 367 | } |
| 368 | |
| 369 | @override |
| 370 | Future<Map<String, dynamic>> createLifeContext({ |
| 371 | required String kind, |
| 372 | required String title, |
| 373 | }) => |
| 374 | _json( |
| 375 | 'POST', |
| 376 | '/v1/life-contexts', |
| 377 | auth: true, |
| 378 | body: {'kind': kind, 'title': title}, |
| 379 | ); |
| 380 | |
| 381 | @override |
| 382 | Future<Map<String, dynamic>> createAcademicYear( |
| 383 | Map<String, dynamic> data, |
| 384 | ) => |
| 385 | _json('POST', '/v1/school/years', auth: true, body: data); |
| 386 | |
| 387 | @override |
| 388 | Future<Map<String, dynamic>> createAcademicTerm( |
| 389 | String yearId, |
| 390 | Map<String, dynamic> data, |
| 391 | ) => |
| 392 | _json( |
| 393 | 'POST', |
| 394 | '/v1/school/years/$yearId/terms', |
| 395 | auth: true, |
| 396 | body: data, |
| 397 | ); |
| 398 | |
| 399 | @override |
| 400 | Future<Map<String, dynamic>> createSubject( |
| 401 | String termId, |
| 402 | Map<String, dynamic> data, |
| 403 | ) => |
| 404 | _json( |
| 405 | 'POST', |
| 406 | '/v1/school/terms/$termId/subjects', |
| 407 | auth: true, |
| 408 | body: data, |
| 409 | ); |
| 410 | |
| 411 | @override |
| 412 | Future<Map<String, dynamic>> createClassSession( |
| 413 | String subjectId, |
| 414 | Map<String, dynamic> data, |
| 415 | ) => |
| 416 | _json( |
| 417 | 'POST', |
| 418 | '/v1/school/subjects/$subjectId/classes', |
| 419 | auth: true, |
| 420 | body: data, |
| 421 | ); |
| 422 | |
| 423 | @override |
| 424 | Future<Map<String, dynamic>> getSchoolOverview(String studentUserId) => |
| 425 | _json('GET', '/v1/school/$studentUserId/overview', auth: true); |
| 426 | |
| 427 | @override |
| 428 | Future<List<Map<String, dynamic>>> getTimetable(String studentUserId) async { |
| 429 | final result = await _json( |
| 430 | 'GET', |
| 431 | '/v1/school/$studentUserId/timetable', |
| 432 | auth: true, |
| 433 | ); |
| 434 | return (result['classes'] as List<dynamic>? ?? const []) |
| 435 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 436 | .toList(); |
| 437 | } |
| 438 | |
| 439 | @override |
| 440 | Future<Map<String, dynamic>> setGrade( |
| 441 | String itemId, { |
| 442 | required num points, |
| 443 | required num outOf, |
| 444 | }) => |
| 445 | _json( |
| 446 | 'PATCH', |
| 447 | '/v1/school/plan-items/$itemId/grade', |
| 448 | auth: true, |
| 449 | body: {'points': points, 'outOf': outOf}, |
| 450 | ); |
| 451 | |
| 452 | @override |
| 453 | Future<List<Map<String, dynamic>>> claimDueReminders() async { |
| 454 | final result = await _json( |
| 455 | 'POST', |
| 456 | '/v1/reminders/claim-due', |
| 457 | auth: true, |
| 458 | body: {'limit': 20}, |
| 459 | ); |
| 460 | return (result['reminders'] as List<dynamic>? ?? const []) |
| 461 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 462 | .toList(); |
| 463 | } |
| 464 | |
| 465 | @override |
| 466 | Future<List<Map<String, dynamic>>> getFamilyCalendar(String familyId) async { |
| 467 | final result = await _json( |
| 468 | 'GET', |
| 469 | '/v1/families/$familyId/calendar', |
| 470 | auth: true, |
| 471 | ); |
| 472 | return (result['items'] as List<dynamic>? ?? const []) |
| 473 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 474 | .toList(); |
| 475 | } |
| 476 | |
| 477 | @override |
| 478 | Future<Map<String, dynamic>> getChildSupportSummary( |
| 479 | String familyId, |
| 480 | String studentUserId, |
| 481 | ) => |
| 482 | _json( |
| 483 | 'GET', |
| 484 | '/v1/families/$familyId/children/$studentUserId/support-summary', |
| 485 | auth: true, |
| 486 | ); |
| 487 | |
| 488 | @override |
| 489 | Future<Map<String, dynamic>> submitSyncMutations( |
| 490 | List<Map<String, dynamic>> mutations, |
| 491 | ) => |
| 492 | _json( |
| 493 | 'POST', |
| 494 | '/v1/sync/mutations', |
| 495 | auth: true, |
| 496 | body: {'mutations': mutations}, |
| 497 | ); |
| 498 | |
| 499 | } |