Skip to content

Commit 2ca7cdd

Browse files
authored
fix(dogfooding): fixes to dogfooding auth flow (#800)
* fixes to dogfooding auth flow * tweaks
1 parent 3e2fe07 commit 2ca7cdd

File tree

7 files changed

+69
-32
lines changed

7 files changed

+69
-32
lines changed

dogfooding/lib/core/repos/user_auth_repository.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class UserAuthRepository {
1616

1717
Future<UserCredentials> login() async {
1818
final response = await videoClient.connect();
19-
final userToken = response.getDataOrNull();
20-
if (userToken == null) {
21-
throw Exception('Failed to connect user');
22-
}
23-
24-
return UserCredentials(
25-
token: userToken,
26-
userInfo: currentUser,
27-
);
19+
20+
return response.fold(success: (success) {
21+
return UserCredentials(
22+
token: success.data,
23+
userInfo: currentUser,
24+
);
25+
}, failure: (failure) {
26+
throw failure.error;
27+
});
2828
}
2929

3030
UserInfo get currentUser => videoClient.currentUser;

dogfooding/lib/screens/home_screen.dart

+27-13
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,40 @@ class _HomeScreenState extends State<HomeScreen> {
6969
bool isRinging = memberIds.isNotEmpty;
7070

7171
try {
72-
await _call!.getOrCreate(
72+
final result = await _call!.getOrCreate(
7373
memberIds: memberIds,
7474
ringing: isRinging,
7575
video: true,
7676
);
77+
78+
result.fold(
79+
success: (success) {
80+
if (mounted) {
81+
if (isRinging) {
82+
CallRoute($extra: (
83+
call: _call!,
84+
connectOptions: null,
85+
)).push(context);
86+
} else {
87+
LobbyRoute($extra: _call!).push(context);
88+
}
89+
}
90+
},
91+
failure: (failure) {
92+
ScaffoldMessenger.of(context).showSnackBar(
93+
SnackBar(
94+
duration: const Duration(seconds: 20),
95+
content: Text('Error: ${failure.error.message}'),
96+
),
97+
);
98+
},
99+
);
77100
} catch (e, stk) {
78101
debugPrint('Error joining or creating call: $e');
79102
debugPrint(stk.toString());
80-
}
81-
82-
if (mounted) {
83-
hideLoadingIndicator(context);
84-
85-
if (isRinging) {
86-
CallRoute($extra: (
87-
call: _call!,
88-
connectOptions: null,
89-
)).push(context);
90-
} else {
91-
LobbyRoute($extra: _call!).push(context);
103+
} finally {
104+
if (mounted) {
105+
hideLoadingIndicator(context);
92106
}
93107
}
94108
}

dogfooding/lib/screens/login_screen.dart

+19-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class _LoginScreenState extends State<LoginScreen> {
4242

4343
final userInfo = UserInfo(
4444
role: 'admin',
45-
id: googleUser.email,
45+
id: createValidId(googleUser.email),
4646
name: googleUser.displayName ?? '',
4747
image: googleUser.photoUrl,
4848
);
@@ -56,7 +56,7 @@ class _LoginScreenState extends State<LoginScreen> {
5656

5757
final userInfo = UserInfo(
5858
role: 'admin',
59-
id: email.replaceAll('@', '_').replaceAll('.', '_'),
59+
id: createValidId(email),
6060
name: email,
6161
);
6262

@@ -82,9 +82,20 @@ class _LoginScreenState extends State<LoginScreen> {
8282

8383
// Register StreamVideo client with the user.
8484
final authController = locator.get<UserAuthController>();
85-
await authController.login(user, environment);
8685

87-
if (mounted) hideLoadingIndicator(context);
86+
try {
87+
await authController.login(user, environment);
88+
} catch (e, _) {
89+
if (mounted) {
90+
hideLoadingIndicator(context);
91+
ScaffoldMessenger.of(context).showSnackBar(
92+
SnackBar(
93+
duration: const Duration(seconds: 20),
94+
content: Text('Error: $e'),
95+
),
96+
);
97+
}
98+
}
8899
}
89100

90101
@override
@@ -268,3 +279,7 @@ String randomId({int size = 21}) {
268279
}
269280
return id;
270281
}
282+
283+
String createValidId(String id) {
284+
return id.replaceAll(RegExp(r'[^\w]'), '_');
285+
}

packages/stream_video/lib/src/errors/video_error_composer.dart

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:tart/tart.dart';
22
import 'package:web_socket_channel/web_socket_channel.dart';
33

4+
import '../../stream_video.dart';
45
import 'video_error.dart';
56

67
/// TODO
@@ -24,6 +25,12 @@ mixin VideoErrors {
2425
cause: exception,
2526
stackTrace: stackTrace,
2627
);
28+
} else if (exception is ApiException) {
29+
return VideoErrorWithCause(
30+
message: exception.message ?? exception.toString(),
31+
cause: exception,
32+
stackTrace: stackTrace,
33+
);
2734
} else if (exception is Exception) {
2835
return VideoErrorWithCause(
2936
message: exception.toString(),

packages/stream_video/lib/src/push_notification/push_notification_manager.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class PushNotificationManager {
2626

2727
/// Unregisters the device for push notifications. Internal use only.
2828
@internal
29-
void unregisterDevice();
29+
Future<void> unregisterDevice();
3030

3131
/// Displays an incoming call notification.
3232
///

packages/stream_video/lib/src/stream_video.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,12 @@ class StreamVideo extends Disposable {
368368
}
369369
try {
370370
await _connectOperation?.cancel();
371-
await _client.disconnectUser();
372-
_subscriptions.cancelAll();
373371

374372
// Unregister device from push notification manager.
375-
pushNotificationManager?.unregisterDevice();
373+
await pushNotificationManager?.unregisterDevice();
374+
375+
await _client.disconnectUser();
376+
_subscriptions.cancelAll();
376377

377378
// Resetting the state.
378379
await _state.clear();

packages/stream_video_push_notification/lib/src/stream_video_push_notification.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
260260
}
261261

262262
@override
263-
void unregisterDevice() async {
263+
Future<void> unregisterDevice() async {
264264
final token = await getDevicePushTokenVoIP();
265265
if (token != null) {
266266
_client.deleteDevice(id: token);
@@ -272,7 +272,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
272272
_client.deleteDevice(id: apnToken);
273273
}
274274

275-
removedStoredTokens();
275+
await removedStoredTokens();
276276
}
277277

278278
@override

0 commit comments

Comments
 (0)