@streamlayer/sdk-web-core 0.18.2 → 0.20.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/lib/auth/bypass/index.d.ts +3 -1
- package/lib/auth/bypass/index.js +16 -2
- package/lib/auth/index.js +1 -1
- package/lib/deepLink/index.d.ts +42 -0
- package/lib/deepLink/index.js +117 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/store/init.d.ts +2 -1
- package/lib/store/store.d.ts +1 -1
- package/package.json +6 -6
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AbstractAuthenticationProvider } from '@streamlayer/sdk-web-interfaces';
|
|
2
2
|
import { Transport } from '@streamlayer/sdk-web-api';
|
|
3
3
|
import { CoreStore } from '../../store/store';
|
|
4
|
+
import { DeepLinkContext } from '../../deepLink';
|
|
4
5
|
/**
|
|
5
6
|
* An authorization service manages user access by providing login, logout,
|
|
6
7
|
* authentication checks, and the ability to revoke access.
|
|
@@ -8,9 +9,10 @@ import { CoreStore } from '../../store/store';
|
|
|
8
9
|
*/
|
|
9
10
|
export declare class BypassAuth extends AbstractAuthenticationProvider {
|
|
10
11
|
private readonly $coreStore;
|
|
12
|
+
private readonly deepLink;
|
|
11
13
|
private readonly transport;
|
|
12
14
|
private readonly bypassLogin;
|
|
13
|
-
constructor(store: CoreStore, transport: Transport);
|
|
15
|
+
constructor(store: CoreStore, transport: Transport, deepLink: DeepLinkContext);
|
|
14
16
|
me: () => Promise<import("@streamlayer/sl-eslib/users/users_common_pb").User | undefined>;
|
|
15
17
|
/**
|
|
16
18
|
* Login user by token and schema.
|
package/lib/auth/bypass/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbstractAuthenticationProvider } from '@streamlayer/sdk-web-interfaces';
|
|
2
2
|
import { queries } from '@streamlayer/sdk-web-api';
|
|
3
|
+
import { DEEP_LINK_USER_ID } from '../../deepLink';
|
|
3
4
|
import { UserStorage } from '../../storage';
|
|
4
5
|
/**
|
|
5
6
|
* An authorization service manages user access by providing login, logout,
|
|
@@ -8,11 +9,13 @@ import { UserStorage } from '../../storage';
|
|
|
8
9
|
*/
|
|
9
10
|
export class BypassAuth extends AbstractAuthenticationProvider {
|
|
10
11
|
$coreStore;
|
|
12
|
+
deepLink;
|
|
11
13
|
transport;
|
|
12
14
|
bypassLogin;
|
|
13
|
-
constructor(store, transport) {
|
|
15
|
+
constructor(store, transport, deepLink) {
|
|
14
16
|
super();
|
|
15
17
|
this.$coreStore = store;
|
|
18
|
+
this.deepLink = deepLink;
|
|
16
19
|
this.transport = transport;
|
|
17
20
|
this.bypassLogin = queries.bypassLogin(this.transport);
|
|
18
21
|
this.connect();
|
|
@@ -30,12 +33,23 @@ export class BypassAuth extends AbstractAuthenticationProvider {
|
|
|
30
33
|
*/
|
|
31
34
|
login = async (schema, userKey) => {
|
|
32
35
|
this.$coreStore.getValues().userKey.setValue(userKey);
|
|
33
|
-
const
|
|
36
|
+
const inviterKey = this.deepLink.getDeepLinkData()?.[DEEP_LINK_USER_ID];
|
|
37
|
+
console.log('inviterKey', inviterKey);
|
|
38
|
+
const user = await this.bypassLogin({
|
|
39
|
+
schema,
|
|
40
|
+
userKey,
|
|
41
|
+
inviterKey,
|
|
42
|
+
init: false,
|
|
43
|
+
});
|
|
34
44
|
const token = user.meta?.jwt;
|
|
35
45
|
const userId = user.data?.id;
|
|
36
46
|
if (!token || !userId) {
|
|
37
47
|
throw new Error('internal: token missing');
|
|
38
48
|
}
|
|
49
|
+
console.log('user', user);
|
|
50
|
+
if (user.meta?.inviterKey) {
|
|
51
|
+
this.deepLink.deepLinkUsed(user.meta.inviterKey);
|
|
52
|
+
}
|
|
39
53
|
this.$coreStore.getValues().user.getStore().mutate(user);
|
|
40
54
|
this.saveUser(token, userId);
|
|
41
55
|
return token;
|
package/lib/auth/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export const storage = new UserStorage();
|
|
|
7
7
|
* Automatically login user if SDK initialized and READY.
|
|
8
8
|
*/
|
|
9
9
|
export const bypass = (instance, opts, done) => {
|
|
10
|
-
instance.auth = new BypassAuth(instance.store, instance.transport);
|
|
10
|
+
instance.auth = new BypassAuth(instance.store, instance.transport, instance.deepLink);
|
|
11
11
|
instance.stores.status.listen((status) => {
|
|
12
12
|
if (status === CoreStatus.READY) {
|
|
13
13
|
void instance.auth.reLogin();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
+
import { MapStore } from '@streamlayer/sdk-web-interfaces';
|
|
3
|
+
type DeepLinkContextData = {
|
|
4
|
+
processed: boolean;
|
|
5
|
+
hash?: string;
|
|
6
|
+
search?: string;
|
|
7
|
+
sessionId?: string;
|
|
8
|
+
data?: DeepLinkUrlParams;
|
|
9
|
+
};
|
|
10
|
+
export type DeepLinkCallback = (deepLinkData: DeepLinkContextData['data']) => void;
|
|
11
|
+
export interface DeepLinkContext {
|
|
12
|
+
$store: DeepLinkContextStore;
|
|
13
|
+
getDeepLinkData: () => DeepLinkContextData['data'];
|
|
14
|
+
deepLinkUsed: (inviterId: string) => void;
|
|
15
|
+
onDeepLinkHandlers: Set<DeepLinkCallback>;
|
|
16
|
+
}
|
|
17
|
+
export type DeepLinkContextStore = MapStore<DeepLinkContextData>;
|
|
18
|
+
declare module '@streamlayer/sdk-web-interfaces' {
|
|
19
|
+
interface StreamLayerContext {
|
|
20
|
+
deepLink: DeepLinkContext;
|
|
21
|
+
}
|
|
22
|
+
interface StreamLayerSDK {
|
|
23
|
+
onDeepLinkHandled: (cb: DeepLinkCallback) => void;
|
|
24
|
+
deepLinkHandled: () => void;
|
|
25
|
+
getInviterId: () => string | undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export declare const DEEP_LINK_PREFIX = "sldl";
|
|
29
|
+
export declare const DEEP_LINK_USER_ID = "sldl_uid";
|
|
30
|
+
export declare const DEEP_LINK_EVENT_ID = "sldl_eid";
|
|
31
|
+
export declare const DEEP_LINK_EXTERNAL_EVENT_ID = "sldl_e_eid";
|
|
32
|
+
export type DeepLinkUrlParams = {
|
|
33
|
+
[DEEP_LINK_USER_ID]?: string;
|
|
34
|
+
[DEEP_LINK_EVENT_ID]?: string;
|
|
35
|
+
[DEEP_LINK_EXTERNAL_EVENT_ID]?: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Bypass authorization, used for login with external token.
|
|
39
|
+
* Automatically login user if SDK initialized and READY.
|
|
40
|
+
*/
|
|
41
|
+
export declare const deepLink: (instance: StreamLayerContext, opts: unknown, done: () => void) => void;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createMapStore, MapStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
+
import { Storage } from '@streamlayer/sdk-web-storage';
|
|
3
|
+
class DeepLinkContextStorage extends Storage {
|
|
4
|
+
get = this.read;
|
|
5
|
+
set = this.write;
|
|
6
|
+
del = this.remove;
|
|
7
|
+
constructor() {
|
|
8
|
+
super('deep-link-context', window.sessionStorage);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export const DEEP_LINK_PREFIX = 'sldl';
|
|
12
|
+
export const DEEP_LINK_USER_ID = 'sldl_uid';
|
|
13
|
+
export const DEEP_LINK_EVENT_ID = 'sldl_eid';
|
|
14
|
+
export const DEEP_LINK_EXTERNAL_EVENT_ID = 'sldl_e_eid';
|
|
15
|
+
const ALLOWED_PARAMS = new Set([DEEP_LINK_USER_ID, DEEP_LINK_EVENT_ID, DEEP_LINK_EXTERNAL_EVENT_ID]);
|
|
16
|
+
const pickOnlyAllowedParams = (params) => {
|
|
17
|
+
const result = {};
|
|
18
|
+
for (const [key, value] of params) {
|
|
19
|
+
if (ALLOWED_PARAMS.has(key)) {
|
|
20
|
+
result[key] = value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
const parseDeepLinkData = ({ hash, search }) => {
|
|
26
|
+
if (hash && hash.includes(DEEP_LINK_PREFIX)) {
|
|
27
|
+
const params = new URLSearchParams(hash.substr(1));
|
|
28
|
+
return pickOnlyAllowedParams(params);
|
|
29
|
+
}
|
|
30
|
+
if (search && search.includes(DEEP_LINK_PREFIX)) {
|
|
31
|
+
const params = new URLSearchParams(search);
|
|
32
|
+
return pickOnlyAllowedParams(params);
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
36
|
+
const pickDeepLinkData = ($deepLinkContext) => {
|
|
37
|
+
const { hash, search, processed } = $deepLinkContext.getValues();
|
|
38
|
+
console.log('get deep link data from', { hash, search, processed });
|
|
39
|
+
if (!processed) {
|
|
40
|
+
const result = parseDeepLinkData({ hash, search });
|
|
41
|
+
console.log('parse deep link result', result);
|
|
42
|
+
if (result) {
|
|
43
|
+
$deepLinkContext.setValue('data', result);
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Bypass authorization, used for login with external token.
|
|
51
|
+
* Automatically login user if SDK initialized and READY.
|
|
52
|
+
*/
|
|
53
|
+
export const deepLink = (instance, opts, done) => {
|
|
54
|
+
const storage = new DeepLinkContextStorage();
|
|
55
|
+
const initStoreStr = storage.get('sl-deep-link-store');
|
|
56
|
+
let initStore = { processed: false };
|
|
57
|
+
if (initStoreStr) {
|
|
58
|
+
try {
|
|
59
|
+
initStore = JSON.parse(initStoreStr);
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
console.log(e);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
instance.deepLink = {
|
|
66
|
+
$store: new MapStore(createMapStore(initStore), 'deep-link-context'),
|
|
67
|
+
getDeepLinkData: () => {
|
|
68
|
+
const { data, processed } = instance.deepLink.$store.getValues();
|
|
69
|
+
return !processed ? data : undefined;
|
|
70
|
+
},
|
|
71
|
+
// we are logged in with inviter id
|
|
72
|
+
deepLinkUsed: (inviterId) => {
|
|
73
|
+
instance.deepLink.$store.setValue('processed', true);
|
|
74
|
+
storage.set('sl-inviter-id', inviterId);
|
|
75
|
+
storage.del('sl-location');
|
|
76
|
+
},
|
|
77
|
+
onDeepLinkHandlers: new Set(),
|
|
78
|
+
};
|
|
79
|
+
instance.deepLink.$store.subscribe((value) => storage.set('sl-deep-link-store', JSON.stringify(value)));
|
|
80
|
+
instance.sdk.getInviterId = () => {
|
|
81
|
+
return storage.get('sl-inviter-id');
|
|
82
|
+
};
|
|
83
|
+
instance.sdk.onDeepLinkHandled = (cb) => {
|
|
84
|
+
instance.deepLink.onDeepLinkHandlers.add(cb);
|
|
85
|
+
};
|
|
86
|
+
// sdk initialized after login by invite
|
|
87
|
+
instance.sdk.deepLinkHandled = () => {
|
|
88
|
+
storage.del('sl-inviter-id');
|
|
89
|
+
instance.deepLink.$store.setValue('processed', true);
|
|
90
|
+
const { data } = instance.deepLink.$store.getValues();
|
|
91
|
+
for (const handler of instance.deepLink.onDeepLinkHandlers) {
|
|
92
|
+
try {
|
|
93
|
+
handler(data);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
console.error(err);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
let sessionId = storage.get('sl-session-id');
|
|
101
|
+
if (!sessionId) {
|
|
102
|
+
const { hash, search } = window.location;
|
|
103
|
+
sessionId = Date.now().toString();
|
|
104
|
+
storage.set('sl-session-id', sessionId);
|
|
105
|
+
storage.set('sl-location', JSON.stringify({ hash, search }));
|
|
106
|
+
instance.deepLink.$store.getStore().set({ sessionId, hash, search, processed: false });
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const initialLocation = storage.get('sl-location');
|
|
110
|
+
if (initialLocation) {
|
|
111
|
+
const parsed = JSON.parse(initialLocation);
|
|
112
|
+
instance.deepLink.$store.getStore().set({ sessionId, hash: parsed.hash, search: parsed.search, processed: false });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
pickDeepLinkData(instance.deepLink.$store);
|
|
116
|
+
done();
|
|
117
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
|
|
2
2
|
export { bypass, storage } from './auth';
|
|
3
3
|
export { store } from './store';
|
|
4
|
+
export { deepLink, type DeepLinkUrlParams, type DeepLinkCallback, DEEP_LINK_PREFIX, DEEP_LINK_USER_ID, DEEP_LINK_EVENT_ID, DEEP_LINK_EXTERNAL_EVENT_ID, } from './deepLink';
|
|
4
5
|
import './store';
|
|
5
6
|
import './auth';
|
|
6
7
|
declare module '@streamlayer/sdk-web-interfaces' {
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CoreStatus } from './store/store';
|
|
2
2
|
export { bypass, storage } from './auth';
|
|
3
3
|
export { store } from './store';
|
|
4
|
+
export { deepLink, DEEP_LINK_PREFIX, DEEP_LINK_USER_ID, DEEP_LINK_EVENT_ID, DEEP_LINK_EXTERNAL_EVENT_ID, } from './deepLink';
|
|
4
5
|
import './store';
|
|
5
6
|
import './auth';
|
|
6
7
|
/**
|
package/lib/store/init.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SingleStore, ApiStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
2
|
import { Transport } from '@streamlayer/sdk-web-api';
|
|
3
|
+
import type { BypassAuthResponse } from '@streamlayer/sl-eslib/users/users_pb';
|
|
3
4
|
export declare enum CoreStatus {
|
|
4
5
|
DISABLED = "disabled",
|
|
5
6
|
INITIALIZATION = "initialization",
|
|
@@ -13,7 +14,7 @@ export declare const initializeStore: (transport: Transport) => {
|
|
|
13
14
|
readonly providerStreamId: SingleStore<unknown, import("nanostores").WritableAtom<string | undefined>>;
|
|
14
15
|
readonly slStreamId: ApiStore<string | undefined, import("@nanostores/query").FetcherStore<string | undefined, any>>;
|
|
15
16
|
readonly streamSettings: ApiStore<import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").StreamSettings | undefined, import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").StreamSettings | undefined, any>>;
|
|
16
|
-
readonly user: ApiStore<import("@streamlayer/sl-eslib/users/users_pb").MeResponse
|
|
17
|
+
readonly user: ApiStore<import("@streamlayer/sl-eslib/users/users_pb").MeResponse & BypassAuthResponse, import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/users/users_pb").MeResponse & BypassAuthResponse, any>>;
|
|
17
18
|
readonly userKey: SingleStore<unknown, import("nanostores").WritableAtom<string | undefined>>;
|
|
18
19
|
readonly userToken: SingleStore<unknown, import("nanostores").WritableAtom<string | undefined>>;
|
|
19
20
|
readonly userSettings: ApiStore<import("@streamlayer/sl-eslib/sdkSettings/client/client_pb").ClientSettings | undefined, import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/sdkSettings/client/client_pb").ClientSettings | undefined, any>>;
|
package/lib/store/store.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export declare class CoreStore extends AbstractStore<CoreStoreInstance> {
|
|
|
53
53
|
readonly providerStreamId: import("@streamlayer/sdk-web-interfaces").SingleStore<unknown, import("nanostores").WritableAtom<string | undefined>>;
|
|
54
54
|
readonly slStreamId: import("@streamlayer/sdk-web-interfaces").ApiStore<string | undefined, import("@nanostores/query").FetcherStore<string | undefined, any>>;
|
|
55
55
|
readonly streamSettings: import("@streamlayer/sdk-web-interfaces").ApiStore<import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").StreamSettings | undefined, import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").StreamSettings | undefined, any>>;
|
|
56
|
-
readonly user: import("@streamlayer/sdk-web-interfaces").ApiStore<import("@streamlayer/sl-eslib/users/users_pb").MeResponse
|
|
56
|
+
readonly user: import("@streamlayer/sdk-web-interfaces").ApiStore<import("@streamlayer/sl-eslib/users/users_pb").MeResponse & import("@streamlayer/sl-eslib/users/users_pb").BypassAuthResponse, import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/users/users_pb").MeResponse & import("@streamlayer/sl-eslib/users/users_pb").BypassAuthResponse, any>>;
|
|
57
57
|
readonly userKey: import("@streamlayer/sdk-web-interfaces").SingleStore<unknown, import("nanostores").WritableAtom<string | undefined>>;
|
|
58
58
|
readonly userToken: import("@streamlayer/sdk-web-interfaces").SingleStore<unknown, import("nanostores").WritableAtom<string | undefined>>;
|
|
59
59
|
readonly userSettings: import("@streamlayer/sdk-web-interfaces").ApiStore<import("@streamlayer/sl-eslib/sdkSettings/client/client_pb").ClientSettings | undefined, import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/sdkSettings/client/client_pb").ClientSettings | undefined, any>>;
|
package/package.json
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
},
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"@nanostores/query": "^0.2.8",
|
|
8
|
-
"@streamlayer/sl-eslib": "^5.
|
|
8
|
+
"@streamlayer/sl-eslib": "^5.67.0",
|
|
9
9
|
"nanostores": "^0.9.5",
|
|
10
|
-
"@streamlayer/sdk-web-api": "^0.
|
|
11
|
-
"@streamlayer/sdk-web-interfaces": "^0.20.
|
|
12
|
-
"@streamlayer/sdk-web-storage": "^0.
|
|
13
|
-
"@streamlayer/sdk-web-types": "^0.
|
|
10
|
+
"@streamlayer/sdk-web-api": "^0.21.1",
|
|
11
|
+
"@streamlayer/sdk-web-interfaces": "^0.20.3",
|
|
12
|
+
"@streamlayer/sdk-web-storage": "^0.4.1",
|
|
13
|
+
"@streamlayer/sdk-web-types": "^0.22.1"
|
|
14
14
|
},
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"default": "./lib/auth/index.js"
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
|
-
"version": "0.
|
|
42
|
+
"version": "0.20.0",
|
|
43
43
|
"type": "module",
|
|
44
44
|
"main": "./lib/index.js",
|
|
45
45
|
"module": "./lib/index.js",
|