LifeMate — Personal, Family & Learning Companion
feat: add provider-neutral identity API client
davoodmehraban89 committed
Oct 5, 2026 at 07:40 UTC
ae43e024924bacb967fa4d2c66d3460240efa86a
1 file changed
+217
apps/lifemate/lib/api.dart
new
+217
@@ -0,0 +1,217 @@
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<Map<String, dynamic>> createFamily(String name, {String? role});
26
+ Future<void> inviteMember({
27
+ required String familyId,
28
+ required String email,
29
+ required String role,
30
+ });
31
+ Future<void> acceptInvitation(String token);
32
+}
33
+
34
+class ApiException implements Exception {
35
+ const ApiException(this.statusCode, this.code);
36
+ final int statusCode;
37
+ final String code;
38
+
39
+ @override
40
+ String toString() => 'ApiException($statusCode, $code)';
41
+}
42
+
43
+class HttpIdentityApi implements IdentityApi {
44
+ HttpIdentityApi({
45
+ String? baseUrl,
46
+ http.Client? client,
47
+ }) : baseUrl = baseUrl ??
48
+ const String.fromEnvironment(
49
+ 'LIFEMATE_API_URL',
50
+ defaultValue: 'http://localhost:8080',
51
+ ),
52
+ _client = client ?? http.Client();
53
+
54
+ final String baseUrl;
55
+ final http.Client _client;
56
+
57
+ @override
58
+ String? accessToken;
59
+ String? _refreshToken;
60
+
61
+ Uri _uri(String path) => Uri.parse('$baseUrl$path');
62
+
63
+ Map<String, String> _headers({bool auth = false}) => {
64
+ 'content-type': 'application/json',
65
+ if (auth && accessToken != null) 'authorization': 'Bearer $accessToken',
66
+ };
67
+
68
+ Future<Map<String, dynamic>> _json(
69
+ String method,
70
+ String path, {
71
+ Map<String, dynamic>? body,
72
+ bool auth = false,
73
+ bool retry = true,
74
+ }) async {
75
+ final request = http.Request(method, _uri(path))
76
+ ..headers.addAll(_headers(auth: auth));
77
+ if (body != null) request.body = jsonEncode(body);
78
+
79
+ final streamed = await _client.send(request);
80
+ final response = await http.Response.fromStream(streamed);
81
+
82
+ if (response.statusCode == 401 && auth && retry && _refreshToken != null) {
83
+ await _refresh();
84
+ return _json(method, path, body: body, auth: auth, retry: false);
85
+ }
86
+
87
+ if (response.statusCode < 200 || response.statusCode >= 300) {
88
+ var code = 'request_failed';
89
+ if (response.body.isNotEmpty) {
90
+ try {
91
+ code = (jsonDecode(response.body) as Map<String, dynamic>)['error']
92
+ ?.toString() ??
93
+ code;
94
+ } catch (_) {}
95
+ }
96
+ throw ApiException(response.statusCode, code);
97
+ }
98
+
99
+ if (response.body.isEmpty) return <String, dynamic>{};
100
+ return jsonDecode(response.body) as Map<String, dynamic>;
101
+ }
102
+
103
+ Future<void> _refresh() async {
104
+ final token = _refreshToken;
105
+ if (token == null) throw const ApiException(401, 'session_expired');
106
+ final result = await _json(
107
+ 'POST',
108
+ '/v1/auth/refresh',
109
+ body: {'refreshToken': token},
110
+ retry: false,
111
+ );
112
+ accessToken = result['accessToken']?.toString();
113
+ _refreshToken = result['refreshToken']?.toString();
114
+ }
115
+
116
+ @override
117
+ Future<void> register({
118
+ required String displayName,
119
+ required String email,
120
+ required String password,
121
+ }) async {
122
+ await _json('POST', '/v1/auth/register', body: {
123
+ 'displayName': displayName,
124
+ 'email': email,
125
+ 'password': password,
126
+ });
127
+ }
128
+
129
+ @override
130
+ Future<void> verifyEmail(String token) async {
131
+ await _json('POST', '/v1/auth/verify-email', body: {'token': token});
132
+ }
133
+
134
+ @override
135
+ Future<void> login(String email, String password) async {
136
+ final result = await _json('POST', '/v1/auth/login', body: {
137
+ 'email': email,
138
+ 'password': password,
139
+ });
140
+ accessToken = result['accessToken']?.toString();
141
+ _refreshToken = result['refreshToken']?.toString();
142
+ }
143
+
144
+ @override
145
+ Future<void> forgotPassword(String email) async {
146
+ await _json('POST', '/v1/auth/forgot-password', body: {'email': email});
147
+ }
148
+
149
+ @override
150
+ Future<void> resetPassword(String token, String newPassword) async {
151
+ await _json('POST', '/v1/auth/reset-password', body: {
152
+ 'token': token,
153
+ 'newPassword': newPassword,
154
+ });
155
+ }
156
+
157
+ @override
158
+ Future<void> changePassword(
159
+ String currentPassword,
160
+ String newPassword,
161
+ ) async {
162
+ await _json('POST', '/v1/auth/change-password', auth: true, body: {
163
+ 'currentPassword': currentPassword,
164
+ 'newPassword': newPassword,
165
+ });
166
+ }
167
+
168
+ @override
169
+ Future<Map<String, dynamic>> getProfile() =>
170
+ _json('GET', '/v1/profile', auth: true);
171
+
172
+ @override
173
+ Future<Map<String, dynamic>> updateProfile({
174
+ String? displayName,
175
+ String? birthDate,
176
+ String? themePreference,
177
+ }) =>
178
+ _json('PATCH', '/v1/profile', auth: true, body: {
179
+ if (displayName != null) 'displayName': displayName,
180
+ if (birthDate != null) 'birthDate': birthDate,
181
+ if (themePreference != null) 'themePreference': themePreference,
182
+ });
183
+
184
+ @override
185
+ Future<Map<String, dynamic>> createFamily(
186
+ String name, {
187
+ String? role,
188
+ }) =>
189
+ _json('POST', '/v1/families', auth: true, body: {
190
+ 'name': name,
191
+ if (role != null) 'role': role,
192
+ });
193
+
194
+ @override
195
+ Future<void> inviteMember({
196
+ required String familyId,
197
+ required String email,
198
+ required String role,
199
+ }) async {
200
+ await _json(
201
+ 'POST',
202
+ '/v1/families/$familyId/invitations',
203
+ auth: true,
204
+ body: {'email': email, 'role': role},
205
+ );
206
+ }
207
+
208
+ @override
209
+ Future<void> acceptInvitation(String token) async {
210
+ await _json(
211
+ 'POST',
212
+ '/v1/invitations/accept',
213
+ auth: true,
214
+ body: {'token': token},
215
+ );
216
+ }
217
+}