@amityco/ts-sdk 7.5.4-db55aa2.0 → 7.5.4-ec2fdd4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/api/index.d.ts +1 -0
- package/dist/client/api/index.d.ts.map +1 -1
- package/dist/client/api/resumeSession.d.ts +32 -0
- package/dist/client/api/resumeSession.d.ts.map +1 -0
- package/dist/client/api/tests/resumeSession.test.d.ts +2 -0
- package/dist/client/api/tests/resumeSession.test.d.ts.map +1 -0
- package/dist/index.cjs.js +186 -13
- package/dist/index.esm.js +185 -13
- package/dist/index.umd.js +4 -4
- package/package.json +2 -1
- package/src/client/api/index.ts +1 -0
- package/src/client/api/resumeSession.ts +282 -0
- package/src/client/api/tests/resumeSession.test.ts +173 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/client/api/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,cAAc,cAAc,CAAC;AAE7B,cAAc,gBAAgB,CAAC;AAE/B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/client/api/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,cAAc,cAAc,CAAC;AAE7B,cAAc,gBAAgB,CAAC;AAE/B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAEhC,cAAc,eAAe,CAAC;AAE9B,cAAc,mBAAmB,CAAC;AAElC,cAAc,WAAW,CAAC;AAE1B,cAAc,cAAc,CAAC;AAE7B,cAAc,qBAAqB,CAAC;AAEpC,cAAc,6BAA6B,CAAC;AAE5C,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface Params {
|
|
2
|
+
userId: string;
|
|
3
|
+
token: {
|
|
4
|
+
issuedAt: string;
|
|
5
|
+
expiresAt: string;
|
|
6
|
+
accessToken: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* ```js
|
|
11
|
+
* import { resumeSession } from '@amityco/ts-sdk/client/api'
|
|
12
|
+
* const success = await resumeSession({
|
|
13
|
+
* userId: 'XYZ123456789',
|
|
14
|
+
* token: { accessToken: 'abc123', issuedAt: '2023-01-01T00:00:00Z', expiresAt: '2023-01-02T00:00:00Z' }
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Connects an {@link Amity.Client} instance to ASC servers using an existing access token
|
|
19
|
+
*
|
|
20
|
+
* @param params the connect parameters
|
|
21
|
+
* @param params.userId the user ID for the current session
|
|
22
|
+
* @param params.token the existing access token with its metadata
|
|
23
|
+
* @param sessionHandler the session handler for token renewal
|
|
24
|
+
* @param config optional configuration
|
|
25
|
+
* @returns a success boolean if connected
|
|
26
|
+
*
|
|
27
|
+
* @category Client API
|
|
28
|
+
* @async
|
|
29
|
+
*/
|
|
30
|
+
export declare const resumeSession: (params: Params, sessionHandler: Amity.SessionHandler, config?: Amity.ConnectClientConfig) => Promise<boolean>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=resumeSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resumeSession.d.ts","sourceRoot":"","sources":["../../../src/client/api/resumeSession.ts"],"names":[],"mappings":"AA0CA,UAAU,MAAM;IACd,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE;QACL,QAAQ,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;CACH;AA+DD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,aAAa,WAChB,MAAM,kBACE,MAAM,cAAc,WAC3B,MAAM,mBAAmB,KACjC,QAAQ,OAAO,CA+IjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resumeSession.test.d.ts","sourceRoot":"","sources":["../../../../src/client/api/tests/resumeSession.test.ts"],"names":[],"mappings":""}
|
package/dist/index.cjs.js
CHANGED
|
@@ -10,6 +10,7 @@ var HttpAgent = require('agentkeepalive');
|
|
|
10
10
|
var io = require('socket.io-client');
|
|
11
11
|
var uuid$1 = require('react-native-uuid');
|
|
12
12
|
var hash = require('object-hash');
|
|
13
|
+
var jwtDecode = require('jwt-decode');
|
|
13
14
|
var Hls = require('hls.js');
|
|
14
15
|
|
|
15
16
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
@@ -21,6 +22,7 @@ var HttpAgent__default = /*#__PURE__*/_interopDefaultLegacy(HttpAgent);
|
|
|
21
22
|
var io__default = /*#__PURE__*/_interopDefaultLegacy(io);
|
|
22
23
|
var uuid__default = /*#__PURE__*/_interopDefaultLegacy(uuid$1);
|
|
23
24
|
var hash__default = /*#__PURE__*/_interopDefaultLegacy(hash);
|
|
25
|
+
var jwtDecode__default = /*#__PURE__*/_interopDefaultLegacy(jwtDecode);
|
|
24
26
|
var Hls__default = /*#__PURE__*/_interopDefaultLegacy(Hls);
|
|
25
27
|
|
|
26
28
|
exports.MembershipAcceptanceTypeEnum = void 0;
|
|
@@ -9901,8 +9903,8 @@ const removeChannelMarkerCache = (channel) => {
|
|
|
9901
9903
|
* than the one already connected, in which case the existing subscriptions need
|
|
9902
9904
|
* to be cleared
|
|
9903
9905
|
*/
|
|
9904
|
-
let subscriptions = [];
|
|
9905
|
-
async function runMqtt() {
|
|
9906
|
+
let subscriptions$1 = [];
|
|
9907
|
+
async function runMqtt$1() {
|
|
9906
9908
|
await modifyMqttConnection();
|
|
9907
9909
|
}
|
|
9908
9910
|
/* begin_public_function
|
|
@@ -9937,8 +9939,8 @@ const login = async (params, sessionHandler, config) => {
|
|
|
9937
9939
|
if (client.userId && client.userId !== params.userId) {
|
|
9938
9940
|
await logout();
|
|
9939
9941
|
// Remove subscription to ban and delete
|
|
9940
|
-
subscriptions.forEach(fn => fn());
|
|
9941
|
-
subscriptions = [];
|
|
9942
|
+
subscriptions$1.forEach(fn => fn());
|
|
9943
|
+
subscriptions$1 = [];
|
|
9942
9944
|
}
|
|
9943
9945
|
// default values
|
|
9944
9946
|
const defaultDeviceId = await getDeviceId();
|
|
@@ -9984,40 +9986,40 @@ const login = async (params, sessionHandler, config) => {
|
|
|
9984
9986
|
throw error;
|
|
9985
9987
|
}
|
|
9986
9988
|
if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
|
|
9987
|
-
runMqtt();
|
|
9989
|
+
runMqtt$1();
|
|
9988
9990
|
}
|
|
9989
9991
|
await initializeMessagePreviewSetting();
|
|
9990
|
-
if (subscriptions.length === 0) {
|
|
9991
|
-
subscriptions.push(
|
|
9992
|
+
if (subscriptions$1.length === 0) {
|
|
9993
|
+
subscriptions$1.push(
|
|
9992
9994
|
// GLOBAL_BAN
|
|
9993
9995
|
onClientBanned((_) => {
|
|
9994
9996
|
terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
|
|
9995
|
-
subscriptions.forEach(fn => fn());
|
|
9997
|
+
subscriptions$1.forEach(fn => fn());
|
|
9996
9998
|
unsubWatcher();
|
|
9997
9999
|
}), onTokenTerminated(_ => {
|
|
9998
10000
|
terminateClient();
|
|
9999
|
-
subscriptions.forEach(fn => fn());
|
|
10001
|
+
subscriptions$1.forEach(fn => fn());
|
|
10000
10002
|
unsubWatcher();
|
|
10001
10003
|
}), onUserDeleted$2((user) => {
|
|
10002
10004
|
if (user.userId === client.userId) {
|
|
10003
10005
|
terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
|
|
10004
|
-
subscriptions.forEach(fn => fn());
|
|
10006
|
+
subscriptions$1.forEach(fn => fn());
|
|
10005
10007
|
unsubWatcher();
|
|
10006
10008
|
}
|
|
10007
10009
|
}), onTokenExpired(state => {
|
|
10008
10010
|
SessionWatcher$1.getInstance().setSessionState(state);
|
|
10009
10011
|
logout();
|
|
10010
|
-
subscriptions.forEach(fn => fn());
|
|
10012
|
+
subscriptions$1.forEach(fn => fn());
|
|
10011
10013
|
}),
|
|
10012
10014
|
// NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
|
|
10013
10015
|
// the channel because currently backend can't handle this, so every time a user is banned from
|
|
10014
10016
|
// a channel or the channel is deleted the channel's unread count will not be reset to zero
|
|
10015
10017
|
onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
|
|
10016
10018
|
if (client.useLegacyUnreadCount) {
|
|
10017
|
-
subscriptions.push(readReceiptSyncEngineOnLoginHandler());
|
|
10019
|
+
subscriptions$1.push(readReceiptSyncEngineOnLoginHandler());
|
|
10018
10020
|
}
|
|
10019
10021
|
else
|
|
10020
|
-
subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
|
|
10022
|
+
subscriptions$1.push(legacyReadReceiptSyncEngineOnLoginHandler());
|
|
10021
10023
|
}
|
|
10022
10024
|
return true;
|
|
10023
10025
|
};
|
|
@@ -10942,6 +10944,176 @@ const secureLogout = async () => {
|
|
|
10942
10944
|
};
|
|
10943
10945
|
/* end_public_function */
|
|
10944
10946
|
|
|
10947
|
+
/*
|
|
10948
|
+
* declared earlier to accomodate case when logging in with a different user
|
|
10949
|
+
* than the one already connected, in which case the existing subscriptions need
|
|
10950
|
+
* to be cleared
|
|
10951
|
+
*/
|
|
10952
|
+
let subscriptions = [];
|
|
10953
|
+
async function runMqtt() {
|
|
10954
|
+
await modifyMqttConnection();
|
|
10955
|
+
}
|
|
10956
|
+
const isSameUserId = (token) => {
|
|
10957
|
+
var _a;
|
|
10958
|
+
const client = getActiveClient();
|
|
10959
|
+
const decoded = jwtDecode__default["default"](token);
|
|
10960
|
+
return ((_a = decoded === null || decoded === void 0 ? void 0 : decoded.user) === null || _a === void 0 ? void 0 : _a.publicUserId) === client.userId;
|
|
10961
|
+
};
|
|
10962
|
+
const validateAccessToken = async ({ token, userId }) => {
|
|
10963
|
+
const client = getActiveClient();
|
|
10964
|
+
// begin establishing session
|
|
10965
|
+
setSessionState("establishing" /* Amity.SessionStates.ESTABLISHING */);
|
|
10966
|
+
const { data: { users }, } = await client.http.get(`/api/v3/users/${userId}`, {
|
|
10967
|
+
headers: {
|
|
10968
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
10969
|
+
},
|
|
10970
|
+
});
|
|
10971
|
+
const user = users.find((u) => u.userId === userId);
|
|
10972
|
+
client.http.defaults.headers.common.Authorization = `Bearer ${token.accessToken}`;
|
|
10973
|
+
client.http.defaults.metadata = {
|
|
10974
|
+
tokenExpiry: token.expiresAt,
|
|
10975
|
+
isGlobalBanned: false,
|
|
10976
|
+
isUserDeleted: false,
|
|
10977
|
+
};
|
|
10978
|
+
client.upload.defaults.headers.common.Authorization = `Bearer ${token.accessToken}`;
|
|
10979
|
+
client.upload.defaults.metadata = {
|
|
10980
|
+
tokenExpiry: token.expiresAt,
|
|
10981
|
+
isGlobalBanned: false,
|
|
10982
|
+
isUserDeleted: false,
|
|
10983
|
+
};
|
|
10984
|
+
// manually setup the token for ws transport
|
|
10985
|
+
if (client.ws)
|
|
10986
|
+
client.ws.io.opts.query = { token: token.accessToken };
|
|
10987
|
+
client.token = token;
|
|
10988
|
+
setSessionState("established" /* Amity.SessionStates.ESTABLISHED */);
|
|
10989
|
+
return user;
|
|
10990
|
+
};
|
|
10991
|
+
/* begin_public_function
|
|
10992
|
+
id: client.resumeSession
|
|
10993
|
+
*/
|
|
10994
|
+
/**
|
|
10995
|
+
* ```js
|
|
10996
|
+
* import { resumeSession } from '@amityco/ts-sdk/client/api'
|
|
10997
|
+
* const success = await resumeSession({
|
|
10998
|
+
* userId: 'XYZ123456789',
|
|
10999
|
+
* token: { accessToken: 'abc123', issuedAt: '2023-01-01T00:00:00Z', expiresAt: '2023-01-02T00:00:00Z' }
|
|
11000
|
+
* })
|
|
11001
|
+
* ```
|
|
11002
|
+
*
|
|
11003
|
+
* Connects an {@link Amity.Client} instance to ASC servers using an existing access token
|
|
11004
|
+
*
|
|
11005
|
+
* @param params the connect parameters
|
|
11006
|
+
* @param params.userId the user ID for the current session
|
|
11007
|
+
* @param params.token the existing access token with its metadata
|
|
11008
|
+
* @param sessionHandler the session handler for token renewal
|
|
11009
|
+
* @param config optional configuration
|
|
11010
|
+
* @returns a success boolean if connected
|
|
11011
|
+
*
|
|
11012
|
+
* @category Client API
|
|
11013
|
+
* @async
|
|
11014
|
+
*/
|
|
11015
|
+
const resumeSession = async (params, sessionHandler, config) => {
|
|
11016
|
+
var _a;
|
|
11017
|
+
const client = getActiveClient();
|
|
11018
|
+
let unsubWatcher;
|
|
11019
|
+
client.log('client/api/resumeSession', Object.assign({ apiKey: client.apiKey, sessionState: client.sessionState }, params));
|
|
11020
|
+
// Handle existing connected user
|
|
11021
|
+
if (client.userId) {
|
|
11022
|
+
if (client.userId === params.userId && isSameUserId(params.token.accessToken)) {
|
|
11023
|
+
// Clear connections and listeners but preserve cache
|
|
11024
|
+
if (client.mqtt && client.mqtt.connected) {
|
|
11025
|
+
client.mqtt.disconnect();
|
|
11026
|
+
}
|
|
11027
|
+
if (client.ws && client.ws.connected) {
|
|
11028
|
+
client.ws.disconnect();
|
|
11029
|
+
}
|
|
11030
|
+
// Clear existing subscriptions
|
|
11031
|
+
subscriptions.forEach(fn => fn());
|
|
11032
|
+
subscriptions = [];
|
|
11033
|
+
}
|
|
11034
|
+
else {
|
|
11035
|
+
// Different user - do full logout
|
|
11036
|
+
await logout();
|
|
11037
|
+
// Remove subscription to ban and delete
|
|
11038
|
+
subscriptions.forEach(fn => fn());
|
|
11039
|
+
subscriptions = [];
|
|
11040
|
+
}
|
|
11041
|
+
}
|
|
11042
|
+
try {
|
|
11043
|
+
const user = await validateAccessToken(params);
|
|
11044
|
+
if (user == null) {
|
|
11045
|
+
throw new ASCError(`${params.userId} has not been found`, 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
|
|
11046
|
+
}
|
|
11047
|
+
if (user.isDeleted) {
|
|
11048
|
+
terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
|
|
11049
|
+
return false;
|
|
11050
|
+
}
|
|
11051
|
+
if (user.isGlobalBanned) {
|
|
11052
|
+
terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
|
|
11053
|
+
return false;
|
|
11054
|
+
}
|
|
11055
|
+
// FIXME: events are duplicated if connectClient is called few times without disconnectClient
|
|
11056
|
+
// wire websocket events to our event emitter
|
|
11057
|
+
proxyWebsocketEvents(client.ws, client.emitter);
|
|
11058
|
+
(_a = client.ws) === null || _a === void 0 ? void 0 : _a.open();
|
|
11059
|
+
client.userId = user.userId;
|
|
11060
|
+
client.sessionHandler = sessionHandler;
|
|
11061
|
+
/*
|
|
11062
|
+
* Cannot push to subscriptions as watcher needs to continue working even if
|
|
11063
|
+
* token expires
|
|
11064
|
+
*/
|
|
11065
|
+
unsubWatcher = client.accessTokenExpiryWatcher(sessionHandler);
|
|
11066
|
+
setActiveUser(user);
|
|
11067
|
+
}
|
|
11068
|
+
catch (error) {
|
|
11069
|
+
/*
|
|
11070
|
+
* if getting token failed session state reverts to initial state when app
|
|
11071
|
+
* is first launched
|
|
11072
|
+
*/
|
|
11073
|
+
SessionWatcher$1.getInstance().setSessionState("notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */);
|
|
11074
|
+
// pass error down tree so the calling function handle it
|
|
11075
|
+
throw error;
|
|
11076
|
+
}
|
|
11077
|
+
if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
|
|
11078
|
+
runMqtt();
|
|
11079
|
+
}
|
|
11080
|
+
await initializeMessagePreviewSetting();
|
|
11081
|
+
if (subscriptions.length === 0) {
|
|
11082
|
+
subscriptions.push(
|
|
11083
|
+
// GLOBAL_BAN
|
|
11084
|
+
onClientBanned((_) => {
|
|
11085
|
+
terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
|
|
11086
|
+
subscriptions.forEach(fn => fn());
|
|
11087
|
+
unsubWatcher();
|
|
11088
|
+
}), onTokenTerminated(_ => {
|
|
11089
|
+
terminateClient();
|
|
11090
|
+
subscriptions.forEach(fn => fn());
|
|
11091
|
+
unsubWatcher();
|
|
11092
|
+
}), onUserDeleted$2((user) => {
|
|
11093
|
+
if (user.userId === client.userId) {
|
|
11094
|
+
terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
|
|
11095
|
+
subscriptions.forEach(fn => fn());
|
|
11096
|
+
unsubWatcher();
|
|
11097
|
+
}
|
|
11098
|
+
}), onTokenExpired(state => {
|
|
11099
|
+
SessionWatcher$1.getInstance().setSessionState(state);
|
|
11100
|
+
logout();
|
|
11101
|
+
subscriptions.forEach(fn => fn());
|
|
11102
|
+
}),
|
|
11103
|
+
// NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
|
|
11104
|
+
// the channel because currently backend can't handle this, so every time a user is banned from
|
|
11105
|
+
// a channel or the channel is deleted the channel's unread count will not be reset to zero
|
|
11106
|
+
onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
|
|
11107
|
+
if (client.useLegacyUnreadCount) {
|
|
11108
|
+
subscriptions.push(readReceiptSyncEngineOnLoginHandler());
|
|
11109
|
+
}
|
|
11110
|
+
else
|
|
11111
|
+
subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
|
|
11112
|
+
}
|
|
11113
|
+
return true;
|
|
11114
|
+
};
|
|
11115
|
+
/* end_public_function */
|
|
11116
|
+
|
|
10945
11117
|
/**
|
|
10946
11118
|
* ```js
|
|
10947
11119
|
* import { isConnected } from '@amityco/ts-sdk'
|
|
@@ -11347,6 +11519,7 @@ var index$n = /*#__PURE__*/Object.freeze({
|
|
|
11347
11519
|
login: login,
|
|
11348
11520
|
logout: logout,
|
|
11349
11521
|
secureLogout: secureLogout,
|
|
11522
|
+
resumeSession: resumeSession,
|
|
11350
11523
|
isConnected: isConnected,
|
|
11351
11524
|
getFeedSettings: getFeedSettings,
|
|
11352
11525
|
renewal: renewal,
|
package/dist/index.esm.js
CHANGED
|
@@ -6,6 +6,7 @@ import HttpAgent, { HttpsAgent } from 'agentkeepalive';
|
|
|
6
6
|
import io from 'socket.io-client';
|
|
7
7
|
import uuid$1 from 'react-native-uuid';
|
|
8
8
|
import hash from 'object-hash';
|
|
9
|
+
import jwtDecode from 'jwt-decode';
|
|
9
10
|
import Hls from 'hls.js';
|
|
10
11
|
|
|
11
12
|
var MembershipAcceptanceTypeEnum;
|
|
@@ -25993,8 +25994,8 @@ const removeChannelMarkerCache = (channel) => {
|
|
|
25993
25994
|
* than the one already connected, in which case the existing subscriptions need
|
|
25994
25995
|
* to be cleared
|
|
25995
25996
|
*/
|
|
25996
|
-
let subscriptions = [];
|
|
25997
|
-
async function runMqtt() {
|
|
25997
|
+
let subscriptions$1 = [];
|
|
25998
|
+
async function runMqtt$1() {
|
|
25998
25999
|
await modifyMqttConnection();
|
|
25999
26000
|
}
|
|
26000
26001
|
/* begin_public_function
|
|
@@ -26029,8 +26030,8 @@ const login = async (params, sessionHandler, config) => {
|
|
|
26029
26030
|
if (client.userId && client.userId !== params.userId) {
|
|
26030
26031
|
await logout();
|
|
26031
26032
|
// Remove subscription to ban and delete
|
|
26032
|
-
subscriptions.forEach(fn => fn());
|
|
26033
|
-
subscriptions = [];
|
|
26033
|
+
subscriptions$1.forEach(fn => fn());
|
|
26034
|
+
subscriptions$1 = [];
|
|
26034
26035
|
}
|
|
26035
26036
|
// default values
|
|
26036
26037
|
const defaultDeviceId = await getDeviceId();
|
|
@@ -26076,40 +26077,40 @@ const login = async (params, sessionHandler, config) => {
|
|
|
26076
26077
|
throw error;
|
|
26077
26078
|
}
|
|
26078
26079
|
if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
|
|
26079
|
-
runMqtt();
|
|
26080
|
+
runMqtt$1();
|
|
26080
26081
|
}
|
|
26081
26082
|
await initializeMessagePreviewSetting();
|
|
26082
|
-
if (subscriptions.length === 0) {
|
|
26083
|
-
subscriptions.push(
|
|
26083
|
+
if (subscriptions$1.length === 0) {
|
|
26084
|
+
subscriptions$1.push(
|
|
26084
26085
|
// GLOBAL_BAN
|
|
26085
26086
|
onClientBanned((_) => {
|
|
26086
26087
|
terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
|
|
26087
|
-
subscriptions.forEach(fn => fn());
|
|
26088
|
+
subscriptions$1.forEach(fn => fn());
|
|
26088
26089
|
unsubWatcher();
|
|
26089
26090
|
}), onTokenTerminated(_ => {
|
|
26090
26091
|
terminateClient();
|
|
26091
|
-
subscriptions.forEach(fn => fn());
|
|
26092
|
+
subscriptions$1.forEach(fn => fn());
|
|
26092
26093
|
unsubWatcher();
|
|
26093
26094
|
}), onUserDeleted$2((user) => {
|
|
26094
26095
|
if (user.userId === client.userId) {
|
|
26095
26096
|
terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
|
|
26096
|
-
subscriptions.forEach(fn => fn());
|
|
26097
|
+
subscriptions$1.forEach(fn => fn());
|
|
26097
26098
|
unsubWatcher();
|
|
26098
26099
|
}
|
|
26099
26100
|
}), onTokenExpired(state => {
|
|
26100
26101
|
SessionWatcher$1.getInstance().setSessionState(state);
|
|
26101
26102
|
logout();
|
|
26102
|
-
subscriptions.forEach(fn => fn());
|
|
26103
|
+
subscriptions$1.forEach(fn => fn());
|
|
26103
26104
|
}),
|
|
26104
26105
|
// NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
|
|
26105
26106
|
// the channel because currently backend can't handle this, so every time a user is banned from
|
|
26106
26107
|
// a channel or the channel is deleted the channel's unread count will not be reset to zero
|
|
26107
26108
|
onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
|
|
26108
26109
|
if (client.useLegacyUnreadCount) {
|
|
26109
|
-
subscriptions.push(readReceiptSyncEngineOnLoginHandler());
|
|
26110
|
+
subscriptions$1.push(readReceiptSyncEngineOnLoginHandler());
|
|
26110
26111
|
}
|
|
26111
26112
|
else
|
|
26112
|
-
subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
|
|
26113
|
+
subscriptions$1.push(legacyReadReceiptSyncEngineOnLoginHandler());
|
|
26113
26114
|
}
|
|
26114
26115
|
return true;
|
|
26115
26116
|
};
|
|
@@ -27034,6 +27035,176 @@ const secureLogout = async () => {
|
|
|
27034
27035
|
};
|
|
27035
27036
|
/* end_public_function */
|
|
27036
27037
|
|
|
27038
|
+
/*
|
|
27039
|
+
* declared earlier to accomodate case when logging in with a different user
|
|
27040
|
+
* than the one already connected, in which case the existing subscriptions need
|
|
27041
|
+
* to be cleared
|
|
27042
|
+
*/
|
|
27043
|
+
let subscriptions = [];
|
|
27044
|
+
async function runMqtt() {
|
|
27045
|
+
await modifyMqttConnection();
|
|
27046
|
+
}
|
|
27047
|
+
const isSameUserId = (token) => {
|
|
27048
|
+
var _a;
|
|
27049
|
+
const client = getActiveClient();
|
|
27050
|
+
const decoded = jwtDecode(token);
|
|
27051
|
+
return ((_a = decoded === null || decoded === void 0 ? void 0 : decoded.user) === null || _a === void 0 ? void 0 : _a.publicUserId) === client.userId;
|
|
27052
|
+
};
|
|
27053
|
+
const validateAccessToken = async ({ token, userId }) => {
|
|
27054
|
+
const client = getActiveClient();
|
|
27055
|
+
// begin establishing session
|
|
27056
|
+
setSessionState("establishing" /* Amity.SessionStates.ESTABLISHING */);
|
|
27057
|
+
const { data: { users }, } = await client.http.get(`/api/v3/users/${userId}`, {
|
|
27058
|
+
headers: {
|
|
27059
|
+
Authorization: `Bearer ${token.accessToken}`,
|
|
27060
|
+
},
|
|
27061
|
+
});
|
|
27062
|
+
const user = users.find((u) => u.userId === userId);
|
|
27063
|
+
client.http.defaults.headers.common.Authorization = `Bearer ${token.accessToken}`;
|
|
27064
|
+
client.http.defaults.metadata = {
|
|
27065
|
+
tokenExpiry: token.expiresAt,
|
|
27066
|
+
isGlobalBanned: false,
|
|
27067
|
+
isUserDeleted: false,
|
|
27068
|
+
};
|
|
27069
|
+
client.upload.defaults.headers.common.Authorization = `Bearer ${token.accessToken}`;
|
|
27070
|
+
client.upload.defaults.metadata = {
|
|
27071
|
+
tokenExpiry: token.expiresAt,
|
|
27072
|
+
isGlobalBanned: false,
|
|
27073
|
+
isUserDeleted: false,
|
|
27074
|
+
};
|
|
27075
|
+
// manually setup the token for ws transport
|
|
27076
|
+
if (client.ws)
|
|
27077
|
+
client.ws.io.opts.query = { token: token.accessToken };
|
|
27078
|
+
client.token = token;
|
|
27079
|
+
setSessionState("established" /* Amity.SessionStates.ESTABLISHED */);
|
|
27080
|
+
return user;
|
|
27081
|
+
};
|
|
27082
|
+
/* begin_public_function
|
|
27083
|
+
id: client.resumeSession
|
|
27084
|
+
*/
|
|
27085
|
+
/**
|
|
27086
|
+
* ```js
|
|
27087
|
+
* import { resumeSession } from '@amityco/ts-sdk/client/api'
|
|
27088
|
+
* const success = await resumeSession({
|
|
27089
|
+
* userId: 'XYZ123456789',
|
|
27090
|
+
* token: { accessToken: 'abc123', issuedAt: '2023-01-01T00:00:00Z', expiresAt: '2023-01-02T00:00:00Z' }
|
|
27091
|
+
* })
|
|
27092
|
+
* ```
|
|
27093
|
+
*
|
|
27094
|
+
* Connects an {@link Amity.Client} instance to ASC servers using an existing access token
|
|
27095
|
+
*
|
|
27096
|
+
* @param params the connect parameters
|
|
27097
|
+
* @param params.userId the user ID for the current session
|
|
27098
|
+
* @param params.token the existing access token with its metadata
|
|
27099
|
+
* @param sessionHandler the session handler for token renewal
|
|
27100
|
+
* @param config optional configuration
|
|
27101
|
+
* @returns a success boolean if connected
|
|
27102
|
+
*
|
|
27103
|
+
* @category Client API
|
|
27104
|
+
* @async
|
|
27105
|
+
*/
|
|
27106
|
+
const resumeSession = async (params, sessionHandler, config) => {
|
|
27107
|
+
var _a;
|
|
27108
|
+
const client = getActiveClient();
|
|
27109
|
+
let unsubWatcher;
|
|
27110
|
+
client.log('client/api/resumeSession', Object.assign({ apiKey: client.apiKey, sessionState: client.sessionState }, params));
|
|
27111
|
+
// Handle existing connected user
|
|
27112
|
+
if (client.userId) {
|
|
27113
|
+
if (client.userId === params.userId && isSameUserId(params.token.accessToken)) {
|
|
27114
|
+
// Clear connections and listeners but preserve cache
|
|
27115
|
+
if (client.mqtt && client.mqtt.connected) {
|
|
27116
|
+
client.mqtt.disconnect();
|
|
27117
|
+
}
|
|
27118
|
+
if (client.ws && client.ws.connected) {
|
|
27119
|
+
client.ws.disconnect();
|
|
27120
|
+
}
|
|
27121
|
+
// Clear existing subscriptions
|
|
27122
|
+
subscriptions.forEach(fn => fn());
|
|
27123
|
+
subscriptions = [];
|
|
27124
|
+
}
|
|
27125
|
+
else {
|
|
27126
|
+
// Different user - do full logout
|
|
27127
|
+
await logout();
|
|
27128
|
+
// Remove subscription to ban and delete
|
|
27129
|
+
subscriptions.forEach(fn => fn());
|
|
27130
|
+
subscriptions = [];
|
|
27131
|
+
}
|
|
27132
|
+
}
|
|
27133
|
+
try {
|
|
27134
|
+
const user = await validateAccessToken(params);
|
|
27135
|
+
if (user == null) {
|
|
27136
|
+
throw new ASCError(`${params.userId} has not been found`, 800000 /* Amity.ClientError.UNKNOWN_ERROR */, "error" /* Amity.ErrorLevel.ERROR */);
|
|
27137
|
+
}
|
|
27138
|
+
if (user.isDeleted) {
|
|
27139
|
+
terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
|
|
27140
|
+
return false;
|
|
27141
|
+
}
|
|
27142
|
+
if (user.isGlobalBanned) {
|
|
27143
|
+
terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
|
|
27144
|
+
return false;
|
|
27145
|
+
}
|
|
27146
|
+
// FIXME: events are duplicated if connectClient is called few times without disconnectClient
|
|
27147
|
+
// wire websocket events to our event emitter
|
|
27148
|
+
proxyWebsocketEvents(client.ws, client.emitter);
|
|
27149
|
+
(_a = client.ws) === null || _a === void 0 ? void 0 : _a.open();
|
|
27150
|
+
client.userId = user.userId;
|
|
27151
|
+
client.sessionHandler = sessionHandler;
|
|
27152
|
+
/*
|
|
27153
|
+
* Cannot push to subscriptions as watcher needs to continue working even if
|
|
27154
|
+
* token expires
|
|
27155
|
+
*/
|
|
27156
|
+
unsubWatcher = client.accessTokenExpiryWatcher(sessionHandler);
|
|
27157
|
+
setActiveUser(user);
|
|
27158
|
+
}
|
|
27159
|
+
catch (error) {
|
|
27160
|
+
/*
|
|
27161
|
+
* if getting token failed session state reverts to initial state when app
|
|
27162
|
+
* is first launched
|
|
27163
|
+
*/
|
|
27164
|
+
SessionWatcher$1.getInstance().setSessionState("notLoggedIn" /* Amity.SessionStates.NOT_LOGGED_IN */);
|
|
27165
|
+
// pass error down tree so the calling function handle it
|
|
27166
|
+
throw error;
|
|
27167
|
+
}
|
|
27168
|
+
if ((config === null || config === void 0 ? void 0 : config.disableRTE) !== true) {
|
|
27169
|
+
runMqtt();
|
|
27170
|
+
}
|
|
27171
|
+
await initializeMessagePreviewSetting();
|
|
27172
|
+
if (subscriptions.length === 0) {
|
|
27173
|
+
subscriptions.push(
|
|
27174
|
+
// GLOBAL_BAN
|
|
27175
|
+
onClientBanned((_) => {
|
|
27176
|
+
terminateClient("globalBan" /* Amity.TokenTerminationReason.GLOBAL_BAN */);
|
|
27177
|
+
subscriptions.forEach(fn => fn());
|
|
27178
|
+
unsubWatcher();
|
|
27179
|
+
}), onTokenTerminated(_ => {
|
|
27180
|
+
terminateClient();
|
|
27181
|
+
subscriptions.forEach(fn => fn());
|
|
27182
|
+
unsubWatcher();
|
|
27183
|
+
}), onUserDeleted$2((user) => {
|
|
27184
|
+
if (user.userId === client.userId) {
|
|
27185
|
+
terminateClient("userDeleted" /* Amity.TokenTerminationReason.USER_DELETED */);
|
|
27186
|
+
subscriptions.forEach(fn => fn());
|
|
27187
|
+
unsubWatcher();
|
|
27188
|
+
}
|
|
27189
|
+
}), onTokenExpired(state => {
|
|
27190
|
+
SessionWatcher$1.getInstance().setSessionState(state);
|
|
27191
|
+
logout();
|
|
27192
|
+
subscriptions.forEach(fn => fn());
|
|
27193
|
+
}),
|
|
27194
|
+
// NOTE: This is a temporary solution to handle the channel marker when the user is forced to leave
|
|
27195
|
+
// the channel because currently backend can't handle this, so every time a user is banned from
|
|
27196
|
+
// a channel or the channel is deleted the channel's unread count will not be reset to zero
|
|
27197
|
+
onChannelDeleted(removeChannelMarkerCache), onChannelMemberBanned(removeChannelMarkerCache), markReadEngineOnLoginHandler(), analyticsEngineOnLoginHandler(), objectResolverEngineOnLoginHandler());
|
|
27198
|
+
if (client.useLegacyUnreadCount) {
|
|
27199
|
+
subscriptions.push(readReceiptSyncEngineOnLoginHandler());
|
|
27200
|
+
}
|
|
27201
|
+
else
|
|
27202
|
+
subscriptions.push(legacyReadReceiptSyncEngineOnLoginHandler());
|
|
27203
|
+
}
|
|
27204
|
+
return true;
|
|
27205
|
+
};
|
|
27206
|
+
/* end_public_function */
|
|
27207
|
+
|
|
27037
27208
|
/**
|
|
27038
27209
|
* ```js
|
|
27039
27210
|
* import { isConnected } from '@amityco/ts-sdk'
|
|
@@ -27439,6 +27610,7 @@ var index$n = /*#__PURE__*/Object.freeze({
|
|
|
27439
27610
|
login: login,
|
|
27440
27611
|
logout: logout,
|
|
27441
27612
|
secureLogout: secureLogout,
|
|
27613
|
+
resumeSession: resumeSession,
|
|
27442
27614
|
isConnected: isConnected,
|
|
27443
27615
|
getFeedSettings: getFeedSettings,
|
|
27444
27616
|
renewal: renewal,
|