Skip to content

Commit d5b869c

Browse files
committed
fix(auth): revert changes to non-trezor sign-in and register methods
1 parent 321aae4 commit d5b869c

File tree

1 file changed

+29
-83
lines changed

1 file changed

+29
-83
lines changed

lib/bloc/auth_bloc/auth_bloc.dart

Lines changed: 29 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
8787

8888
final weakPasswordsAllowed = await _areWeakPasswordsAllowed();
8989

90-
final authStream = _kdfSdk.auth.signInStream(
90+
await _kdfSdk.auth.signIn(
9191
walletName: event.wallet.name,
9292
password: event.password,
9393
options: AuthOptions(
@@ -97,28 +97,14 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
9797
allowWeakPassword: weakPasswordsAllowed,
9898
),
9999
);
100-
101-
await for (final authState in authStream) {
102-
if (authState.status == AuthenticationStatus.completed) {
103-
final user = authState.user;
104-
if (user != null) {
105-
_log.info('logged in from a wallet');
106-
emit(AuthBlocState.loggedIn(user));
107-
_listenToAuthStateChanges();
108-
} else {
109-
emit(AuthBlocState.error(AuthException.notSignedIn()));
110-
}
111-
break;
112-
} else if (authState.status == AuthenticationStatus.error) {
113-
emit(AuthBlocState.error(
114-
AuthException(authState.message ?? 'Login failed',
115-
type: AuthExceptionType.generalAuthError),
116-
));
117-
break;
118-
} else {
119-
emit(AuthBlocState.loading());
120-
}
100+
final KdfUser? currentUser = await _kdfSdk.auth.currentUser;
101+
if (currentUser == null) {
102+
return emit(AuthBlocState.error(AuthException.notSignedIn()));
121103
}
104+
105+
_log.info('logged in from a wallet');
106+
emit(AuthBlocState.loggedIn(currentUser));
107+
_listenToAuthStateChanges();
122108
} catch (e, s) {
123109
if (e is AuthException) {
124110
// Preserve the original error type for specific errors like incorrect password
@@ -171,7 +157,7 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
171157

172158
final weakPasswordsAllowed = await _areWeakPasswordsAllowed();
173159

174-
final authStream = _kdfSdk.auth.registerStream(
160+
await _kdfSdk.auth.register(
175161
password: event.password,
176162
walletName: event.wallet.name,
177163
options: AuthOptions(
@@ -182,33 +168,17 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
182168
),
183169
);
184170

185-
await for (final authState in authStream) {
186-
if (authState.status == AuthenticationStatus.completed) {
187-
final user = authState.user;
188-
if (user != null) {
189-
_log.info('registered from a wallet');
190-
await _kdfSdk.setWalletType(event.wallet.config.type);
191-
await _kdfSdk.confirmSeedBackup(hasBackup: false);
192-
await _kdfSdk.addActivatedCoins(enabledByDefaultCoins);
193-
emit(AuthBlocState.loggedIn(user));
194-
_listenToAuthStateChanges();
195-
} else {
196-
emit(AuthBlocState.error(
197-
AuthException('Registration failed',
198-
type: AuthExceptionType.generalAuthError),
199-
));
200-
}
201-
break;
202-
} else if (authState.status == AuthenticationStatus.error) {
203-
emit(AuthBlocState.error(
204-
AuthException(authState.message ?? 'Registration failed',
205-
type: AuthExceptionType.generalAuthError),
206-
));
207-
break;
208-
} else {
209-
emit(AuthBlocState.loading());
210-
}
171+
_log.info('registered from a wallet');
172+
await _kdfSdk.setWalletType(event.wallet.config.type);
173+
await _kdfSdk.confirmSeedBackup(hasBackup: false);
174+
await _kdfSdk.addActivatedCoins(enabledByDefaultCoins);
175+
176+
final currentUser = await _kdfSdk.auth.currentUser;
177+
if (currentUser == null) {
178+
throw Exception('Registration failed: user is not signed in');
211179
}
180+
emit(AuthBlocState.loggedIn(currentUser));
181+
_listenToAuthStateChanges();
212182
} catch (e, s) {
213183
final errorMsg = 'Failed to register wallet ${event.wallet.name}';
214184
_log.shout(errorMsg, e, s);
@@ -235,7 +205,7 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
235205

236206
final weakPasswordsAllowed = await _areWeakPasswordsAllowed();
237207

238-
final authStream = _kdfSdk.auth.registerStream(
208+
await _kdfSdk.auth.register(
239209
password: event.password,
240210
walletName: event.wallet.name,
241211
mnemonic: Mnemonic.plaintext(event.seed),
@@ -247,40 +217,16 @@ class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> with TrezorAuthMixin {
247217
),
248218
);
249219

250-
await for (final authState in authStream) {
251-
if (authState.status == AuthenticationStatus.completed) {
252-
final user = authState.user;
253-
if (user != null) {
254-
_log.info('restored from a wallet');
255-
await _kdfSdk.setWalletType(event.wallet.config.type);
256-
await _kdfSdk.confirmSeedBackup(
257-
hasBackup: event.wallet.config.hasBackup);
258-
await _kdfSdk.addActivatedCoins(enabledByDefaultCoins);
259-
emit(AuthBlocState.loggedIn(user));
260-
261-
if (event.wallet.isLegacyWallet) {
262-
await _kdfSdk
263-
.addActivatedCoins(event.wallet.config.activatedCoins);
264-
await _walletsRepository.deleteWallet(event.wallet);
265-
}
266-
_listenToAuthStateChanges();
267-
} else {
268-
emit(AuthBlocState.error(
269-
AuthException('Restoration failed',
270-
type: AuthExceptionType.generalAuthError),
271-
));
272-
}
273-
break;
274-
} else if (authState.status == AuthenticationStatus.error) {
275-
emit(AuthBlocState.error(
276-
AuthException(authState.message ?? 'Restoration failed',
277-
type: AuthExceptionType.generalAuthError),
278-
));
279-
break;
280-
} else {
281-
emit(AuthBlocState.loading());
282-
}
220+
_log.info('restored from a wallet');
221+
await _kdfSdk.setWalletType(event.wallet.config.type);
222+
await _kdfSdk.confirmSeedBackup(hasBackup: event.wallet.config.hasBackup);
223+
await _kdfSdk.addActivatedCoins(enabledByDefaultCoins);
224+
225+
final currentUser = await _kdfSdk.auth.currentUser;
226+
if (currentUser == null) {
227+
throw Exception('Registration failed: user is not signed in');
283228
}
229+
emit(AuthBlocState.loggedIn(currentUser));
284230

285231
// Delete legacy wallet on successful restoration & login to avoid
286232
// duplicates in the wallet list

0 commit comments

Comments
 (0)