+signup +login
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle 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
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle 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
This commit is contained in:
@@ -4,21 +4,48 @@ 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/provider/exception.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/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;
|
||||
|
||||
Account? get account => _resource.data;
|
||||
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) {
|
||||
@@ -26,16 +53,24 @@ class AccountProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateProvider(LocaleProvider localeProvider) => _localeProvider = localeProvider;
|
||||
|
||||
Future<Account?> login({
|
||||
void _pickupLocale(String locale) => _localeProvider.setLocale(Locale(locale));
|
||||
|
||||
Future<Account> login({
|
||||
required String email,
|
||||
required String password,
|
||||
required String locale,
|
||||
}) async {
|
||||
_setResource(_resource.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final acc = await AccountService.login(email, password, locale);
|
||||
final acc = await AccountService.login(LoginData.build(
|
||||
login: email,
|
||||
password: password,
|
||||
locale: locale,
|
||||
));
|
||||
_setResource(Resource(data: acc, isLoading: false));
|
||||
_pickupLocale(acc.locale);
|
||||
return acc;
|
||||
} catch (e) {
|
||||
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
|
||||
@@ -43,11 +78,14 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
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)));
|
||||
@@ -55,24 +93,20 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signup(
|
||||
String name,
|
||||
String login,
|
||||
String password,
|
||||
String locale,
|
||||
String organizationName,
|
||||
String timezone,
|
||||
) async {
|
||||
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(
|
||||
name: name,
|
||||
login: login.trim().toLowerCase(),
|
||||
password: password,
|
||||
locale: locale,
|
||||
organizationName: organizationName,
|
||||
account: account,
|
||||
organization: organization,
|
||||
organizationTimeZone: timezone,
|
||||
ownerRole: ownerRole,
|
||||
),
|
||||
);
|
||||
// Signup might not automatically log in the user,
|
||||
@@ -96,6 +130,7 @@ class AccountProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<Account?> update({
|
||||
Describable? describable,
|
||||
String? locale,
|
||||
String? avatarUrl,
|
||||
String? notificationFrequency,
|
||||
@@ -105,6 +140,7 @@ class AccountProvider extends ChangeNotifier {
|
||||
try {
|
||||
final updated = await AccountService.update(
|
||||
account!.copyWith(
|
||||
describable: describable,
|
||||
avatarUrl: () => avatarUrl ?? account!.avatarUrl,
|
||||
locale: locale ?? account!.locale,
|
||||
),
|
||||
@@ -141,4 +177,26 @@ class AccountProvider extends ChangeNotifier {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user