Skip to content

Commit 87f583f

Browse files
committed
feat: temporarily copy decodeTokenPayload into this project from livekit-client
I should be able to get rid of this once this is merged: livekit/client-sdk-js#1710
1 parent ffd6617 commit 87f583f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/react/src/hooks/useSession.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ import {
1010
TokenSourceFixed,
1111
TokenSourceFetchOptions,
1212
RoomConnectOptions,
13+
TokenPayload,
14+
RoomConfigurationObject,
1315
} from 'livekit-client';
1416
import { EventEmitter } from 'events';
1517

1618
import { useMaybeRoomContext } from '../context';
1719
import { AgentState, useAgent, useAgentTimeoutIdStore } from './useAgent';
1820
import { TrackReference } from '@livekit/components-core';
1921
import { useLocalParticipant } from './useLocalParticipant';
22+
import { decodeJwt } from 'jose';
23+
import { RoomConfiguration } from '@livekit/protocol';
2024

2125
/** @public */
2226
export enum SessionEvent {
@@ -153,6 +157,24 @@ type UseSessionCommonOptions = {
153157
type UseSessionConfigurableOptions = UseSessionCommonOptions & TokenSourceFetchOptions;
154158
type UseSessionFixedOptions = UseSessionCommonOptions;
155159

160+
/** Given a LiveKit generated participant token, decodes and returns the associated {@link TokenPayload} data. */
161+
export function decodeTokenPayload(token: string) {
162+
const payload = decodeJwt<Omit<TokenPayload, 'roomConfig'>>(token);
163+
164+
const { roomConfig, ...rest } = payload;
165+
166+
const mappedPayload: TokenPayload = {
167+
...rest,
168+
roomConfig: payload.roomConfig
169+
? (RoomConfiguration.fromJson(
170+
payload.roomConfig as Record<string, any>,
171+
) as RoomConfigurationObject)
172+
: undefined,
173+
};
174+
175+
return mappedPayload;
176+
}
177+
156178
/**
157179
* A Session represents a manages connection to a Room which can contain Agents.
158180
* @public

0 commit comments

Comments
 (0)