@amedia/user 0.1.2 → 0.2.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/dist/actions/authentication.d.ts +4 -0
- package/dist/actions/authorization.d.ts +3 -0
- package/dist/cache/engines/global.d.ts +6 -0
- package/dist/cache/engines/indexeddb.d.ts +6 -0
- package/dist/cache/index.d.ts +9 -0
- package/dist/cache/types.d.ts +10 -0
- package/dist/eventHandlers/activation.d.ts +13 -0
- package/dist/eventHandlers/authentication.d.ts +15 -0
- package/dist/eventHandlers/userAttributes.d.ts +12 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/lib/dataOrganizer.d.ts +2 -9
- package/dist/services/pluto.d.ts +5 -10
- package/dist/types.d.ts +12 -0
- package/dist/utils/customEventWrapper.d.ts +17 -0
- package/dist/utils/log.d.ts +19 -0
- package/dist/utils/queue.d.ts +5 -0
- package/package.json +4 -3
- package/src/actions/authentication.ts +37 -0
- package/src/actions/authorization.ts +51 -0
- package/src/{lib/cache → cache/engines}/global.ts +5 -3
- package/src/{lib/cache/db.ts → cache/engines/indexeddb.ts} +5 -5
- package/src/cache/index.spec.ts +82 -0
- package/src/{services/cache.ts → cache/index.ts} +25 -13
- package/src/{lib/cache → cache}/types.ts +7 -1
- package/src/eventHandlers/activation.ts +29 -0
- package/src/eventHandlers/authentication.ts +29 -0
- package/src/{lib → eventHandlers}/userAttributes.ts +11 -12
- package/src/index.spec.ts +49 -30
- package/src/index.ts +14 -5
- package/src/lib/dataOrganizer.ts +33 -14
- package/src/services/pluto.spec.ts +3 -2
- package/src/services/pluto.ts +14 -10
- package/src/types.ts +13 -0
- package/src/utils/customEventWrapper.ts +42 -0
- package/src/utils/log.ts +7 -0
- package/webpack.config.js +1 -0
- package/src/lib/authentication.ts +0 -94
- package/src/lib/authorization.ts +0 -86
- package/src/lib/eventHandler.ts +0 -16
- package/src/lib/types.ts +0 -7
- /package/src/{lib → utils}/queue.ts +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
|
+
import { activate } from '../actions/authorization';
|
|
3
|
+
|
|
4
|
+
export enum ActivationEvents {
|
|
5
|
+
REQUEST_ACTIVATION = 'amedia-user.activation.request-activation',
|
|
6
|
+
ON_ACTIVATION_COMPLETE = 'amedia-user.activation.on-activation-complete',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
interface WindowEventMap {
|
|
11
|
+
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent;
|
|
12
|
+
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
13
|
+
status: 'success' | 'error';
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const listenForActivationRequests = () => {
|
|
19
|
+
listenWithExclusivity(ActivationEvents.REQUEST_ACTIVATION, () => {
|
|
20
|
+
activate().then((result) => {
|
|
21
|
+
dispatch(
|
|
22
|
+
new CustomEvent(ActivationEvents.ON_ACTIVATION_COMPLETE, {
|
|
23
|
+
detail: result,
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
listenForActivationRequests();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
|
+
import { login, logout } from '../actions/authentication';
|
|
3
|
+
|
|
4
|
+
export enum AuthenticationEvents {
|
|
5
|
+
REQUEST_LOGIN = 'amedia-user.authentication.request-login',
|
|
6
|
+
REQUEST_LOGOUT = 'amedia-user.authentication.request-logout',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
interface WindowEventMap {
|
|
11
|
+
[AuthenticationEvents.REQUEST_LOGIN]: CustomEvent<{
|
|
12
|
+
requestedUrl?: string;
|
|
13
|
+
}>;
|
|
14
|
+
[AuthenticationEvents.REQUEST_LOGOUT]: CustomEvent<{
|
|
15
|
+
requestedUrl?: string;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const listenForAuthenticationRequests = () => {
|
|
21
|
+
listenWithExclusivity(AuthenticationEvents.REQUEST_LOGIN, (evt) => {
|
|
22
|
+
login(evt.detail.requestedUrl || window.location.href);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
listenWithExclusivity(AuthenticationEvents.REQUEST_LOGOUT, () => {
|
|
26
|
+
logout();
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
listenForAuthenticationRequests();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { fetchDataBasedOnAttributes } from '
|
|
2
|
-
import { dispatch,
|
|
3
|
-
import { UserAttributes } from '
|
|
1
|
+
import { fetchDataBasedOnAttributes } from '../lib/dataOrganizer';
|
|
2
|
+
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
3
|
+
import { UserAttributes } from '../types';
|
|
4
4
|
|
|
5
5
|
export enum AmediaUserEvents {
|
|
6
6
|
REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
|
|
@@ -17,15 +17,14 @@ declare global {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export const listenForUserAttributeRequests = () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
);
|
|
20
|
+
listenWithExclusivity(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, (evt) => {
|
|
21
|
+
fetchDataBasedOnAttributes(evt.detail, (attributes) => {
|
|
22
|
+
dispatch(
|
|
23
|
+
new CustomEvent(AmediaUserEvents.ON_USER_ATTRIBUTES_UPDATED, {
|
|
24
|
+
detail: attributes,
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
});
|
|
29
28
|
});
|
|
30
29
|
};
|
|
31
30
|
|
package/src/index.spec.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import { subscribeToUserUpdates } from './index';
|
|
6
6
|
import * as dataOrganizer from './lib/dataOrganizer';
|
|
7
|
-
import { setRootElement } from './
|
|
8
|
-
import { listenForUserAttributeRequests } from './
|
|
7
|
+
import { setRootElement } from './utils/customEventWrapper';
|
|
8
|
+
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
9
9
|
|
|
10
10
|
beforeEach(() => {
|
|
11
11
|
jest.resetAllMocks();
|
|
@@ -21,10 +21,14 @@ beforeEach(() => {
|
|
|
21
21
|
|
|
22
22
|
describe('Public endpoint', () => {
|
|
23
23
|
it('should alert about user data', () => {
|
|
24
|
-
jest
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
jest
|
|
25
|
+
.spyOn(dataOrganizer, 'fetchDataBasedOnAttributes')
|
|
26
|
+
.mockImplementation((_, callback: Function) => {
|
|
27
|
+
callback({
|
|
28
|
+
name: 'Donald Duck',
|
|
29
|
+
isLoggedIn: true,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
28
32
|
|
|
29
33
|
const onUserUpdate = jest.fn();
|
|
30
34
|
subscribeToUserUpdates(['name'], onUserUpdate);
|
|
@@ -38,13 +42,17 @@ describe('Public endpoint', () => {
|
|
|
38
42
|
it('should alert when others also ask about data', () => {
|
|
39
43
|
jest
|
|
40
44
|
.spyOn(dataOrganizer, 'fetchDataBasedOnAttributes')
|
|
41
|
-
.
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
.mockImplementationOnce((_, callback: Function) => {
|
|
46
|
+
callback({
|
|
47
|
+
name: undefined,
|
|
48
|
+
isLoggedIn: false,
|
|
49
|
+
});
|
|
44
50
|
})
|
|
45
|
-
.
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
.mockImplementationOnce((_, callback: Function) => {
|
|
52
|
+
callback({
|
|
53
|
+
name: 'Donald Duck',
|
|
54
|
+
isLoggedIn: true,
|
|
55
|
+
});
|
|
48
56
|
});
|
|
49
57
|
|
|
50
58
|
const firstOnUserUpdate = jest.fn();
|
|
@@ -73,10 +81,12 @@ describe('Public endpoint', () => {
|
|
|
73
81
|
it('should not return more data that requested (except for login status)', () => {
|
|
74
82
|
jest
|
|
75
83
|
.spyOn(dataOrganizer, 'fetchDataBasedOnAttributes')
|
|
76
|
-
.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
84
|
+
.mockImplementationOnce((_, callback: Function) => {
|
|
85
|
+
callback({
|
|
86
|
+
name: 'Donald Duck',
|
|
87
|
+
email: 'donald.duck@andeby.no',
|
|
88
|
+
isLoggedIn: true,
|
|
89
|
+
});
|
|
80
90
|
});
|
|
81
91
|
|
|
82
92
|
const onUserUpdate = jest.fn();
|
|
@@ -91,10 +101,13 @@ describe('Public endpoint', () => {
|
|
|
91
101
|
it('should support returning multiple requested attributes', () => {
|
|
92
102
|
jest
|
|
93
103
|
.spyOn(dataOrganizer, 'fetchDataBasedOnAttributes')
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
|
|
105
|
+
.mockImplementationOnce((_, callback: Function) => {
|
|
106
|
+
callback({
|
|
107
|
+
name: 'Donald Duck',
|
|
108
|
+
email: 'donald.duck@andeby.no',
|
|
109
|
+
isLoggedIn: true,
|
|
110
|
+
});
|
|
98
111
|
});
|
|
99
112
|
|
|
100
113
|
const onUserUpdate = jest.fn();
|
|
@@ -110,10 +123,12 @@ describe('Public endpoint', () => {
|
|
|
110
123
|
it('should support no attributes, returning only logged in state', () => {
|
|
111
124
|
jest
|
|
112
125
|
.spyOn(dataOrganizer, 'fetchDataBasedOnAttributes')
|
|
113
|
-
.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
126
|
+
.mockImplementationOnce((_, callback: Function) => {
|
|
127
|
+
callback({
|
|
128
|
+
name: 'Donald Duck',
|
|
129
|
+
email: 'donald.duck@andeby.no',
|
|
130
|
+
isLoggedIn: true,
|
|
131
|
+
});
|
|
117
132
|
});
|
|
118
133
|
|
|
119
134
|
const onUserUpdate = jest.fn();
|
|
@@ -124,12 +139,16 @@ describe('Public endpoint', () => {
|
|
|
124
139
|
});
|
|
125
140
|
});
|
|
126
141
|
|
|
127
|
-
it('should support requesting
|
|
128
|
-
jest
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
142
|
+
it('should support requesting different attributes', () => {
|
|
143
|
+
jest
|
|
144
|
+
.spyOn(dataOrganizer, 'fetchDataBasedOnAttributes')
|
|
145
|
+
.mockImplementation((_, callback: Function) => {
|
|
146
|
+
callback({
|
|
147
|
+
name: 'Donald Duck',
|
|
148
|
+
email: 'donald.duck@andeby.no',
|
|
149
|
+
isLoggedIn: true,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
133
152
|
|
|
134
153
|
const firstOnUserUpdate = jest.fn();
|
|
135
154
|
subscribeToUserUpdates([], firstOnUserUpdate);
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { AuthenticationEvents } from './
|
|
2
|
-
import { AmediaUserEvents } from './
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { AuthenticationEvents } from './eventHandlers/authentication';
|
|
2
|
+
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
3
|
+
import { ActivationEvents } from './eventHandlers/activation';
|
|
4
|
+
import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
5
|
+
import { UserAttributes } from './types';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Enables you to subscribe to the user attributes you care about and
|
|
@@ -22,7 +23,7 @@ export const subscribeToUserUpdates = <T extends Array<keyof UserAttributes>>(
|
|
|
22
23
|
attributes: Pick<UserAttributes, T[number] | 'isLoggedIn'>
|
|
23
24
|
) => void
|
|
24
25
|
) => {
|
|
25
|
-
|
|
26
|
+
listenForAllEvents(AmediaUserEvents.ON_USER_ATTRIBUTES_UPDATED, (evt) => {
|
|
26
27
|
const userAttributes = evt.detail;
|
|
27
28
|
// @ts-ignore
|
|
28
29
|
const responseAttributes: Pick<UserAttributes, T[number] | 'isLoggedIn'> = {
|
|
@@ -56,3 +57,11 @@ export const requestLogin = (requestedUrl?: string) =>
|
|
|
56
57
|
detail: { requestedUrl },
|
|
57
58
|
})
|
|
58
59
|
);
|
|
60
|
+
|
|
61
|
+
export const activate = (): Promise<unknown> =>
|
|
62
|
+
new Promise((resolve, reject) => {
|
|
63
|
+
listenForAllEvents(ActivationEvents.ON_ACTIVATION_COMPLETE, (result) => {
|
|
64
|
+
resolve(result);
|
|
65
|
+
});
|
|
66
|
+
dispatch(new CustomEvent(ActivationEvents.REQUEST_ACTIVATION));
|
|
67
|
+
});
|
package/src/lib/dataOrganizer.ts
CHANGED
|
@@ -1,18 +1,37 @@
|
|
|
1
|
-
import { UserAttributes } from '
|
|
1
|
+
import { UserAttributes } from '../types';
|
|
2
|
+
import { getSession } from '../actions/authentication';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
// TODO We may want actions to tell us what they can deliver and not have this "organizer" (wohoo-bad-name) tell the actions..
|
|
5
|
+
const dataServiceMap: {
|
|
6
|
+
// TODO Must get proper type here. Perhaps all actions should return UserAttribute-compatible response?
|
|
7
|
+
[key in keyof UserAttributes]?: () => Promise<any>;
|
|
8
|
+
} = {
|
|
9
|
+
name: getSession,
|
|
10
|
+
trackingKey: getSession,
|
|
11
|
+
};
|
|
10
12
|
|
|
11
13
|
export const fetchDataBasedOnAttributes = (
|
|
12
|
-
attributes:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
(
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
attributes: Array<keyof UserAttributes>,
|
|
15
|
+
onDataUpdated: (attributes: UserAttributes) => void
|
|
16
|
+
) => {
|
|
17
|
+
const requiredServices: Array<() => Promise<UserAttributes>> = [];
|
|
18
|
+
attributes.forEach((attribute) => {
|
|
19
|
+
const service = dataServiceMap[attribute];
|
|
20
|
+
if (!service) {
|
|
21
|
+
console.warn('Unknown attribute was requested');
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!requiredServices.includes(service)) {
|
|
25
|
+
requiredServices.push(service);
|
|
26
|
+
}
|
|
18
27
|
});
|
|
28
|
+
|
|
29
|
+
while (requiredServices.length > 0) {
|
|
30
|
+
const service = requiredServices.pop();
|
|
31
|
+
if (service) {
|
|
32
|
+
service().then((a) => {
|
|
33
|
+
onDataUpdated(a);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -20,8 +20,9 @@ describe('Pluto service', () => {
|
|
|
20
20
|
};
|
|
21
21
|
fetch.mockResponseOnce(JSON.stringify(data));
|
|
22
22
|
|
|
23
|
-
getCurrentSession().
|
|
24
|
-
|
|
23
|
+
expect(getCurrentSession()).resolves.toEqual({
|
|
24
|
+
name: 'Earl Hickey',
|
|
25
|
+
trackingKey: 'some tracking key',
|
|
25
26
|
});
|
|
26
27
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
27
28
|
});
|
package/src/services/pluto.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Record, Static, String } from 'runtypes';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
created_at: String,
|
|
5
|
-
updated_at: String,
|
|
6
|
-
user_uuid: String,
|
|
3
|
+
const SessionSchema = Record({
|
|
7
4
|
user_name: String,
|
|
8
5
|
tracking_key: String,
|
|
9
|
-
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export const Session = Record({
|
|
9
|
+
name: String,
|
|
10
|
+
trackingKey: String,
|
|
11
|
+
});
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
13
|
+
export type Session = Static<typeof Session>;
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
const mapSession = (session: Static<typeof SessionSchema>): Session => ({
|
|
16
|
+
name: session.user_name,
|
|
17
|
+
trackingKey: session.tracking_key,
|
|
13
18
|
});
|
|
14
|
-
export type Session = Static<typeof SessionSchema>;
|
|
15
19
|
|
|
16
20
|
export const getCurrentSession = async (): Promise<Session> =>
|
|
17
21
|
fetch(`/api/pluto/v1/sessions/current`)
|
|
@@ -26,7 +30,7 @@ export const getCurrentSession = async (): Promise<Session> =>
|
|
|
26
30
|
})
|
|
27
31
|
.then((json) => {
|
|
28
32
|
try {
|
|
29
|
-
return SessionSchema.check(json);
|
|
33
|
+
return mapSession(SessionSchema.check(json));
|
|
30
34
|
} catch (error) {
|
|
31
35
|
console.error('Invalid schema', error);
|
|
32
36
|
throw error;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
let rootElement = window || global;
|
|
2
|
+
|
|
3
|
+
export const setRootElement = (element) => {
|
|
4
|
+
rootElement = element;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* This listener MUST be used by all listeners in ../eventHandlers
|
|
9
|
+
* We want to ensure that only _one_ instance of this package actually
|
|
10
|
+
* orchestrates data and performs requests against backends.
|
|
11
|
+
* @param type
|
|
12
|
+
* @param listener
|
|
13
|
+
*/
|
|
14
|
+
export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
15
|
+
type: T,
|
|
16
|
+
listener: (this: Window, ev: WindowEventMap[T]) => void
|
|
17
|
+
) => {
|
|
18
|
+
rootElement.addEventListener(type, (evt) => {
|
|
19
|
+
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
20
|
+
evt.stopImmediatePropagation();
|
|
21
|
+
listener.apply(window, [evt]);
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Use this for response listeners. (../index.ts) where we want
|
|
27
|
+
* all updates with data - no matter who sent the actual request
|
|
28
|
+
* @param type
|
|
29
|
+
* @param listener
|
|
30
|
+
*/
|
|
31
|
+
export const listenForAllEvents = <T extends keyof WindowEventMap>(
|
|
32
|
+
type: T,
|
|
33
|
+
listener: (this: Window, ev: WindowEventMap[T]) => void
|
|
34
|
+
) => {
|
|
35
|
+
rootElement.addEventListener(type, (evt) => {
|
|
36
|
+
listener.apply(window, [evt]);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const dispatch = <T extends Event>(event: T) => {
|
|
41
|
+
rootElement.dispatchEvent(event);
|
|
42
|
+
};
|
package/src/utils/log.ts
ADDED
package/webpack.config.js
CHANGED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import cache from '../services/cache';
|
|
2
|
-
import { getCurrentSession, Session, SessionSchema } from '../services/pluto';
|
|
3
|
-
import { dispatch, listen } from './eventHandler';
|
|
4
|
-
|
|
5
|
-
const EXPIRES_OFFSET = 10000; // 10 seconds
|
|
6
|
-
|
|
7
|
-
export enum AuthenticationEvents {
|
|
8
|
-
REQUEST_LOGIN = 'amedia_user.authentication.request_login',
|
|
9
|
-
REQUEST_LOGOUT = 'amedia_user.authentication.request_logout',
|
|
10
|
-
REQUEST_AUTH_STATE = 'amedia_user.authentication.request_auth_state',
|
|
11
|
-
ON_AUTH_STATE_UPDATED = 'amedia_user.authentication.on_auth_state_updated',
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
declare global {
|
|
15
|
-
interface WindowEventMap {
|
|
16
|
-
[AuthenticationEvents.REQUEST_LOGIN]: CustomEvent<{
|
|
17
|
-
requestedUrl?: string;
|
|
18
|
-
}>;
|
|
19
|
-
[AuthenticationEvents.REQUEST_LOGOUT]: CustomEvent<{
|
|
20
|
-
requestedUrl?: string;
|
|
21
|
-
}>;
|
|
22
|
-
[AuthenticationEvents.REQUEST_AUTH_STATE]: CustomEvent<Session>;
|
|
23
|
-
[AuthenticationEvents.ON_AUTH_STATE_UPDATED]: CustomEvent<Session>;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const login = (requestedUrl: string) => {
|
|
28
|
-
// TODO Need to get environment smarts from incentive embed
|
|
29
|
-
const url = new URL('https://aid.no/logg_inn/');
|
|
30
|
-
url.searchParams.set('requestedUrl', requestedUrl || window.location.href);
|
|
31
|
-
window.location.href = url.href;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const logout = () => {
|
|
35
|
-
// TODO Perform logout
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
let isFetchingSession = false;
|
|
39
|
-
const IS_LOGGED_IN_CACHE_KEY = 'is_logged_in';
|
|
40
|
-
const getSession = async (): Promise<Session | null> => {
|
|
41
|
-
if (isFetchingSession) {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
isFetchingSession = true;
|
|
45
|
-
|
|
46
|
-
// Try the cache first
|
|
47
|
-
const cacheResponse = await cache.get(IS_LOGGED_IN_CACHE_KEY, SessionSchema);
|
|
48
|
-
if (cacheResponse) {
|
|
49
|
-
isFetchingSession = false;
|
|
50
|
-
return cacheResponse;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Actually perform a request against backend for the session
|
|
54
|
-
const session = await getCurrentSession();
|
|
55
|
-
cache.set(IS_LOGGED_IN_CACHE_KEY, session, Date.now() + EXPIRES_OFFSET);
|
|
56
|
-
isFetchingSession = false;
|
|
57
|
-
return session;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const listenForAuthenticationRequests = () => {
|
|
61
|
-
listen(AuthenticationEvents.REQUEST_LOGIN, (evt) => {
|
|
62
|
-
if (!window) {
|
|
63
|
-
throw new Error('Cannot perform login in node-context yet!');
|
|
64
|
-
}
|
|
65
|
-
login(evt.detail.requestedUrl || window.location.href);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
listen(AuthenticationEvents.REQUEST_LOGOUT, () => {
|
|
69
|
-
if (!window) {
|
|
70
|
-
throw new Error('Cannot perform login in node-context yet!');
|
|
71
|
-
}
|
|
72
|
-
logout();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
listen(AuthenticationEvents.REQUEST_AUTH_STATE, (evt) => {
|
|
76
|
-
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
77
|
-
evt.stopImmediatePropagation();
|
|
78
|
-
|
|
79
|
-
getSession().then((session) => {
|
|
80
|
-
// Supports not getting a response. We're probably working on it already and will emit
|
|
81
|
-
// events on window / global when complete.
|
|
82
|
-
if (session === null) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
dispatch(
|
|
86
|
-
new CustomEvent(AuthenticationEvents.ON_AUTH_STATE_UPDATED, {
|
|
87
|
-
detail: session,
|
|
88
|
-
})
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
listenForAuthenticationRequests();
|
package/src/lib/authorization.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AccessFeatures,
|
|
3
|
-
CheckAccessForPaper,
|
|
4
|
-
CheckAccessForPaperSchema,
|
|
5
|
-
checkForAccess,
|
|
6
|
-
} from '../services/jupiter';
|
|
7
|
-
import cache from '../services/cache';
|
|
8
|
-
import { dispatch, listen } from './eventHandler';
|
|
9
|
-
|
|
10
|
-
const EXPIRES_OFFSET = 10000; // 10 seconds
|
|
11
|
-
const CHECK_FOR_ACCESS_CACHE_KEY = 'check_for_access_to_publication';
|
|
12
|
-
let isCheckingForAccess: boolean = false;
|
|
13
|
-
|
|
14
|
-
export enum AuthorizationEvents {
|
|
15
|
-
REQUEST_PUBLICATION_ACCESS_STATE_CHECK = 'amedia_user.authorization.request_publication_access_state_check',
|
|
16
|
-
ON_PUBLICATION_ACCESS_STATE_UPDATED = 'amedia_user.authorization.on_publication_access_state_updated',
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare global {
|
|
20
|
-
interface WindowEventMap {
|
|
21
|
-
[AuthorizationEvents.REQUEST_PUBLICATION_ACCESS_STATE_CHECK]: CustomEvent<{
|
|
22
|
-
publicationDomain: string;
|
|
23
|
-
accessFeatures: AccessFeatures;
|
|
24
|
-
}>;
|
|
25
|
-
[AuthorizationEvents.ON_PUBLICATION_ACCESS_STATE_UPDATED]: CustomEvent<CheckAccessForPaper>;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const checkForAccessToPublication = async (
|
|
30
|
-
publicationDomain: string,
|
|
31
|
-
accessFeatures: AccessFeatures
|
|
32
|
-
): Promise<CheckAccessForPaper | null> => {
|
|
33
|
-
if (isCheckingForAccess) {
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
isCheckingForAccess = true;
|
|
37
|
-
|
|
38
|
-
// Try the cache first
|
|
39
|
-
const cacheResponse = await cache.get(
|
|
40
|
-
CHECK_FOR_ACCESS_CACHE_KEY,
|
|
41
|
-
CheckAccessForPaperSchema
|
|
42
|
-
);
|
|
43
|
-
if (cacheResponse) {
|
|
44
|
-
isCheckingForAccess = false;
|
|
45
|
-
return cacheResponse;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Actually perform a request against backend for the accessState
|
|
49
|
-
const accessState = await checkForAccess(publicationDomain, accessFeatures);
|
|
50
|
-
cache.set(
|
|
51
|
-
CHECK_FOR_ACCESS_CACHE_KEY,
|
|
52
|
-
accessState,
|
|
53
|
-
Date.now() + EXPIRES_OFFSET
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
isCheckingForAccess = false;
|
|
57
|
-
|
|
58
|
-
return accessState;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export const listenForAuthorizationRequests = () => {
|
|
62
|
-
listen(AuthorizationEvents.REQUEST_PUBLICATION_ACCESS_STATE_CHECK, (evt) => {
|
|
63
|
-
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
64
|
-
evt.stopImmediatePropagation();
|
|
65
|
-
checkForAccessToPublication(
|
|
66
|
-
evt.detail.publicationDomain,
|
|
67
|
-
evt.detail.accessFeatures
|
|
68
|
-
).then((accessState) => {
|
|
69
|
-
// Supports not getting a response. We're probably working on it already and will emit
|
|
70
|
-
// events on window / global when complete.
|
|
71
|
-
if (!accessState) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
dispatch(
|
|
75
|
-
new CustomEvent(
|
|
76
|
-
AuthorizationEvents.ON_PUBLICATION_ACCESS_STATE_UPDATED,
|
|
77
|
-
{
|
|
78
|
-
detail: accessState,
|
|
79
|
-
}
|
|
80
|
-
)
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
listenForAuthorizationRequests();
|
package/src/lib/eventHandler.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
let rootElement = window || global;
|
|
2
|
-
|
|
3
|
-
export const setRootElement = (element) => {
|
|
4
|
-
rootElement = element;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export const listen = <T extends keyof WindowEventMap>(
|
|
8
|
-
type: T,
|
|
9
|
-
listener: (this: Window, ev: WindowEventMap[T]) => void
|
|
10
|
-
) => {
|
|
11
|
-
rootElement.addEventListener(type, listener);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const dispatch = <T extends Event>(event: T) => {
|
|
15
|
-
rootElement.dispatchEvent(event);
|
|
16
|
-
};
|
package/src/lib/types.ts
DELETED
|
File without changes
|