Skip to content

Commit c83d427

Browse files
committed
generate permanent user token for streaming
1 parent 6da49b1 commit c83d427

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/StreamCall.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class StreamCall extends CallApi {
5757
);
5858
}
5959

60-
const token = this.streamClient.generateUserToken({
60+
const token = this.streamClient.generatePermanentUserToken({
6161
user_id: userID,
6262
});
6363
const segments = token.split('.');

src/StreamClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ export class StreamClient extends CommonApi {
145145
return JWTUserToken(this.secret, payload as UserTokenPayload);
146146
};
147147

148+
/**
149+
*
150+
* @param payload
151+
* - user_id - the id of the user the token is for
152+
* - iat - issued at date of the token, unix timestamp in seconds, by default it's now
153+
*/
154+
generatePermanentUserToken = (
155+
payload: {
156+
user_id: string;
157+
iat?: number;
158+
} & Record<string, unknown>,
159+
) => {
160+
const defaultIat = Math.floor((Date.now() - 1000) / 1000);
161+
payload.iat = payload.iat ?? defaultIat;
162+
163+
return JWTUserToken(this.secret, payload as UserTokenPayload);
164+
};
165+
148166
/**
149167
*
150168
* @param payload

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface RateLimit {
4040

4141
interface BaseTokenPayload {
4242
user_id: string;
43-
exp: number;
43+
exp?: number;
4444
iat: number;
4545
call_cids?: string[];
4646
}

src/utils/create-token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function JWTUserToken(
44
apiSecret: Secret,
55
payload: {
66
user_id: string;
7-
exp: number;
7+
exp?: number;
88
iat: number;
99
call_cids?: string[];
1010
} & { [key: string]: any },

0 commit comments

Comments
 (0)