@amedia/user 0.10.0 → 0.11.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/.eik/integrity.json +36 -0
- package/dist/actions/authentication.spec.d.ts +4 -0
- package/dist/actions/authorization.d.ts +3 -1
- package/dist/actions/storage.d.ts +3 -0
- package/dist/autologin/index.d.ts +1 -0
- package/dist/autologin/thirdPartyCookie.d.ts +3 -0
- package/dist/autologin/thirdPartyCookie.spec.d.ts +1 -0
- package/dist/eventHandlers/userAttributes.d.ts +4 -0
- package/dist/index-storage.spec.d.ts +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -1
- package/dist/janitor/index.d.ts +1 -0
- package/dist/janitor/jobs/accessCookieRefresher.d.ts +1 -0
- package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +4 -0
- package/dist/types.d.ts +1 -0
- package/dist/utils/location.d.ts +1 -0
- package/dist/utils/url.d.ts +3 -0
- package/eik.json +9 -0
- package/package.json +12 -2
- package/rollup.config.js +14 -0
- package/src/actions/authentication.spec.ts +41 -0
- package/src/actions/authentication.ts +6 -7
- package/src/actions/authorization.ts +18 -5
- package/src/actions/storage.ts +6 -1
- package/src/actions/userData.ts +2 -1
- package/src/autologin/index.ts +7 -0
- package/src/autologin/thirdPartyCookie.spec.ts +108 -0
- package/src/autologin/thirdPartyCookie.ts +68 -0
- package/src/clients/mercury.ts +7 -3
- package/src/clients/pluto.spec.ts +1 -0
- package/src/clients/pluto.ts +2 -0
- package/src/eventHandlers/userAttributes.ts +8 -1
- package/src/index-storage.spec.ts +96 -0
- package/src/index-subscription.spec.ts +0 -54
- package/src/index.ts +8 -1
- package/src/janitor/index.ts +11 -0
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +230 -0
- package/src/janitor/jobs/accessCookieRefresher.ts +96 -0
- package/src/types.ts +1 -0
- package/src/utils/location.ts +6 -4
- package/src/utils/url.ts +14 -1
- package/test.html +12 -0
- package/esm/user.js +0 -6499
- package/esm/user.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
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)",
|
|
@@ -8,13 +8,15 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"test": "jest --coverage",
|
|
10
10
|
"lint": "eslint ./src --ext .ts",
|
|
11
|
-
"build": "webpack --mode=production"
|
|
11
|
+
"build": "webpack --mode=production",
|
|
12
|
+
"build:rollup": "rollup -c"
|
|
12
13
|
},
|
|
13
14
|
"dependencies": {
|
|
14
15
|
"@amedia/browserid": "0.1.6",
|
|
15
16
|
"@amedia/frontend-rpc": "0.0.12",
|
|
16
17
|
"dexie": "3.0.3",
|
|
17
18
|
"regenerator-runtime": "0.13.7",
|
|
19
|
+
"rollup-plugin-uglify": "^6.0.4",
|
|
18
20
|
"runtypes": "6.2.1"
|
|
19
21
|
},
|
|
20
22
|
"devDependencies": {
|
|
@@ -23,6 +25,10 @@
|
|
|
23
25
|
"@babel/core": "7.14.0",
|
|
24
26
|
"@babel/preset-env": "7.14.1",
|
|
25
27
|
"@babel/preset-typescript": "7.13.0",
|
|
28
|
+
"@eik/cli": "1.8.30",
|
|
29
|
+
"@rollup/plugin-commonjs": "19.0.0",
|
|
30
|
+
"@rollup/plugin-node-resolve": "13.0.0",
|
|
31
|
+
"@rollup/plugin-typescript": "8.2.1",
|
|
26
32
|
"@types/convict": "6.0.1",
|
|
27
33
|
"@types/dexie": "1.3.1",
|
|
28
34
|
"@types/jest": "^26.0.23",
|
|
@@ -38,9 +44,13 @@
|
|
|
38
44
|
"mockdate": "3.0.5",
|
|
39
45
|
"npm-run-all": "4.1.5",
|
|
40
46
|
"prettier": "2.2.1",
|
|
47
|
+
"rollup": "2.51.0",
|
|
48
|
+
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
49
|
+
"rollup-plugin-typescript2": "0.30.0",
|
|
41
50
|
"source-map-loader": "2.0.1",
|
|
42
51
|
"ts-jest": "^26.5.5",
|
|
43
52
|
"ts-loader": "9.1.0",
|
|
53
|
+
"tslib": "2.2.0",
|
|
44
54
|
"typescript": "4.2.4",
|
|
45
55
|
"webpack": "5.36.2",
|
|
46
56
|
"webpack-cli": "4.6.0"
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
import typescript from 'rollup-plugin-typescript2';
|
|
4
|
+
import { uglify } from 'rollup-plugin-uglify';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
input: 'src/index.ts',
|
|
8
|
+
output: {
|
|
9
|
+
file: 'esm/user.js',
|
|
10
|
+
format: 'esm',
|
|
11
|
+
sourcemap: true,
|
|
12
|
+
},
|
|
13
|
+
plugins: [resolve(), commonjs(), typescript(), uglify()],
|
|
14
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as url from '../utils/url';
|
|
6
|
+
import * as location from '../utils/location';
|
|
7
|
+
import { login } from './authentication';
|
|
8
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
restoreAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('authentication', () => {
|
|
15
|
+
describe('goToLoginPage', () => {
|
|
16
|
+
it('should resolve dev env', () => {
|
|
17
|
+
spyOn(url, 'getCurrentEnvironment').and.returnValue('localhost');
|
|
18
|
+
spyOn(location, 'navigateTo');
|
|
19
|
+
login('current');
|
|
20
|
+
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
21
|
+
'https://www.aid.no.localhost.api.no/aid/logg_inn/?requestedUrl=current'
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
it('should resolve snap5 env', () => {
|
|
25
|
+
spyOn(url, 'getCurrentEnvironment').and.returnValue('snap5');
|
|
26
|
+
spyOn(location, 'navigateTo');
|
|
27
|
+
login('current');
|
|
28
|
+
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
29
|
+
'https://www-snap5.aid.no/aid/logg_inn/?requestedUrl=current'
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
it('should resolve prod env', () => {
|
|
33
|
+
spyOn(url, 'getCurrentEnvironment').and.returnValue('');
|
|
34
|
+
spyOn(location, 'navigateTo');
|
|
35
|
+
login('current');
|
|
36
|
+
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
37
|
+
'https://www.aid.no/aid/logg_inn/?requestedUrl=current'
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -3,25 +3,24 @@ import cache from '../cache';
|
|
|
3
3
|
import { getRPCClient } from '../lib/rpc';
|
|
4
4
|
import { removeCookie } from '../lib/cookie';
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
getAidDomain,
|
|
7
7
|
getCurrentPublicationDomain,
|
|
8
|
+
getCurrentUrl,
|
|
8
9
|
} from '../utils/url';
|
|
9
10
|
import log from '../utils/log';
|
|
10
11
|
import { UserAttributes } from '../types';
|
|
11
12
|
import store from '../store';
|
|
13
|
+
import { navigateTo } from '../utils/location';
|
|
12
14
|
|
|
13
15
|
export const login = (requestedUrl: URL | string) => {
|
|
14
|
-
const
|
|
15
|
-
const url = new URL(
|
|
16
|
-
`https://aid.no${environment && `.${environment}.api.no`}/aid/logg_inn/`
|
|
17
|
-
);
|
|
16
|
+
const url = new URL(`https://${getAidDomain()}/aid/logg_inn/`);
|
|
18
17
|
url.searchParams.set(
|
|
19
18
|
'requestedUrl',
|
|
20
19
|
requestedUrl instanceof URL
|
|
21
20
|
? requestedUrl.href
|
|
22
|
-
: requestedUrl ||
|
|
21
|
+
: requestedUrl || getCurrentUrl()
|
|
23
22
|
);
|
|
24
|
-
|
|
23
|
+
navigateTo(url.href);
|
|
25
24
|
};
|
|
26
25
|
|
|
27
26
|
function clearCookies() {
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
getNonUserAccess,
|
|
4
4
|
getVstoken,
|
|
5
5
|
} from '../clients/jupiter';
|
|
6
|
-
import { getCurrentPublicationDomain } from '../utils/url';
|
|
6
|
+
import { getCurrentPublicationDomain, getCurrentUrl } from '../utils/url';
|
|
7
7
|
import { UserAttributes } from '../types';
|
|
8
8
|
import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
9
9
|
import { logout } from './authentication';
|
|
@@ -34,7 +34,9 @@ export const hasAccess = (
|
|
|
34
34
|
currentAccessFeatures.includes(requestedAccessFeature)
|
|
35
35
|
).length > 0;
|
|
36
36
|
|
|
37
|
-
export const
|
|
37
|
+
export const setUserAccessCookie = async (
|
|
38
|
+
publicationDomain
|
|
39
|
+
): Promise<boolean> => {
|
|
38
40
|
const vstoken = await getVstoken(publicationDomain);
|
|
39
41
|
if (vstoken.status === 'success') {
|
|
40
42
|
/**
|
|
@@ -45,6 +47,12 @@ export const setAccessCookies = async (publicationDomain): Promise<boolean> => {
|
|
|
45
47
|
setCookie('VSTOKEN', vstoken.cookie, 365, '/');
|
|
46
48
|
return true;
|
|
47
49
|
}
|
|
50
|
+
return false;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const setNonUserAccessCookie = async (
|
|
54
|
+
publicationDomain
|
|
55
|
+
): Promise<boolean> => {
|
|
48
56
|
const vstoken2 = await getNonUserAccess(publicationDomain);
|
|
49
57
|
if (vstoken2.status === 'success') {
|
|
50
58
|
setCookie('VSTOKEN2', vstoken2.vstoken_cookie, 1, '/');
|
|
@@ -58,14 +66,19 @@ export const hasRecentlyTriedToUnlockPaywall = (): boolean => {
|
|
|
58
66
|
if (!cookie) {
|
|
59
67
|
return false;
|
|
60
68
|
}
|
|
69
|
+
const currentUrl = getCurrentUrl();
|
|
61
70
|
if (Array.isArray(cookie)) {
|
|
62
|
-
return cookie.filter((value) => value ===
|
|
71
|
+
return cookie.filter((value) => value === currentUrl).length > 0;
|
|
63
72
|
}
|
|
64
|
-
return cookie ===
|
|
73
|
+
return cookie === currentUrl;
|
|
65
74
|
};
|
|
66
75
|
|
|
76
|
+
export const setAccessCookies = async (publicationDomain): Promise<boolean> =>
|
|
77
|
+
setUserAccessCookie(publicationDomain) &&
|
|
78
|
+
setNonUserAccessCookie(publicationDomain);
|
|
79
|
+
|
|
67
80
|
export const setHasTriedToUnlockPaywall = (): void => {
|
|
68
|
-
const url = new URL(
|
|
81
|
+
const url = new URL(getCurrentUrl());
|
|
69
82
|
url.searchParams.set('access', 'granted');
|
|
70
83
|
|
|
71
84
|
const expires = new Date();
|
package/src/actions/storage.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getNamespaces } from '../clients/europa';
|
|
1
|
+
import { getNamespaces, updateNamespace } from '../clients/europa';
|
|
2
2
|
import store from '../store';
|
|
3
3
|
|
|
4
4
|
export const requestStorage = (namespaces: string[]) => {
|
|
@@ -6,3 +6,8 @@ export const requestStorage = (namespaces: string[]) => {
|
|
|
6
6
|
store.set('storage', storage);
|
|
7
7
|
});
|
|
8
8
|
};
|
|
9
|
+
|
|
10
|
+
export const updateStorage = (storage: { [namespace: string]: object }) =>
|
|
11
|
+
Object.keys(storage).forEach((namespace) =>
|
|
12
|
+
updateNamespace(namespace, storage[namespace])
|
|
13
|
+
);
|
package/src/actions/userData.ts
CHANGED
|
@@ -8,7 +8,7 @@ import store from '../store';
|
|
|
8
8
|
import { getInfo } from '../clients/nebula';
|
|
9
9
|
|
|
10
10
|
const getExtraData = () =>
|
|
11
|
-
getBrowserId().then((browserId) =>
|
|
11
|
+
getBrowserId().then((browserId: string) =>
|
|
12
12
|
getInfo(
|
|
13
13
|
window.location.hostname,
|
|
14
14
|
browserId,
|
|
@@ -19,6 +19,7 @@ const getExtraData = () =>
|
|
|
19
19
|
const dataServiceMap: {
|
|
20
20
|
[key in keyof UserAttributes]?: () => Promise<Partial<UserAttributes>>;
|
|
21
21
|
} = {
|
|
22
|
+
uuid: getSession,
|
|
22
23
|
name: getSession,
|
|
23
24
|
trackingKey: getSession,
|
|
24
25
|
access: getAccess,
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import fetch from 'jest-fetch-mock';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import thirdPartyCookie, { CACHE_KEY, CACHE_TTL } from './thirdPartyCookie';
|
|
4
|
+
import * as location from '../utils/location';
|
|
5
|
+
import * as url from '../utils/url';
|
|
6
|
+
import * as pluto from '../clients/pluto';
|
|
7
|
+
import store from '../store';
|
|
8
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
restoreAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
function itShouldSetAlreadyTriedCache() {
|
|
15
|
+
it('should set already tried cache', () => {
|
|
16
|
+
expect(cache.set).toHaveBeenCalledWith(CACHE_KEY, true, CACHE_TTL);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function itShouldNotNavigateAway() {
|
|
21
|
+
it('should not navigate away', () => {
|
|
22
|
+
expect(location.navigateTo).not.toHaveBeenCalled();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe('Autologin: Third Party Cookie', () => {
|
|
27
|
+
let cacheGetSpy: jasmine.Spy;
|
|
28
|
+
let storeGetSpy: jasmine.Spy;
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
cacheGetSpy = spyOn(cache, 'get');
|
|
31
|
+
spyOn(cache, 'set');
|
|
32
|
+
spyOn(location, 'navigateTo');
|
|
33
|
+
storeGetSpy = spyOn(store, 'get');
|
|
34
|
+
spyOn(url, 'getAidDomain').and.returnValue('www.aid.no');
|
|
35
|
+
spyOn(url, 'getCurrentUrl').and.returnValue(
|
|
36
|
+
'https://avisnavn.no/abo/?query=param'
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('when already logged in', () => {
|
|
41
|
+
beforeEach(async () => {
|
|
42
|
+
spyOn(pluto, 'getCurrentSession').and.returnValue(Promise.resolve());
|
|
43
|
+
storeGetSpy.and.returnValue({ isLoggedIn: true });
|
|
44
|
+
await thirdPartyCookie();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should not try to get already tried cache', () => {
|
|
48
|
+
expect(cacheGetSpy).not.toHaveBeenCalledWith(CACHE_KEY);
|
|
49
|
+
});
|
|
50
|
+
itShouldNotNavigateAway();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('when not logged in first party', () => {
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
spyOn(pluto, 'getCurrentSession').and.returnValue(Promise.resolve());
|
|
56
|
+
storeGetSpy.and.returnValue({ isLoggedIn: false });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('when already checked third party option', () => {
|
|
60
|
+
beforeEach(async () => {
|
|
61
|
+
cacheGetSpy.and.returnValue(true);
|
|
62
|
+
await thirdPartyCookie();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should not set already tried cache', () => {
|
|
66
|
+
expect(cache.set).not.toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
itShouldNotNavigateAway();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('when not logged in via third party', () => {
|
|
73
|
+
beforeEach(async () => {
|
|
74
|
+
fetch.doMockIf(
|
|
75
|
+
`https://www.aid.no/api/pluto/v1/sessions/current`,
|
|
76
|
+
JSON.stringify({}),
|
|
77
|
+
{
|
|
78
|
+
status: 403,
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
await thirdPartyCookie();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
itShouldSetAlreadyTriedCache();
|
|
85
|
+
itShouldNotNavigateAway();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('when logged in via third party', () => {
|
|
89
|
+
beforeEach(async () => {
|
|
90
|
+
fetch.doMockIf(
|
|
91
|
+
`https://www.aid.no/api/pluto/v1/sessions/current`,
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
user_name: 'Donald Duck',
|
|
94
|
+
tracking_key: 'key',
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
await thirdPartyCookie();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
itShouldSetAlreadyTriedCache();
|
|
101
|
+
it('should navigate to auto', async () => {
|
|
102
|
+
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
103
|
+
'https://www.aid.no/logg_inn/auto/?requestedUrl=https%3A%2F%2Favisnavn.no%2Fabo%2F%3Fquery%3Dparam'
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Boolean, Record, Static, String } from 'runtypes';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import log from '../utils/log';
|
|
4
|
+
import { getAidDomain, getCurrentUrl } from '../utils/url';
|
|
5
|
+
import { navigateTo } from '../utils/location';
|
|
6
|
+
import { getCurrentSession } from '../clients/pluto';
|
|
7
|
+
import store from '../store';
|
|
8
|
+
|
|
9
|
+
export const CACHE_KEY = 'haveTriedThirdPartyLoginCheck';
|
|
10
|
+
export const CACHE_TTL = 60 * 60 * 1000; // One hour
|
|
11
|
+
|
|
12
|
+
const ServiceSessionSchema = Record({
|
|
13
|
+
user_name: String,
|
|
14
|
+
tracking_key: String,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const getAidSession = () =>
|
|
18
|
+
<Promise<Static<typeof ServiceSessionSchema> | null>>fetch(
|
|
19
|
+
`https://${getAidDomain()}/api/pluto/v1/sessions/current`,
|
|
20
|
+
{
|
|
21
|
+
mode: 'cors',
|
|
22
|
+
credentials: 'include',
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
.then((response) => {
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
if (response.status !== 403) {
|
|
28
|
+
log.error(
|
|
29
|
+
`Unknown error (${response.status}: ${response.statusText}) when fetching current session`,
|
|
30
|
+
{ response }
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return response.json();
|
|
36
|
+
})
|
|
37
|
+
.then((data) => {
|
|
38
|
+
if (!data) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
return ServiceSessionSchema.check(data);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
log.error('Invalid schema', error);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export default async function thirdPartyCookie() {
|
|
50
|
+
await getCurrentSession();
|
|
51
|
+
if (store.get('state')?.isLoggedIn) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// TODO: Should perhaps use cookie for this? Cache as it stands now only survives on the current page until vi replace it with something more persistent
|
|
56
|
+
if (await cache.get(CACHE_KEY, Boolean)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
await cache.set(CACHE_KEY, true, CACHE_TTL);
|
|
60
|
+
|
|
61
|
+
const aidSession = await getAidSession();
|
|
62
|
+
if (aidSession) {
|
|
63
|
+
// Try to get newspaper cookie
|
|
64
|
+
const url = new URL(`https://${getAidDomain()}/logg_inn/auto/`);
|
|
65
|
+
url.searchParams.set('requestedUrl', getCurrentUrl());
|
|
66
|
+
navigateTo(url.href);
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/clients/mercury.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getCurrentHostname,
|
|
3
|
+
getCurrentPublicationDomain,
|
|
4
|
+
getCurrentUrl,
|
|
5
|
+
} from '../utils/url';
|
|
2
6
|
|
|
3
7
|
function postLog(url: string, query: { [index: string]: any }) {
|
|
4
8
|
const body = new FormData();
|
|
@@ -26,8 +30,8 @@ const backendLog = (
|
|
|
26
30
|
const extendedContext = {
|
|
27
31
|
...context,
|
|
28
32
|
message,
|
|
29
|
-
url:
|
|
30
|
-
domain:
|
|
33
|
+
url: getCurrentUrl(),
|
|
34
|
+
domain: getCurrentHostname(),
|
|
31
35
|
};
|
|
32
36
|
postLog(`//www.aid.no/api/mercury/v1/log/${level}`, extendedContext);
|
|
33
37
|
};
|
package/src/clients/pluto.ts
CHANGED
|
@@ -5,6 +5,7 @@ import store from '../store';
|
|
|
5
5
|
import { UserAttributes } from '../types';
|
|
6
6
|
|
|
7
7
|
const ServiceSessionSchema = Record({
|
|
8
|
+
user_uuid: String,
|
|
8
9
|
user_name: String,
|
|
9
10
|
tracking_key: String,
|
|
10
11
|
});
|
|
@@ -12,6 +13,7 @@ const ServiceSessionSchema = Record({
|
|
|
12
13
|
const mapSessionToUserAttributes = (
|
|
13
14
|
session: Static<typeof ServiceSessionSchema>
|
|
14
15
|
): UserAttributes => ({
|
|
16
|
+
uuid: session.user_uuid,
|
|
15
17
|
name: session.user_name,
|
|
16
18
|
trackingKey: session.tracking_key,
|
|
17
19
|
});
|
|
@@ -2,11 +2,12 @@ import { requestAttributes } from '../actions/userData';
|
|
|
2
2
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
3
3
|
import { State, UserAttributes } from '../types';
|
|
4
4
|
import store from '../store';
|
|
5
|
-
import { requestStorage } from '../actions/storage';
|
|
5
|
+
import { requestStorage, updateStorage } from '../actions/storage';
|
|
6
6
|
|
|
7
7
|
export enum AmediaUserEvents {
|
|
8
8
|
REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
|
|
9
9
|
REQUEST_STORAGE_NAMESPACES = 'amedia-user.request-storage-namespaces',
|
|
10
|
+
UPDATE_STORAGE = 'amedua-user.update-storage-namespaces',
|
|
10
11
|
ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
|
|
11
12
|
ON_STATE_UPDATED = 'amedia-user.on-state-updated',
|
|
12
13
|
ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
|
|
@@ -18,6 +19,9 @@ declare global {
|
|
|
18
19
|
Array<keyof UserAttributes>
|
|
19
20
|
>;
|
|
20
21
|
[AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
|
|
22
|
+
[AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
|
|
23
|
+
[namespace: string]: object;
|
|
24
|
+
}>;
|
|
21
25
|
[AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
|
|
22
26
|
[AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
|
|
23
27
|
[AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
|
|
@@ -50,6 +54,9 @@ export const listenForUserAttributeRequests = () => {
|
|
|
50
54
|
listenWithExclusivity(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, (evt) => {
|
|
51
55
|
requestStorage(evt.detail);
|
|
52
56
|
});
|
|
57
|
+
listenWithExclusivity(AmediaUserEvents.UPDATE_STORAGE, (evt) => {
|
|
58
|
+
updateStorage(evt.detail);
|
|
59
|
+
});
|
|
53
60
|
};
|
|
54
61
|
|
|
55
62
|
listenForUserAttributeRequests();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as europa from './clients/europa';
|
|
6
|
+
import { updateStorage } from './index';
|
|
7
|
+
import * as storage from './actions/storage';
|
|
8
|
+
import store from './store';
|
|
9
|
+
import User from './lib/User';
|
|
10
|
+
import { setRootElement } from './utils/customEventWrapper';
|
|
11
|
+
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
12
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
restoreAllMocks();
|
|
16
|
+
|
|
17
|
+
// We cannot use window as our event root during testing
|
|
18
|
+
// since tests are executed in parallel.
|
|
19
|
+
setRootElement(new Document());
|
|
20
|
+
|
|
21
|
+
// Store must be cleared before we re-listen for user attribute requests
|
|
22
|
+
store.clear();
|
|
23
|
+
|
|
24
|
+
// Also, since we keep creating new root elements, we also
|
|
25
|
+
// need to attach to these new roots.
|
|
26
|
+
listenForUserAttributeRequests();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('Storage', () => {
|
|
30
|
+
it('should update storage', () => {
|
|
31
|
+
spyOn(europa, 'updateNamespace');
|
|
32
|
+
|
|
33
|
+
updateStorage({
|
|
34
|
+
user_info: {
|
|
35
|
+
some: 'data',
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(europa.updateNamespace).toHaveBeenCalledWith('user_info', {
|
|
40
|
+
some: 'data',
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return storage', () => {
|
|
45
|
+
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
46
|
+
store.set('storage', {
|
|
47
|
+
namespace: {
|
|
48
|
+
key: 'value',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const onUpdate = jest.fn();
|
|
54
|
+
new User().withStorage('namespace').subscribe(onUpdate);
|
|
55
|
+
|
|
56
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
57
|
+
storage: {
|
|
58
|
+
namespace: {
|
|
59
|
+
key: 'value',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
state: {},
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should return multiple namespaces if requested (but only requested)', () => {
|
|
67
|
+
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
68
|
+
store.set('storage', {
|
|
69
|
+
one: {
|
|
70
|
+
key1: 'value1',
|
|
71
|
+
},
|
|
72
|
+
two: {
|
|
73
|
+
key2: 'value2',
|
|
74
|
+
},
|
|
75
|
+
three: {
|
|
76
|
+
key3: 'value3',
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const onUpdate = jest.fn();
|
|
82
|
+
new User().withStorage('one', 'three').subscribe(onUpdate);
|
|
83
|
+
|
|
84
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
85
|
+
storage: {
|
|
86
|
+
one: {
|
|
87
|
+
key1: 'value1',
|
|
88
|
+
},
|
|
89
|
+
three: {
|
|
90
|
+
key3: 'value3',
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
state: {},
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
import User from './lib/User';
|
|
6
6
|
import * as userData from './actions/userData';
|
|
7
|
-
import * as storage from './actions/storage';
|
|
8
7
|
import { setRootElement } from './utils/customEventWrapper';
|
|
9
8
|
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
10
9
|
import store from './store';
|
|
@@ -192,57 +191,4 @@ describe('Public endpoint - userdata', () => {
|
|
|
192
191
|
state: { isLoggedIn: true },
|
|
193
192
|
});
|
|
194
193
|
});
|
|
195
|
-
|
|
196
|
-
it('should return storage', () => {
|
|
197
|
-
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
198
|
-
store.set('storage', {
|
|
199
|
-
namespace: {
|
|
200
|
-
key: 'value',
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const onUpdate = jest.fn();
|
|
206
|
-
new User().withStorage('namespace').subscribe(onUpdate);
|
|
207
|
-
|
|
208
|
-
expect(onUpdate).toHaveBeenCalledWith({
|
|
209
|
-
storage: {
|
|
210
|
-
namespace: {
|
|
211
|
-
key: 'value',
|
|
212
|
-
},
|
|
213
|
-
},
|
|
214
|
-
state: {},
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('should return multiple namespaces if requested (but only requested)', () => {
|
|
219
|
-
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
220
|
-
store.set('storage', {
|
|
221
|
-
one: {
|
|
222
|
-
key1: 'value1',
|
|
223
|
-
},
|
|
224
|
-
two: {
|
|
225
|
-
key2: 'value2',
|
|
226
|
-
},
|
|
227
|
-
three: {
|
|
228
|
-
key3: 'value3',
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
const onUpdate = jest.fn();
|
|
234
|
-
new User().withStorage('one', 'three').subscribe(onUpdate);
|
|
235
|
-
|
|
236
|
-
expect(onUpdate).toHaveBeenCalledWith({
|
|
237
|
-
storage: {
|
|
238
|
-
one: {
|
|
239
|
-
key1: 'value1',
|
|
240
|
-
},
|
|
241
|
-
three: {
|
|
242
|
-
key3: 'value3',
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
state: {},
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
194
|
});
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
|
4
4
|
import * as authentication from './actions/authentication';
|
|
5
5
|
import User from './lib/User';
|
|
6
6
|
import PaywallUnlocker from './lib/PaywallUnlocker';
|
|
7
|
+
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
8
|
+
import { getCurrentUrl } from './utils/url';
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Enables you to subscribe to the user attributes you care about and
|
|
@@ -17,7 +19,7 @@ export const user = () => new User();
|
|
|
17
19
|
* @param requestedUrl
|
|
18
20
|
*/
|
|
19
21
|
export const goToLoginPage = (requestedUrl?: string) =>
|
|
20
|
-
authentication.login(requestedUrl ||
|
|
22
|
+
authentication.login(requestedUrl || getCurrentUrl());
|
|
21
23
|
|
|
22
24
|
export const logout = () => authentication.logout();
|
|
23
25
|
|
|
@@ -36,3 +38,8 @@ export const tryActivateSubscriptionAccess = (
|
|
|
36
38
|
});
|
|
37
39
|
|
|
38
40
|
export const paywall = () => new PaywallUnlocker();
|
|
41
|
+
|
|
42
|
+
export const updateStorage = (storage: { [index: string]: Object }): void =>
|
|
43
|
+
dispatch(
|
|
44
|
+
new CustomEvent(AmediaUserEvents.UPDATE_STORAGE, { detail: storage })
|
|
45
|
+
);
|