Skip to content

Commit

Permalink
Add helper function to generate random user identity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocupe committed Nov 6, 2023
1 parent 5337dd5 commit 1946a66
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions examples/nextjs/lib/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* A simple helper function to generate a random user identity.
*/
export function generateRandomUserId() {
return `test-identity-${String(Math.random() * 100_000).slice(0, 5)}` as const;
}
3 changes: 2 additions & 1 deletion examples/nextjs/pages/audio-only.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AudioConference, LiveKitRoom, useToken } from '@livekit/components-react';
import type { NextPage } from 'next';
import { generateRandomUserId } from '../lib/helper';

const AudioExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const userIdentity = params?.get('user') ?? generateRandomUserId();

const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
userInfo: {
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs/pages/clubhouse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import {
import styles from '../styles/Clubhouse.module.scss';
import { Track } from 'livekit-client';
import { useMemo, useState } from 'react';
import { generateRandomUserId } from '../lib/helper';

const Clubhouse = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const userIdentity = params?.get('user') ?? generateRandomUserId();

const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
userInfo: {
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs/pages/customize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import myStyles from '../styles/Customize.module.css';
import type { NextPage } from 'next';
import { HTMLAttributes, useState } from 'react';
import { isTrackReference } from '@livekit/components-core';
import { generateRandomUserId } from '../lib/helper';

const CustomizeExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const userIdentity = params?.get('user') ?? generateRandomUserId();
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
userInfo: {
identity: userIdentity,
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs/pages/e2ee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { LiveKitRoom, useToken, VideoConference } from '@livekit/components-reac
import type { NextPage } from 'next';
import * as React from 'react';
import { Room, ExternalE2EEKeyProvider } from 'livekit-client';
import { generateRandomUserId } from '../lib/helper';

const E2EEExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const userIdentity = params?.get('user') ?? generateRandomUserId();
setLogLevel('warn', { liveKitClientLogLevel: 'debug' });

const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs/pages/minimal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { setLogLevel } from '@livekit/components-core';
import { LiveKitRoom, useToken, VideoConference } from '@livekit/components-react';
import { RoomConnectOptions } from 'livekit-client';
import type { NextPage } from 'next';
import { generateRandomUserId } from '../lib/helper';

const MinimalExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const userIdentity = params?.get('user') ?? generateRandomUserId();
setLogLevel('info', { liveKitClientLogLevel: 'warn' });

const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs/pages/simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import { Track } from 'livekit-client';
import type { NextPage } from 'next';
import { useState } from 'react';
import styles from '../styles/Simple.module.css';
import { generateRandomUserId } from '../lib/helper';

const SimpleExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = params?.get('room') ?? 'test-room';
const userIdentity = params?.get('user') ?? 'test-identity';
const userIdentity = params?.get('user') ?? generateRandomUserId();
const [connect, setConnect] = useState(false);
const [isConnected, setIsConnected] = useState(false);

Expand Down

0 comments on commit 1946a66

Please sign in to comment.