Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to join room using auth token #1720

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/hms_room_kit/lib/src/common/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class Constant {
"https://public.app.100ms.live/meeting/xvm-wxwo-gbl";

/// [Constant.roomCode] is the meeting code that will be used
static String roomCode = "";
static String? roomCode = "";

///[Constant.authToken] is auth token to join the room
static String? authToken = "";

/// [Constant.streamingUrl] is the streaming url that will be used
static String streamingUrl = "";
Expand Down
19 changes: 16 additions & 3 deletions packages/hms_room_kit/lib/src/hms_prebuilt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class HMSPrebuilt extends StatelessWidget {
///
/// Example: For the public Room: https://public.app.100ms.live/meeting/xvm-wxwo-gbl
/// The room code is: xvm-wxwo-gbl
final String roomCode;
final String? roomCode;

///[token]: The auth token to join the room
final String? authToken;

///The options for the prebuilt
///For more details checkout the [HMSPrebuiltOptions] class
Expand All @@ -28,8 +31,17 @@ class HMSPrebuilt extends StatelessWidget {
final Function? onLeave;

///The key for the widget
const HMSPrebuilt(
{super.key, required this.roomCode, this.options, this.onLeave});
HMSPrebuilt(
{super.key,
required this.roomCode,
this.options,
this.onLeave,
this.authToken}) {
if (roomCode == null && authToken == null) {
throw ArgumentError.notNull(
"At least one parameter roomCode or authToken must be provided.");
}
}

///Builds the widget
///Returns a [ScreenController] widget
Expand All @@ -45,6 +57,7 @@ class HMSPrebuilt extends StatelessWidget {
Widget build(BuildContext context) {
return ScreenController(
roomCode: roomCode,
authToken: authToken,
options: options,
onLeave: onLeave,
);
Expand Down
7 changes: 1 addition & 6 deletions packages/hms_room_kit/lib/src/meeting/meeting_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,13 @@ import 'package:hms_room_kit/src/widgets/common_widgets/hms_left_room_screen.dar

///[MeetingPage] is the main page of the meeting
///It takes the following parameters:
///[meetingLink] is the link of the meeting
///[isRoomMute] is the flag to mute the room
class MeetingPage extends StatefulWidget {
final String meetingLink;
final bool isRoomMute;
final HMSAudioDevice currentAudioDeviceMode;

const MeetingPage(
{Key? key,
required this.meetingLink,
this.isRoomMute = true,
required this.currentAudioDeviceMode})
{Key? key, this.isRoomMute = true, required this.currentAudioDeviceMode})
: super(key: key);

@override
Expand Down
19 changes: 11 additions & 8 deletions packages/hms_room_kit/lib/src/meeting/meeting_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ class MeetingStore extends ChangeNotifier

bool isMessageInfoShown = true;

String meetingUrl = "";
bool isAudioShareStarted = false;

List<HMSAudioDevice> availableAudioOutputDevices = [];
Expand Down Expand Up @@ -263,17 +262,22 @@ class MeetingStore extends ChangeNotifier
///List of bottom sheets currently open
List<BuildContext> bottomSheets = [];

Future<HMSException?> join(String userName, String roomCode,
{HMSConfig? roomConfig}) async {
Future<HMSException?> join(String userName, {HMSConfig? roomConfig}) async {
//If roomConfig is null then only we call the methods to get the authToken
//If we are joining the room from preview we already have authToken so we don't
//need to call the getAuthTokenByRoomCode method
if (roomConfig == null) {
//We use this to get the auth token from room code
dynamic tokenData = await _hmsSDKInteractor.getAuthTokenByRoomCode(
userId: Constant.prebuiltOptions?.userId,
roomCode: Constant.roomCode,
endPoint: Constant.tokenEndPoint);
dynamic tokenData;

if (Constant.roomCode != null) {
await _hmsSDKInteractor.getAuthTokenByRoomCode(
userId: Constant.prebuiltOptions?.userId,
roomCode: Constant.roomCode!,
endPoint: Constant.tokenEndPoint);
} else {
tokenData = Constant.authToken;
}

///If the tokenData is String then we set the authToken in the roomConfig
///and then we join the room
Expand All @@ -300,7 +304,6 @@ class MeetingStore extends ChangeNotifier
setMeetingModeUsingLayoutApi();
_hmsSDKInteractor.join(config: roomConfig);
setRecipientSelectorValue();
meetingUrl = roomCode;
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import 'package:hms_room_kit/src/meeting/meeting_store.dart';
///[MeetingScreenController] is the controller for the meeting screen
///It is used to join the room
class MeetingScreenController extends StatefulWidget {
///[roomCode] is the room code of the room to join
final String roomCode;

///[user] is the name of the user joining the room
final String user;

Expand Down Expand Up @@ -48,7 +45,6 @@ class MeetingScreenController extends StatefulWidget {

const MeetingScreenController(
{Key? key,
required this.roomCode,
required this.user,
required this.localPeerNetworkQuality,
this.isRoomMute = false,
Expand Down Expand Up @@ -97,7 +93,6 @@ class _MeetingScreenControllerState extends State<MeetingScreenController> {
value: _hlsPlayerStore, child: const HLSViewerPage());
}
return MeetingPage(
meetingLink: widget.roomCode,
isRoomMute: widget.isRoomMute,
currentAudioDeviceMode: widget.currentAudioDeviceMode,
);
Expand Down
29 changes: 13 additions & 16 deletions packages/hms_room_kit/lib/src/preview/preview_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ import 'package:hms_room_kit/src/meeting/meeting_store.dart';
///This renders the Preview Screen
class PreviewPage extends StatefulWidget {
final String name;
final String roomCode;
final HMSPrebuiltOptions? options;

const PreviewPage(
{super.key,
required this.name,
required this.roomCode,
required this.options});
const PreviewPage({super.key, required this.name, required this.options});
@override
State<PreviewPage> createState() => _PreviewPageState();
}
Expand Down Expand Up @@ -77,8 +72,7 @@ class _PreviewPageState extends State<PreviewPage> {
_setMeetingStore(previewStore);

/// We join the room here
HMSException? ans = await _meetingStore.join(
nameController.text.trim(), Constant.roomCode);
HMSException? ans = await _meetingStore.join(nameController.text.trim());

///If the room join fails we show the error dialog
if (ans != null && mounted) {
Expand Down Expand Up @@ -149,17 +143,21 @@ class _PreviewPageState extends State<PreviewPage> {
previewStore.isMeetingJoined = false,
previewStore.hmsSDKInteractor.leave(),
HMSThemeColors.resetLayoutColors(),
Navigator.pushReplacement(
context,
CupertinoPageRoute(
builder: (_) => ScreenController(
roomCode: Constant.roomCode,
options: widget.options,
))),
navigateBack()
}
});
}

void navigateBack() {
Navigator.pushReplacement(
context,
CupertinoPageRoute(
builder: (_) => ScreenController(
roomCode: Constant.roomCode,
options: widget.options,
)));
}

@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
Expand Down Expand Up @@ -188,7 +186,6 @@ class _PreviewPageState extends State<PreviewPage> {
value: _meetingStore,
child: MeetingScreenController(
role: previewStore.peer?.role,
roomCode: Constant.roomCode,
localPeerNetworkQuality: null,
user: nameController.text,
isRoomMute: previewStore.isRoomMute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ import 'package:hms_room_kit/src/widgets/hms_buttons/hms_back_button.dart';

///This renders the preview permissions screen
class PreviewPermissions extends StatefulWidget {
final String roomCode;
final HMSPrebuiltOptions? options;
final void Function() callback;

const PreviewPermissions(
{super.key,
required this.roomCode,
this.options,
required this.callback});
const PreviewPermissions({super.key, this.options, required this.callback});

@override
State<PreviewPermissions> createState() => _PreviewPermissionsState();
Expand Down
20 changes: 11 additions & 9 deletions packages/hms_room_kit/lib/src/preview/preview_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class PreviewStore extends ChangeNotifier

int? networkQuality;

String meetingUrl = "";

bool isRoomMute = false;

List<HMSRole> roles = [];
Expand Down Expand Up @@ -108,13 +106,18 @@ class PreviewStore extends ChangeNotifier
notifyListeners();
}

Future<HMSException?> startPreview(
{required String userName, required String roomCode}) async {
Future<HMSException?> startPreview({required String userName}) async {
//We use this to get the auth token from room code
dynamic tokenData = await hmsSDKInteractor.getAuthTokenByRoomCode(
userId: Constant.prebuiltOptions?.userId,
roomCode: roomCode,
endPoint: Constant.tokenEndPoint);

dynamic tokenData;
if (Constant.roomCode != null) {
tokenData = await hmsSDKInteractor.getAuthTokenByRoomCode(
userId: Constant.prebuiltOptions?.userId,
roomCode: Constant.roomCode!,
endPoint: Constant.tokenEndPoint);
} else {
tokenData = Constant.authToken;
}

if ((tokenData is String?) && tokenData != null) {
roomConfig = HMSConfig(
Expand All @@ -133,7 +136,6 @@ class PreviewStore extends ChangeNotifier
Constant.webRTCLogLevel, Constant.sdkLogLevel);
hmsSDKInteractor.addPreviewListener(this);
hmsSDKInteractor.preview(config: roomConfig!);
meetingUrl = roomCode;
return null;
}
return tokenData;
Expand Down
16 changes: 11 additions & 5 deletions packages/hms_room_kit/lib/src/screen_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import 'package:hms_room_kit/src/preview/preview_store.dart';
///For more details checkout the [HMSPrebuiltOptions] class
class ScreenController extends StatefulWidget {
///[roomCode] is the room code of the room to join
final String roomCode;
final String? roomCode;

///[token] auth token to join the room
final String? authToken;

///[options] is the options for the prebuilt
///For more details checkout the [HMSPrebuiltOptions] class
Expand All @@ -30,7 +33,11 @@ class ScreenController extends StatefulWidget {
final Function? onLeave;

const ScreenController(
{super.key, required this.roomCode, this.options, this.onLeave});
{super.key,
required this.roomCode,
this.options,
this.onLeave,
this.authToken});
@override
State<ScreenController> createState() => _ScreenControllerState();
}
Expand All @@ -48,6 +55,7 @@ class _ScreenControllerState extends State<ScreenController> {
///Setting the prebuilt options and roomCode
Constant.prebuiltOptions = widget.options;
Constant.roomCode = widget.roomCode;
Constant.authToken = widget.authToken;
Constant.onLeave = widget.onLeave;

///Here we set the endPoints if it's non-null
Expand Down Expand Up @@ -112,7 +120,7 @@ class _ScreenControllerState extends State<ScreenController> {
await _hmsSDKInteractor.build();
_previewStore = PreviewStore(hmsSDKInteractor: _hmsSDKInteractor);
HMSException? ans = await _previewStore.startPreview(
userName: widget.options?.userName ?? "", roomCode: Constant.roomCode);
userName: widget.options?.userName ?? "");

///If preview fails then we show the error dialog
///with the error message and description
Expand Down Expand Up @@ -159,12 +167,10 @@ class _ScreenControllerState extends State<ScreenController> {
? ListenableProvider.value(
value: _previewStore,
child: PreviewPage(
roomCode: Constant.roomCode,
name: widget.options?.userName?.trim() ?? "",
options: widget.options,
))
: PreviewPermissions(
roomCode: Constant.roomCode,
options: widget.options,
callback: _isPermissionGrantedCallback),
);
Expand Down
Loading