@amedia/user 0.9.0 → 0.10.1
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/.codeclimate.yml +1 -0
- package/dist/actions/authentication.d.ts +2 -1
- package/dist/actions/authentication.spec.d.ts +4 -0
- package/dist/actions/authorization.d.ts +5 -1
- package/dist/exceptions/PaywallError.d.ts +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/lib/PaywallUnlocker.d.ts +16 -0
- package/dist/lib/PaywallUnlocker.spec.d.ts +4 -0
- package/dist/lib/User.d.ts +1 -1
- package/dist/lib/cookie.d.ts +1 -1
- package/dist/utils/location.d.ts +3 -0
- package/esm/actions/authentication.d.ts +5 -0
- package/esm/actions/authorization.d.ts +7 -0
- package/esm/actions/preferences.d.ts +9 -0
- package/esm/actions/preferences.spec.d.ts +1 -0
- package/esm/actions/storage.d.ts +1 -0
- package/esm/actions/userData.d.ts +2 -0
- package/esm/cache/engines/global.d.ts +21 -0
- package/esm/cache/engines/global.spec.d.ts +1 -0
- package/esm/cache/engines/indexeddb.d.ts +7 -0
- package/esm/cache/index.d.ts +11 -0
- package/esm/cache/index.spec.d.ts +4 -0
- package/esm/cache/types.d.ts +11 -0
- package/esm/clients/europa.d.ts +28 -0
- package/esm/clients/europa.spec.d.ts +1 -0
- package/esm/clients/jupiter.d.ts +30 -0
- package/esm/clients/jupiter.spec.d.ts +1 -0
- package/esm/clients/mercury.d.ts +18 -0
- package/esm/clients/mercury.spec.d.ts +4 -0
- package/esm/clients/nebula.d.ts +2 -0
- package/esm/clients/nebula.spec.d.ts +1 -0
- package/esm/clients/pluto.d.ts +3 -0
- package/esm/clients/pluto.spec.d.ts +1 -0
- package/esm/eventHandlers/activation.d.ts +15 -0
- package/esm/eventHandlers/authentication.d.ts +17 -0
- package/esm/eventHandlers/userAttributes.d.ts +20 -0
- package/esm/exceptions/PaywallError.d.ts +7 -0
- package/esm/index-authentcation.spec.d.ts +4 -0
- package/esm/index-subscription.spec.d.ts +4 -0
- package/esm/index.d.ts +23 -0
- package/esm/lib/PaywallUnlocker.d.ts +16 -0
- package/esm/lib/PaywallUnlocker.spec.d.ts +4 -0
- package/esm/lib/User.d.ts +21 -0
- package/esm/lib/cookie.d.ts +5 -0
- package/esm/lib/rpc.d.ts +6 -0
- package/esm/store/index.d.ts +22 -0
- package/esm/store/index.spec.d.ts +1 -0
- package/esm/types.d.ts +21 -0
- package/esm/user.js +2 -0
- package/esm/user.js.map +1 -0
- package/esm/utils/customEventWrapper.d.ts +17 -0
- package/esm/utils/deepEquals.d.ts +4 -0
- package/esm/utils/getOrUpdate.d.ts +6 -0
- package/esm/utils/getOrUpdate.spec.d.ts +1 -0
- package/esm/utils/location.d.ts +2 -0
- package/esm/utils/log.d.ts +26 -0
- package/esm/utils/queue.d.ts +12 -0
- package/esm/utils/url.d.ts +3 -0
- package/esm/utils/url.spec.d.ts +1 -0
- package/package.json +1 -1
- package/src/actions/authentication.spec.ts +41 -0
- package/src/actions/authentication.ts +17 -5
- package/src/actions/authorization.ts +58 -3
- package/src/actions/preferences.spec.ts +2 -2
- package/src/clients/nebula.ts +1 -1
- package/src/exceptions/PaywallError.ts +13 -0
- package/src/index-subscription.spec.ts +10 -10
- package/src/index.ts +3 -0
- package/src/lib/PaywallUnlocker.spec.ts +196 -0
- package/src/lib/PaywallUnlocker.ts +113 -0
- package/src/lib/User.ts +2 -2
- package/src/lib/cookie.ts +1 -1
- package/src/utils/location.ts +8 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
activate,
|
|
3
|
+
getAccess,
|
|
4
|
+
hasAccess,
|
|
5
|
+
hasRecentlyTriedToUnlockPaywall,
|
|
6
|
+
setAccessCookies,
|
|
7
|
+
setHasTriedToUnlockPaywall,
|
|
8
|
+
} from '../actions/authorization';
|
|
9
|
+
import { isLoggedIn } from '../actions/authentication';
|
|
10
|
+
import { getCurrentSession } from '../clients/pluto';
|
|
11
|
+
import { getCurrentPublicationDomain } from '../utils/url';
|
|
12
|
+
import * as location from '../utils/location';
|
|
13
|
+
import PaywallError from '../exceptions/PaywallError';
|
|
14
|
+
|
|
15
|
+
class PaywallUnlocker {
|
|
16
|
+
private requestedAccessFeatures: string[] = ['newspaper'];
|
|
17
|
+
|
|
18
|
+
private reload: boolean = false;
|
|
19
|
+
|
|
20
|
+
private requestedUrl: string | undefined;
|
|
21
|
+
|
|
22
|
+
public withAccessFeatures(accessFeatures: string[]): this {
|
|
23
|
+
this.requestedAccessFeatures = accessFeatures;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public reloadOnAccess(): this {
|
|
28
|
+
this.reload = true;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public redirectOnAccess(requestedUrl: string): this {
|
|
33
|
+
this.requestedUrl = requestedUrl;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async tryUnlock(): Promise<{
|
|
38
|
+
isLoggedIn: boolean;
|
|
39
|
+
hasAccess: boolean;
|
|
40
|
+
accessFeatures: string[];
|
|
41
|
+
}> {
|
|
42
|
+
if (hasRecentlyTriedToUnlockPaywall()) {
|
|
43
|
+
throw new PaywallError(
|
|
44
|
+
'Unlock check already performed.',
|
|
45
|
+
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
!isLoggedIn() &&
|
|
51
|
+
(await getCurrentSession().then(() => !isLoggedIn()))
|
|
52
|
+
) {
|
|
53
|
+
return {
|
|
54
|
+
isLoggedIn: false,
|
|
55
|
+
hasAccess: false,
|
|
56
|
+
accessFeatures: [],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return this.resolveActualAccessFeatures().then((accessFeatures) =>
|
|
61
|
+
hasAccess(accessFeatures, this.requestedAccessFeatures)
|
|
62
|
+
? this.setAccessCookiesAndPerformRequestedActions(accessFeatures)
|
|
63
|
+
: {
|
|
64
|
+
isLoggedIn: true,
|
|
65
|
+
hasAccess: false,
|
|
66
|
+
accessFeatures,
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private async resolveActualAccessFeatures() {
|
|
72
|
+
let accessFeatures = await getAccess(this.requestedAccessFeatures).then(
|
|
73
|
+
(a) => a.access || []
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (!hasAccess(accessFeatures, this.requestedAccessFeatures)) {
|
|
77
|
+
if (!(await activate(this.requestedAccessFeatures))) {
|
|
78
|
+
return accessFeatures;
|
|
79
|
+
}
|
|
80
|
+
accessFeatures = await getAccess(this.requestedAccessFeatures).then(
|
|
81
|
+
(a) => a.access || []
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return accessFeatures;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private setAccessCookiesAndPerformRequestedActions(
|
|
88
|
+
actualAccessFeatures: string[]
|
|
89
|
+
) {
|
|
90
|
+
return setAccessCookies(getCurrentPublicationDomain()).then((success) => {
|
|
91
|
+
if (!success) {
|
|
92
|
+
throw new PaywallError(
|
|
93
|
+
'Could not set access cookies',
|
|
94
|
+
PaywallError.ERROR_ENABLING_ACCESS_FAILED
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
setHasTriedToUnlockPaywall();
|
|
98
|
+
if (this.reload) {
|
|
99
|
+
location.reload();
|
|
100
|
+
}
|
|
101
|
+
if (this.requestedUrl) {
|
|
102
|
+
location.replace(this.requestedUrl);
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
isLoggedIn: true,
|
|
106
|
+
hasAccess: true,
|
|
107
|
+
accessFeatures: actualAccessFeatures,
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export default PaywallUnlocker;
|
package/src/lib/User.ts
CHANGED
|
@@ -16,7 +16,7 @@ class User<
|
|
|
16
16
|
) {}
|
|
17
17
|
|
|
18
18
|
withAttributes<NewAttributes extends Array<keyof UA>>(
|
|
19
|
-
|
|
19
|
+
attributes: NewAttributes
|
|
20
20
|
) {
|
|
21
21
|
return new User<
|
|
22
22
|
UA,
|
|
@@ -27,7 +27,7 @@ class User<
|
|
|
27
27
|
attributes: Pick<UA, Attributes[number] & NewAttributes[number]>;
|
|
28
28
|
}
|
|
29
29
|
>(
|
|
30
|
-
[...((this.attributes || ([] as unknown)) as Attributes), ...
|
|
30
|
+
[...((this.attributes || ([] as unknown)) as Attributes), ...attributes],
|
|
31
31
|
this.namespaces
|
|
32
32
|
);
|
|
33
33
|
}
|
package/src/lib/cookie.ts
CHANGED