LifeMate — Personal, Family & Learning Companion
phase-2/product-foundation-identity
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 | |
| 42 | class ApiException implements Exception { |
| 43 | const ApiException(this.statusCode, this.code); |
| 44 | final int statusCode; |
| 45 | final String code; |
| 46 | |
| 47 | @override |
| 48 | String toString() => 'ApiException($statusCode, $code)'; |
| 49 | } |
| 50 | |
| 51 | class HttpIdentityApi implements IdentityApi { |
| 52 | HttpIdentityApi({ |
| 53 | String? baseUrl, |
| 54 | http.Client? client, |
| 55 | }) : baseUrl = baseUrl ?? |
| 56 | const String.fromEnvironment( |
| 57 | 'LIFEMATE_API_URL', |
| 58 | defaultValue: 'http://localhost:8080', |
| 59 | ), |
| 60 | _client = client ?? http.Client(); |
| 61 | |
| 62 | final String baseUrl; |
| 63 | final http.Client _client; |
| 64 | |
| 65 | @override |
| 66 | String? accessToken; |
| 67 | String? _refreshToken; |
| 68 | |
| 69 | Uri _uri(String path) => Uri.parse('$baseUrl$path'); |
| 70 | |
| 71 | Map<String, String> _headers({bool auth = false}) => { |
| 72 | 'content-type': 'application/json', |
| 73 | if (auth && accessToken != null) 'authorization': 'Bearer $accessToken', |
| 74 | }; |
| 75 | |
| 76 | Future<Map<String, dynamic>> _json( |
| 77 | String method, |
| 78 | String path, { |
| 79 | Map<String, dynamic>? body, |
| 80 | bool auth = false, |
| 81 | bool retry = true, |
| 82 | }) async { |
| 83 | final request = http.Request(method, _uri(path)) |
| 84 | ..headers.addAll(_headers(auth: auth)); |
| 85 | if (body != null) request.body = jsonEncode(body); |
| 86 | |
| 87 | final streamed = await _client.send(request); |
| 88 | final response = await http.Response.fromStream(streamed); |
| 89 | |
| 90 | if (response.statusCode == 401 && auth && retry && _refreshToken != null) { |
| 91 | await _refresh(); |
| 92 | return _json(method, path, body: body, auth: auth, retry: false); |
| 93 | } |
| 94 | |
| 95 | if (response.statusCode < 200 || response.statusCode >= 300) { |
| 96 | var code = 'request_failed'; |
| 97 | if (response.body.isNotEmpty) { |
| 98 | try { |
| 99 | code = (jsonDecode(response.body) as Map<String, dynamic>)['error'] |
| 100 | ?.toString() ?? |
| 101 | code; |
| 102 | } catch (_) {} |
| 103 | } |
| 104 | throw ApiException(response.statusCode, code); |
| 105 | } |
| 106 | |
| 107 | if (response.body.isEmpty) return <String, dynamic>{}; |
| 108 | return jsonDecode(response.body) as Map<String, dynamic>; |
| 109 | } |
| 110 | |
| 111 | Future<void> _refresh() async { |
| 112 | final token = _refreshToken; |
| 113 | if (token == null) throw const ApiException(401, 'session_expired'); |
| 114 | final result = await _json( |
| 115 | 'POST', |
| 116 | '/v1/auth/refresh', |
| 117 | body: {'refreshToken': token}, |
| 118 | retry: false, |
| 119 | ); |
| 120 | accessToken = result['accessToken']?.toString(); |
| 121 | _refreshToken = result['refreshToken']?.toString(); |
| 122 | } |
| 123 | |
| 124 | @override |
| 125 | Future<void> register({ |
| 126 | required String displayName, |
| 127 | required String email, |
| 128 | required String password, |
| 129 | }) async { |
| 130 | await _json('POST', '/v1/auth/register', body: { |
| 131 | 'displayName': displayName, |
| 132 | 'email': email, |
| 133 | 'password': password, |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | @override |
| 138 | Future<void> verifyEmail(String token) async { |
| 139 | await _json('POST', '/v1/auth/verify-email', body: {'token': token}); |
| 140 | } |
| 141 | |
| 142 | @override |
| 143 | Future<void> login(String email, String password) async { |
| 144 | final result = await _json('POST', '/v1/auth/login', body: { |
| 145 | 'email': email, |
| 146 | 'password': password, |
| 147 | }); |
| 148 | accessToken = result['accessToken']?.toString(); |
| 149 | _refreshToken = result['refreshToken']?.toString(); |
| 150 | } |
| 151 | |
| 152 | @override |
| 153 | Future<void> forgotPassword(String email) async { |
| 154 | await _json('POST', '/v1/auth/forgot-password', body: {'email': email}); |
| 155 | } |
| 156 | |
| 157 | @override |
| 158 | Future<void> resetPassword(String token, String newPassword) async { |
| 159 | await _json('POST', '/v1/auth/reset-password', body: { |
| 160 | 'token': token, |
| 161 | 'newPassword': newPassword, |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | @override |
| 166 | Future<void> changePassword( |
| 167 | String currentPassword, |
| 168 | String newPassword, |
| 169 | ) async { |
| 170 | await _json('POST', '/v1/auth/change-password', auth: true, body: { |
| 171 | 'currentPassword': currentPassword, |
| 172 | 'newPassword': newPassword, |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | @override |
| 177 | Future<Map<String, dynamic>> getProfile() => |
| 178 | _json('GET', '/v1/profile', auth: true); |
| 179 | |
| 180 | @override |
| 181 | Future<Map<String, dynamic>> updateProfile({ |
| 182 | String? displayName, |
| 183 | String? birthDate, |
| 184 | String? themePreference, |
| 185 | }) => |
| 186 | _json('PATCH', '/v1/profile', auth: true, body: { |
| 187 | if (displayName != null) 'displayName': displayName, |
| 188 | if (birthDate != null) 'birthDate': birthDate, |
| 189 | if (themePreference != null) 'themePreference': themePreference, |
| 190 | }); |
| 191 | |
| 192 | @override |
| 193 | Future<List<Map<String, dynamic>>> listFamilies() async { |
| 194 | final result = await _json('GET', '/v1/families', auth: true); |
| 195 | final families = (result['families'] as List<dynamic>? ?? const []); |
| 196 | return families |
| 197 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 198 | .toList(); |
| 199 | } |
| 200 | |
| 201 | @override |
| 202 | Future<List<Map<String, dynamic>>> listFamilyMembers(String familyId) async { |
| 203 | final result = await _json( |
| 204 | 'GET', |
| 205 | '/v1/families/$familyId/members', |
| 206 | auth: true, |
| 207 | ); |
| 208 | final members = (result['members'] as List<dynamic>? ?? const []); |
| 209 | return members |
| 210 | .map((item) => Map<String, dynamic>.from(item as Map)) |
| 211 | .toList(); |
| 212 | } |
| 213 | |
| 214 | @override |
| 215 | Future<Map<String, dynamic>> createFamily( |
| 216 | String name, { |
| 217 | String? role, |
| 218 | }) => |
| 219 | _json('POST', '/v1/families', auth: true, body: { |
| 220 | 'name': name, |
| 221 | if (role != null) 'role': role, |
| 222 | }); |
| 223 | |
| 224 | @override |
| 225 | Future<void> inviteMember({ |
| 226 | required String familyId, |
| 227 | required String email, |
| 228 | required String role, |
| 229 | String? themePreference, |
| 230 | }) async { |
| 231 | await _json( |
| 232 | 'POST', |
| 233 | '/v1/families/$familyId/invitations', |
| 234 | auth: true, |
| 235 | body: { |
| 236 | 'email': email, |
| 237 | 'role': role, |
| 238 | if (themePreference != null) 'themePreference': themePreference, |
| 239 | }, |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | @override |
| 244 | Future<void> acceptInvitation(String token) async { |
| 245 | await _json( |
| 246 | 'POST', |
| 247 | '/v1/invitations/accept', |
| 248 | auth: true, |
| 249 | body: {'token': token}, |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | @override |
| 254 | Future<void> setGuardian({ |
| 255 | required String familyId, |
| 256 | required String guardianUserId, |
| 257 | required String minorUserId, |
| 258 | }) async { |
| 259 | await _json( |
| 260 | 'POST', |
| 261 | '/v1/families/$familyId/guardians', |
| 262 | auth: true, |
| 263 | body: { |
| 264 | 'guardianUserId': guardianUserId, |
| 265 | 'minorUserId': minorUserId, |
| 266 | }, |
| 267 | ); |
| 268 | } |
| 269 | } |