Skip to content

Commit 6da49b1

Browse files
committed
revert token changes
1 parent bf839ad commit 6da49b1

File tree

3 files changed

+22
-60
lines changed

3 files changed

+22
-60
lines changed

src/ApiClient.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { v4 as uuidv4 } from 'uuid';
2-
import {
3-
ApiConfig,
4-
RequestMetadata,
5-
StreamError,
6-
UserTokenPayload,
7-
} from './types';
8-
import { JWTUserToken } from './utils/create-token';
2+
import { ApiConfig, RequestMetadata, StreamError } from './types';
93
import { APIError } from './gen/models';
104
import { getRateLimitFromResponseHeader } from './utils/rate-limit';
115

@@ -153,50 +147,4 @@ export class ApiClient {
153147

154148
return newParams.join('&');
155149
};
156-
157-
/**
158-
*
159-
* @param payload
160-
* - user_id - the id of the user the token is for
161-
* - validity_in_seconds - how many seconds is the token valid for (starting from issued at), by default it's 1 hour, dicarded if exp is provided
162-
* - exp - when the token expires, unix timestamp in seconds
163-
* - iat - issued at date of the token, unix timestamp in seconds, by default it's now
164-
*/
165-
generateUserToken = (
166-
payload: {
167-
user_id: string;
168-
validity_in_seconds?: number;
169-
exp?: number;
170-
iat?: number;
171-
} & Record<string, unknown>,
172-
) => {
173-
if (!this.apiConfig.secret) {
174-
throw new Error('API secret is not set');
175-
}
176-
177-
const defaultIat = Math.floor((Date.now() - 1000) / 1000);
178-
payload.iat = payload.iat ?? defaultIat;
179-
const validityInSeconds = payload.validity_in_seconds ?? 60 * 60;
180-
payload.exp = payload.exp ?? payload.iat + validityInSeconds;
181-
182-
return JWTUserToken(this.apiConfig.secret, payload as UserTokenPayload);
183-
};
184-
185-
createToken = (
186-
userID: string,
187-
exp = Math.round(Date.now() / 1000) + 60 * 60,
188-
iat = Math.floor((Date.now() - 1000) / 1000),
189-
) => {
190-
if (!this.apiConfig.secret) {
191-
throw new Error('API secret is not set');
192-
}
193-
194-
const payload: UserTokenPayload = {
195-
user_id: userID,
196-
exp,
197-
iat,
198-
};
199-
200-
return JWTUserToken(this.apiConfig.secret, payload);
201-
};
202150
}

src/StreamCall.ts

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

60-
const token = this.videoApi.apiClient.createToken(userID, undefined);
60+
const token = this.streamClient.generateUserToken({
61+
user_id: userID,
62+
});
6163
const segments = token.split('.');
6264
if (segments.length !== 3) {
6365
throw new Error('Invalid token format');

src/StreamClient.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CommonApi } from './gen/common/CommonApi';
33
import { StreamVideoClient } from './StreamVideoClient';
44
import crypto from 'crypto';
55
import { StreamChatClient } from './StreamChatClient';
6-
import { CallTokenPayload } from './types';
6+
import { CallTokenPayload, UserTokenPayload } from './types';
77
import {
88
FileUploadRequest,
99
ImageUploadRequest,
@@ -54,7 +54,6 @@ export class StreamClient extends CommonApi {
5454
baseUrl: chatBaseUrl,
5555
timeout,
5656
agent: config?.agent as RequestInit['dispatcher'],
57-
secret,
5857
});
5958

6059
const videoApiClient = new ApiClient({
@@ -63,7 +62,6 @@ export class StreamClient extends CommonApi {
6362
baseUrl: videoBaseUrl,
6463
timeout,
6564
agent: config?.agent as RequestInit['dispatcher'],
66-
secret,
6765
});
6866

6967
const feedsApiClient = new ApiClient({
@@ -72,7 +70,6 @@ export class StreamClient extends CommonApi {
7270
baseUrl: feedsBaseUrl,
7371
timeout,
7472
agent: config?.agent as RequestInit['dispatcher'],
75-
secret,
7673
});
7774

7875
super(chatApiClient);
@@ -139,7 +136,14 @@ export class StreamClient extends CommonApi {
139136
exp?: number;
140137
iat?: number;
141138
} & Record<string, unknown>,
142-
) => this.apiClient.generateUserToken(payload);
139+
) => {
140+
const defaultIat = Math.floor((Date.now() - 1000) / 1000);
141+
payload.iat = payload.iat ?? defaultIat;
142+
const validityInSeconds = payload.validity_in_seconds ?? 60 * 60;
143+
payload.exp = payload.exp ?? payload.iat + validityInSeconds;
144+
145+
return JWTUserToken(this.secret, payload as UserTokenPayload);
146+
};
143147

144148
/**
145149
*
@@ -175,7 +179,15 @@ export class StreamClient extends CommonApi {
175179
userID: string,
176180
exp = Math.round(Date.now() / 1000) + 60 * 60,
177181
iat = Math.floor((Date.now() - 1000) / 1000),
178-
) => this.apiClient.createToken(userID, exp, iat);
182+
) => {
183+
const payload: UserTokenPayload = {
184+
user_id: userID,
185+
exp,
186+
iat,
187+
};
188+
189+
return JWTUserToken(this.secret, payload);
190+
};
179191

180192
/**
181193
*

0 commit comments

Comments
 (0)