Skip to content

Commit

Permalink
fix(dogfooding): fixes to dogfooding auth flow (#800)
Browse files Browse the repository at this point in the history
* fixes to dogfooding auth flow

* tweaks
  • Loading branch information
Brazol authored Nov 26, 2024
1 parent 3e2fe07 commit 2ca7cdd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 32 deletions.
18 changes: 9 additions & 9 deletions dogfooding/lib/core/repos/user_auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class UserAuthRepository {

Future<UserCredentials> login() async {
final response = await videoClient.connect();
final userToken = response.getDataOrNull();
if (userToken == null) {
throw Exception('Failed to connect user');
}

return UserCredentials(
token: userToken,
userInfo: currentUser,
);

return response.fold(success: (success) {
return UserCredentials(
token: success.data,
userInfo: currentUser,
);
}, failure: (failure) {
throw failure.error;
});
}

UserInfo get currentUser => videoClient.currentUser;
Expand Down
40 changes: 27 additions & 13 deletions dogfooding/lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,40 @@ class _HomeScreenState extends State<HomeScreen> {
bool isRinging = memberIds.isNotEmpty;

try {
await _call!.getOrCreate(
final result = await _call!.getOrCreate(
memberIds: memberIds,
ringing: isRinging,
video: true,
);

result.fold(
success: (success) {
if (mounted) {
if (isRinging) {
CallRoute($extra: (
call: _call!,
connectOptions: null,
)).push(context);
} else {
LobbyRoute($extra: _call!).push(context);
}
}
},
failure: (failure) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(seconds: 20),
content: Text('Error: ${failure.error.message}'),
),
);
},
);
} catch (e, stk) {
debugPrint('Error joining or creating call: $e');
debugPrint(stk.toString());
}

if (mounted) {
hideLoadingIndicator(context);

if (isRinging) {
CallRoute($extra: (
call: _call!,
connectOptions: null,
)).push(context);
} else {
LobbyRoute($extra: _call!).push(context);
} finally {
if (mounted) {
hideLoadingIndicator(context);
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions dogfooding/lib/screens/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class _LoginScreenState extends State<LoginScreen> {

final userInfo = UserInfo(
role: 'admin',
id: googleUser.email,
id: createValidId(googleUser.email),
name: googleUser.displayName ?? '',
image: googleUser.photoUrl,
);
Expand All @@ -56,7 +56,7 @@ class _LoginScreenState extends State<LoginScreen> {

final userInfo = UserInfo(
role: 'admin',
id: email.replaceAll('@', '_').replaceAll('.', '_'),
id: createValidId(email),
name: email,
);

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

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

if (mounted) hideLoadingIndicator(context);
try {
await authController.login(user, environment);
} catch (e, _) {
if (mounted) {
hideLoadingIndicator(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(seconds: 20),
content: Text('Error: $e'),
),
);
}
}
}

@override
Expand Down Expand Up @@ -268,3 +279,7 @@ String randomId({int size = 21}) {
}
return id;
}

String createValidId(String id) {
return id.replaceAll(RegExp(r'[^\w]'), '_');
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:tart/tart.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import '../../stream_video.dart';
import 'video_error.dart';

/// TODO
Expand All @@ -24,6 +25,12 @@ mixin VideoErrors {
cause: exception,
stackTrace: stackTrace,
);
} else if (exception is ApiException) {
return VideoErrorWithCause(
message: exception.message ?? exception.toString(),
cause: exception,
stackTrace: stackTrace,
);
} else if (exception is Exception) {
return VideoErrorWithCause(
message: exception.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class PushNotificationManager {

/// Unregisters the device for push notifications. Internal use only.
@internal
void unregisterDevice();
Future<void> unregisterDevice();

/// Displays an incoming call notification.
///
Expand Down
7 changes: 4 additions & 3 deletions packages/stream_video/lib/src/stream_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,12 @@ class StreamVideo extends Disposable {
}
try {
await _connectOperation?.cancel();
await _client.disconnectUser();
_subscriptions.cancelAll();

// Unregister device from push notification manager.
pushNotificationManager?.unregisterDevice();
await pushNotificationManager?.unregisterDevice();

await _client.disconnectUser();
_subscriptions.cancelAll();

// Resetting the state.
await _state.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
}

@override
void unregisterDevice() async {
Future<void> unregisterDevice() async {
final token = await getDevicePushTokenVoIP();
if (token != null) {
_client.deleteDevice(id: token);
Expand All @@ -272,7 +272,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
_client.deleteDevice(id: apnToken);
}

removedStoredTokens();
await removedStoredTokens();
}

@override
Expand Down

0 comments on commit 2ca7cdd

Please sign in to comment.