Files
sendico/frontend/pshared/lib/provider/account.dart
Stephan D 26a1e284b2
Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
Rewired login confirmation
2025-11-25 00:46:11 +01:00

224 lines
7.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:pshared/api/errors/unauthorized.dart';
import 'package:pshared/api/requests/signup.dart';
import 'package:pshared/api/requests/login_data.dart';
import 'package:pshared/config/constants.dart';
import 'package:pshared/models/account/account.dart';
import 'package:pshared/models/auth/login_outcome.dart';
import 'package:pshared/models/auth/pending_login.dart';
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/storable.dart';
import 'package:pshared/provider/locale.dart';
import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/account.dart';
import 'package:pshared/service/authorization/service.dart';
import 'package:pshared/service/verification.dart';
import 'package:pshared/utils/exception.dart';
class AccountProvider extends ChangeNotifier {
static String get currentUserRef => Constants.nilObjectRef;
// The resource now wraps our Account? state along with its loading/error state.
Resource<Account?> _resource = Resource(data: null);
Resource<Account?> get resource => _resource;
late LocaleProvider _localeProvider;
PendingLogin? _pendingLogin;
Account? get account => _resource.data;
PendingLogin? get pendingLogin => _pendingLogin;
bool get isLoggedIn => account != null;
bool get isLoading => _resource.isLoading;
Object? get error => _resource.error;
bool get isReady => (!isLoading) && (account != null);
Account? currentUser() {
final acc = account;
if (acc == null) return null;
return Account(
storable: newStorable(
id: currentUserRef,
createdAt: acc.createdAt,
updatedAt: acc.updatedAt,
),
describable: acc.describable,
lastName: acc.lastName,
avatarUrl: acc.avatarUrl,
login: acc.login,
locale: acc.locale,
);
}
// Private helper to update the resource and notify listeners.
void _setResource(Resource<Account?> newResource) {
_resource = newResource;
notifyListeners();
}
void updateProvider(LocaleProvider localeProvider) => _localeProvider = localeProvider;
void _pickupLocale(String locale) => _localeProvider.setLocale(Locale(locale));
Future<LoginOutcome> login({
required String email,
required String password,
required String locale,
}) async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final outcome = await AccountService.login(LoginData.build(
login: email,
password: password,
locale: locale,
));
if (outcome.account != null) {
_setResource(Resource(data: outcome.account, isLoading: false));
_pickupLocale(outcome.account!.locale);
} else {
final pending = outcome.pending;
if (pending == null) {
throw Exception('Pending login data is missing');
}
await VerificationService.requestLoginCode(pending);
_pendingLogin = pending;
_setResource(_resource.copyWith(isLoading: false));
}
return outcome;
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
void completePendingLogin(Account account) {
_pendingLogin = null;
_setResource(Resource(data: account, isLoading: false, error: null));
_pickupLocale(account.locale);
}
Future<bool> isAuthorizationStored() async => AuthorizationService.isAuthorizationStored();
Future<Account?> restore() async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final acc = await AccountService.restore();
_setResource(Resource(data: acc, isLoading: false));
_pickupLocale(acc.locale);
return acc;
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<void> signup({
required AccountData account,
required Describable organization,
required String timezone,
required Describable ownerRole,
}) async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
await AccountService.signup(
SignupRequest.build(
account: account,
organization: organization,
organizationTimeZone: timezone,
ownerRole: ownerRole,
),
);
// Signup might not automatically log in the user,
// so we just mark the request as complete.
_setResource(_resource.copyWith(isLoading: false));
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<void> logout() async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
await AccountService.logout();
_setResource(Resource(data: null, isLoading: false));
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<Account?> update({
Describable? describable,
String? locale,
String? avatarUrl,
String? notificationFrequency,
}) async {
if (account == null) throw ErrorUnauthorized();
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final updated = await AccountService.update(
account!.copyWith(
describable: describable,
avatarUrl: () => avatarUrl ?? account!.avatarUrl,
locale: locale ?? account!.locale,
),
);
_setResource(Resource(data: updated, isLoading: false));
return updated;
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<void> changePassword(String oldPassword, String newPassword) async {
if (account == null) throw ErrorUnauthorized();
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final updated = await AccountService.changePassword(oldPassword, newPassword);
_setResource(Resource(data: updated, isLoading: false));
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<Account?> uploadAvatar(XFile avatarFile) async {
if (account == null) throw ErrorUnauthorized();
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final avatarUrl = await AccountService.uploadAvatar(account!.id, avatarFile);
// Reuse the update method to update the avatar URL.
return update(avatarUrl: avatarUrl);
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<void> forgotPassword(String email) async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
await AccountService.forgotPassword(email);
_setResource(_resource.copyWith(isLoading: false));
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<void> resetPassword(String accountId, String token, String newPassword) async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
await AccountService.resetPassword(accountId, token, newPassword);
_setResource(_resource.copyWith(isLoading: false));
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
}