Initial commit — DLS lab-app Flutter project
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import 'package:pocketbase/pocketbase.dart';
|
||||
import '../api/pocketbase_client.dart';
|
||||
import '../../models/tenant.dart';
|
||||
import '../../models/user_profile.dart';
|
||||
|
||||
class AuthRepository {
|
||||
AuthRepository._();
|
||||
static final instance = AuthRepository._();
|
||||
|
||||
PocketBase get _pb => PocketBaseClient.instance.pb;
|
||||
|
||||
Future<AuthResult> login(String email, String password) async {
|
||||
await _pb.collection('users').authWithPassword(email, password);
|
||||
return _buildAuthResult();
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
_pb.authStore.clear();
|
||||
}
|
||||
|
||||
Future<bool> isLoggedIn() async {
|
||||
if (!_pb.authStore.isValid) return false;
|
||||
try {
|
||||
await _pb.collection('users').authRefresh();
|
||||
return true;
|
||||
} catch (_) {
|
||||
_pb.authStore.clear();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<AuthResult> register({
|
||||
required String email,
|
||||
required String password,
|
||||
String? firstName,
|
||||
String? lastName,
|
||||
}) async {
|
||||
await _pb.collection('users').create(body: {
|
||||
'email': email,
|
||||
'password': password,
|
||||
'passwordConfirm': password,
|
||||
'emailVisibility': true,
|
||||
if (firstName != null && firstName.isNotEmpty) 'first_name': firstName,
|
||||
if (lastName != null && lastName.isNotEmpty) 'last_name': lastName,
|
||||
});
|
||||
return login(email, password);
|
||||
}
|
||||
|
||||
Future<AuthResult> refreshSession() async {
|
||||
try {
|
||||
await _pb.collection('users').authRefresh();
|
||||
} catch (_) {}
|
||||
return _buildAuthResult();
|
||||
}
|
||||
|
||||
Future<void> updateUserLanguage(String userId, String languageCode) async {
|
||||
await _pb.collection('users').update(userId, body: {
|
||||
'preferred_language': languageCode,
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateTenant(
|
||||
String id, {
|
||||
String? companyName,
|
||||
String? defaultCurrency,
|
||||
}) async {
|
||||
final body = <String, dynamic>{};
|
||||
if (companyName != null) body['company_name'] = companyName;
|
||||
if (defaultCurrency != null) body['default_currency'] = defaultCurrency;
|
||||
if (body.isEmpty) return;
|
||||
await _pb.collection('tenants').update(id, body: body);
|
||||
}
|
||||
|
||||
Future<AuthResult> _buildAuthResult() async {
|
||||
final record = _pb.authStore.record!;
|
||||
final user = UserProfile.fromJson(record.toJson());
|
||||
List<TenantMembership> tenants = [];
|
||||
try {
|
||||
tenants = await _fetchUserTenants(record.id);
|
||||
} catch (_) {}
|
||||
return AuthResult(user: user, tenants: tenants);
|
||||
}
|
||||
|
||||
Future<List<TenantMembership>> _fetchUserTenants(String userId) async {
|
||||
final result = await _pb.collection('tenant_members').getList(
|
||||
filter: 'user_id = "$userId"',
|
||||
expand: 'tenant_id',
|
||||
perPage: 50,
|
||||
);
|
||||
return result.items
|
||||
.map((r) => TenantMembership.fromJson(r.toJson()))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
class AuthResult {
|
||||
const AuthResult({required this.user, required this.tenants});
|
||||
final UserProfile user;
|
||||
final List<TenantMembership> tenants;
|
||||
}
|
||||
Reference in New Issue
Block a user