@amedia/user 0.14.8-withPublicationDomain → 0.14.8
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/README.md +0 -1
- package/dist/user.d.ts +2 -5
- package/package.json +1 -1
- package/src/actions/authorization.spec.ts +4 -12
- package/src/actions/authorization.ts +2 -5
- package/src/clients/mercury.spec.ts +13 -15
- package/src/clients/mercury.ts +3 -3
- package/src/eventHandlers/activation.ts +1 -6
- package/src/lib/PaywallUnlockRequest.spec.ts +2 -31
- package/src/lib/PaywallUnlockRequest.ts +1 -11
package/README.md
CHANGED
|
@@ -151,7 +151,6 @@ To unlock the paywall, you first need to create a new `PaywallUnlockRequest`. On
|
|
|
151
151
|
to `requestedUrl`. (Provides more flexibility than `reloadOnAccess()`)
|
|
152
152
|
- `withAccessFeatures(accessFeatures: string[])`: Specify what access features will give access to the current content.
|
|
153
153
|
The default is [`newspaper`].
|
|
154
|
-
- `withPublicationDomain(domain: string)`: Specify which site we to attempt activation against. The default is the current domain. This is mostly useful for direktesport.no which often knows what domain the user came from.
|
|
155
154
|
|
|
156
155
|
More details about the access model can be found here: [Access model](https://amedia.slite.com/app/channels/hOlqx3Miez/notes/nhG2Ok39BN)
|
|
157
156
|
|
package/dist/user.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ declare module '@amedia/user/actions/authorization' {
|
|
|
16
16
|
accessFeatures: string[];
|
|
17
17
|
};
|
|
18
18
|
export const getAccess: (accessFeatures?: string[]) => Promise<Pick<UserAttributes, 'access'>>;
|
|
19
|
-
export const activate: (accessFeatures: string[]
|
|
19
|
+
export const activate: (accessFeatures: string[]) => Promise<string[]>;
|
|
20
20
|
export const hasAccess: (currentAccessFeatures: string[], requestedAccessFeatures: string[]) => boolean;
|
|
21
21
|
export const setUserAccessCookie: (publicationDomain: string) => Promise<SetCookieResponse>;
|
|
22
22
|
export const setNonUserAccessCookie: (publicationDomain: string) => Promise<SetCookieResponse>;
|
|
@@ -240,7 +240,7 @@ declare module '@amedia/user/clients/jupiter.spec' {
|
|
|
240
240
|
declare module '@amedia/user/clients/mercury' {
|
|
241
241
|
export class UserDeactivatedError extends Error {
|
|
242
242
|
}
|
|
243
|
-
export const activateSubscription: (accessFeatures: string[]
|
|
243
|
+
export const activateSubscription: (accessFeatures: string[]) => Promise<string[]>;
|
|
244
244
|
|
|
245
245
|
}
|
|
246
246
|
declare module '@amedia/user/clients/mercury.spec' {
|
|
@@ -291,7 +291,6 @@ declare module '@amedia/user/eventHandlers/activation' {
|
|
|
291
291
|
interface WindowEventMap {
|
|
292
292
|
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
293
293
|
accessFeatures: string[];
|
|
294
|
-
publicationDomain?: string;
|
|
295
294
|
}>;
|
|
296
295
|
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
297
296
|
accessFeatures: string[];
|
|
@@ -487,8 +486,6 @@ declare module '@amedia/user/lib/PaywallUnlockRequest' {
|
|
|
487
486
|
private requestedAccessFeatures;
|
|
488
487
|
private reload;
|
|
489
488
|
private requestedUrl;
|
|
490
|
-
private publicationDomain;
|
|
491
|
-
withPublicationDomain(publicationDomain: string): this;
|
|
492
489
|
withAccessFeatures(accessFeatures: string[]): this;
|
|
493
490
|
reloadOnAccess(): this;
|
|
494
491
|
redirectOnAccess(requestedUrl: string): this;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.14.8
|
|
3
|
+
"version": "0.14.8",
|
|
4
4
|
"description": "Client lib for working with aID user and associated data",
|
|
5
5
|
"repository": "https://github.com/amedia/amedia-user-js",
|
|
6
6
|
"author": "Amedia Utvikling (ID og data)",
|
|
@@ -49,13 +49,8 @@ describe('activate', () => {
|
|
|
49
49
|
.mockResolvedValue(['whatever']);
|
|
50
50
|
});
|
|
51
51
|
it('Returns whatever mercury returns', async () => {
|
|
52
|
-
expect(await activate(['newspaper']
|
|
53
|
-
|
|
54
|
-
]);
|
|
55
|
-
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
56
|
-
['newspaper'],
|
|
57
|
-
'www.telen.no'
|
|
58
|
-
);
|
|
52
|
+
expect(await activate(['newspaper'])).toEqual(['whatever']);
|
|
53
|
+
expect(mercury.activateSubscription).toHaveBeenCalledWith(['newspaper']);
|
|
59
54
|
});
|
|
60
55
|
});
|
|
61
56
|
|
|
@@ -67,11 +62,8 @@ describe('activate', () => {
|
|
|
67
62
|
jest.spyOn(authentication, 'logout').mockResolvedValue(undefined);
|
|
68
63
|
});
|
|
69
64
|
it('Returns whatever mercury returns', async () => {
|
|
70
|
-
expect(await activate([]
|
|
71
|
-
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
72
|
-
[],
|
|
73
|
-
'www.telen.no'
|
|
74
|
-
);
|
|
65
|
+
expect(await activate([])).toEqual([]);
|
|
66
|
+
expect(mercury.activateSubscription).toHaveBeenCalledWith([]);
|
|
75
67
|
expect(authentication.logout).toHaveBeenCalled();
|
|
76
68
|
});
|
|
77
69
|
});
|
|
@@ -16,12 +16,9 @@ export const getAccess = (
|
|
|
16
16
|
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
17
17
|
checkForAccess(getCurrentPublicationDomain(), accessFeatures);
|
|
18
18
|
|
|
19
|
-
export const activate = async (
|
|
20
|
-
accessFeatures: string[],
|
|
21
|
-
publicationDomain: string
|
|
22
|
-
): Promise<string[]> => {
|
|
19
|
+
export const activate = async (accessFeatures: string[]): Promise<string[]> => {
|
|
23
20
|
try {
|
|
24
|
-
return await activateSubscription(accessFeatures
|
|
21
|
+
return await activateSubscription(accessFeatures);
|
|
25
22
|
} catch (error) {
|
|
26
23
|
if (error instanceof UserDeactivatedError) {
|
|
27
24
|
await logout();
|
|
@@ -6,18 +6,16 @@ describe('activateSubscription()', () => {
|
|
|
6
6
|
fetch.mockResponse(JSON.stringify({ status: 'user_deactivated' }), {
|
|
7
7
|
status: 401,
|
|
8
8
|
});
|
|
9
|
-
await expect(
|
|
10
|
-
|
|
11
|
-
)
|
|
9
|
+
await expect(activateSubscription(['newspaper'])).rejects.toThrow(
|
|
10
|
+
UserDeactivatedError
|
|
11
|
+
);
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
it('should return empty list if user not found', async () => {
|
|
15
15
|
fetch.mockResponse(JSON.stringify({ status: 'no_session_found' }), {
|
|
16
16
|
status: 401,
|
|
17
17
|
});
|
|
18
|
-
await expect(
|
|
19
|
-
activateSubscription(['newspaper'], 'www.telen.no')
|
|
20
|
-
).resolves.toEqual([]);
|
|
18
|
+
await expect(activateSubscription(['newspaper'])).resolves.toEqual([]);
|
|
21
19
|
});
|
|
22
20
|
|
|
23
21
|
it('should return list if access features even if requested access is not "granted"', async () => {
|
|
@@ -28,9 +26,9 @@ describe('activateSubscription()', () => {
|
|
|
28
26
|
access_features: ['newspaper'],
|
|
29
27
|
})
|
|
30
28
|
);
|
|
31
|
-
await expect(
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
await expect(activateSubscription(['newspaper'])).resolves.toEqual([
|
|
30
|
+
'newspaper',
|
|
31
|
+
]);
|
|
34
32
|
});
|
|
35
33
|
|
|
36
34
|
it('should return list of access features even if status is not "success"', async () => {
|
|
@@ -41,9 +39,9 @@ describe('activateSubscription()', () => {
|
|
|
41
39
|
access_features: ['newspaper'],
|
|
42
40
|
})
|
|
43
41
|
);
|
|
44
|
-
await expect(
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
await expect(activateSubscription(['newspaper'])).resolves.toEqual([
|
|
43
|
+
'newspaper',
|
|
44
|
+
]);
|
|
47
45
|
});
|
|
48
46
|
|
|
49
47
|
it('should return ["newspaper"] if status is "success" and access is "granted"', async () => {
|
|
@@ -54,8 +52,8 @@ describe('activateSubscription()', () => {
|
|
|
54
52
|
access_features: ['newspaper'],
|
|
55
53
|
})
|
|
56
54
|
);
|
|
57
|
-
await expect(
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
await expect(activateSubscription(['newspaper'])).resolves.toEqual([
|
|
56
|
+
'newspaper',
|
|
57
|
+
]);
|
|
60
58
|
});
|
|
61
59
|
});
|
package/src/clients/mercury.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import log from '../utils/log';
|
|
2
|
+
import { getCurrentPublicationDomain } from '../utils/url';
|
|
2
3
|
|
|
3
4
|
export class UserDeactivatedError extends Error {}
|
|
4
5
|
|
|
@@ -9,11 +10,10 @@ const EXPECTED_ERRORS = [
|
|
|
9
10
|
];
|
|
10
11
|
|
|
11
12
|
export const activateSubscription = async (
|
|
12
|
-
accessFeatures: string[]
|
|
13
|
-
publicationDomain: string
|
|
13
|
+
accessFeatures: string[]
|
|
14
14
|
): Promise<string[]> =>
|
|
15
15
|
fetch(
|
|
16
|
-
`/api/mercury/v2/access/activate?publication_wwwdomain=${
|
|
16
|
+
`/api/mercury/v2/access/activate?publication_wwwdomain=${getCurrentPublicationDomain()}&access_features=${accessFeatures.join(
|
|
17
17
|
','
|
|
18
18
|
)}`,
|
|
19
19
|
{
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
2
|
import { activate } from '../actions/authorization';
|
|
3
3
|
import getGlobalThis from '../utils/getGlobalThis';
|
|
4
|
-
import { getCurrentPublicationDomain } from '../utils/url';
|
|
5
4
|
|
|
6
5
|
export enum ActivationEvents {
|
|
7
6
|
REQUEST_ACTIVATION = 'amedia-user.activation.request-activation',
|
|
@@ -12,7 +11,6 @@ declare global {
|
|
|
12
11
|
interface WindowEventMap {
|
|
13
12
|
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
14
13
|
accessFeatures: string[];
|
|
15
|
-
publicationDomain?: string;
|
|
16
14
|
}>;
|
|
17
15
|
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
18
16
|
accessFeatures: string[];
|
|
@@ -22,10 +20,7 @@ declare global {
|
|
|
22
20
|
|
|
23
21
|
export const listenForActivationRequests = () => {
|
|
24
22
|
listenWithExclusivity(ActivationEvents.REQUEST_ACTIVATION, (evt) => {
|
|
25
|
-
activate(
|
|
26
|
-
evt.detail.accessFeatures,
|
|
27
|
-
evt.detail.publicationDomain || getCurrentPublicationDomain()
|
|
28
|
-
).then((accessFeatures) => {
|
|
23
|
+
activate(evt.detail.accessFeatures).then((accessFeatures) => {
|
|
29
24
|
dispatch(
|
|
30
25
|
new CustomEvent(ActivationEvents.ON_ACTIVATION_COMPLETE, {
|
|
31
26
|
detail: { accessFeatures },
|
|
@@ -41,15 +41,9 @@ function itShouldNotAttemptActivation() {
|
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function itShouldAttemptActivationWithAccessFeatures(
|
|
45
|
-
accessFeatures: string[],
|
|
46
|
-
publicationDomain: string = 'localhost'
|
|
47
|
-
) {
|
|
44
|
+
function itShouldAttemptActivationWithAccessFeatures(accessFeatures: string[]) {
|
|
48
45
|
it('should attempt activation with access features', () => {
|
|
49
|
-
expect(authorization.activate).toHaveBeenCalledWith(
|
|
50
|
-
accessFeatures,
|
|
51
|
-
publicationDomain
|
|
52
|
-
);
|
|
46
|
+
expect(authorization.activate).toHaveBeenCalledWith(accessFeatures);
|
|
53
47
|
});
|
|
54
48
|
}
|
|
55
49
|
|
|
@@ -263,29 +257,6 @@ describe('PaywallUnlocker', () => {
|
|
|
263
257
|
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
264
258
|
itShouldNotRedirect();
|
|
265
259
|
});
|
|
266
|
-
|
|
267
|
-
describe('with specific publicationDomain', () => {
|
|
268
|
-
beforeEach(async () => {
|
|
269
|
-
unlockResponse = await new PaywallUnlockRequest()
|
|
270
|
-
.withPublicationDomain('www.telen.no')
|
|
271
|
-
.tryUnlock();
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('should give access with access features', () => {
|
|
275
|
-
expect(unlockResponse).toEqual({
|
|
276
|
-
isLoggedIn: true,
|
|
277
|
-
hasAccess: true,
|
|
278
|
-
accessFeatures: ['newspaper'],
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
itShouldAttemptActivationWithAccessFeatures(
|
|
283
|
-
['newspaper'],
|
|
284
|
-
'www.telen.no'
|
|
285
|
-
);
|
|
286
|
-
itShouldNotReload();
|
|
287
|
-
itShouldNotRedirect();
|
|
288
|
-
});
|
|
289
260
|
});
|
|
290
261
|
|
|
291
262
|
describe('and activation does not give access', () => {
|
|
@@ -50,13 +50,6 @@ class PaywallUnlockRequest {
|
|
|
50
50
|
|
|
51
51
|
private requestedUrl: string | undefined;
|
|
52
52
|
|
|
53
|
-
private publicationDomain: string = getCurrentPublicationDomain();
|
|
54
|
-
|
|
55
|
-
public withPublicationDomain(publicationDomain: string): this {
|
|
56
|
-
this.publicationDomain = publicationDomain;
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
53
|
public withAccessFeatures(accessFeatures: string[]): this {
|
|
61
54
|
this.requestedAccessFeatures = accessFeatures;
|
|
62
55
|
return this;
|
|
@@ -165,10 +158,7 @@ class PaywallUnlockRequest {
|
|
|
165
158
|
}
|
|
166
159
|
|
|
167
160
|
private async activateAccess(requestedAccessFeatures: AccessFeatures) {
|
|
168
|
-
const accessFeatures = await activate(
|
|
169
|
-
this.requestedAccessFeatures,
|
|
170
|
-
this.publicationDomain
|
|
171
|
-
);
|
|
161
|
+
const accessFeatures = await activate(this.requestedAccessFeatures);
|
|
172
162
|
return {
|
|
173
163
|
denied: !hasAccess(accessFeatures, requestedAccessFeatures),
|
|
174
164
|
accessFeatures,
|