@amedia/user 0.13.19 → 0.13.20

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/types.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  export declare type UserAttributes = {
2
- uuid?: string;
3
- name?: string;
4
- email?: string;
5
- phone?: string;
6
- trackingKey?: string;
2
+ uuid?: string | null;
3
+ name?: string | null;
4
+ email?: string | null;
5
+ phone?: string | null;
6
+ trackingKey?: string | null;
7
7
  extraData?: {
8
8
  [index: string]: unknown;
9
9
  };
10
10
  access?: string[];
11
- privacyPreferences?: PrivacyPreferences;
11
+ privacyPreferences?: PrivacyPreferences | null;
12
12
  };
13
13
  export declare type State = {
14
14
  isLoggedIn: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amedia/user",
3
- "version": "0.13.19",
3
+ "version": "0.13.20",
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)",
@@ -14,7 +14,9 @@ const getExtraData = () =>
14
14
  getInfo(
15
15
  window.location.hostname,
16
16
  browserId,
17
- isLoggedIn ? store.get('userAttributes')?.trackingKey : undefined
17
+ isLoggedIn
18
+ ? store.get('userAttributes')?.trackingKey || undefined
19
+ : undefined
18
20
  )
19
21
  );
20
22
 
@@ -49,6 +51,12 @@ export const requestAttributes = (attributes: Array<keyof UserAttributes>) => {
49
51
  service()
50
52
  .then((data) => {
51
53
  store.merge('userAttributes', data);
54
+ if (data.uuid !== undefined) {
55
+ // We have tried to fetch uuid, so we now know if the user is logged in or not
56
+ store.merge('state', {
57
+ isLoggedIn: data.uuid !== null,
58
+ });
59
+ }
52
60
  })
53
61
  .catch((error) => {
54
62
  log.warn(`User attribute call failed (${service.toString()})`, error);
@@ -50,7 +50,7 @@ describe('Pluto service', () => {
50
50
  jest.spyOn(store, 'merge');
51
51
  });
52
52
  let currentSession;
53
- function itShouldReturnSessionDataAndSetLoggedInTrueInState() {
53
+ function itShouldReturnSessionData() {
54
54
  it('should return session data', async () => {
55
55
  expect(currentSession).toEqual({
56
56
  uuid: userUUID,
@@ -58,19 +58,13 @@ describe('Pluto service', () => {
58
58
  trackingKey: userTrackingKey,
59
59
  });
60
60
  });
61
- it('should merge isLoggedIn:true to state in store', () => {
62
- expect(store.merge).toHaveBeenCalledWith('state', {
63
- isLoggedIn: true,
64
- });
65
- });
66
61
  }
67
- function itShouldReturnNoSessionDataAndSetLoggedInFalseInState() {
62
+ function itShouldReturnNoSessionData() {
68
63
  it('should return session data', async () => {
69
- expect(currentSession).toEqual({});
70
- });
71
- it('should merge isLoggedIn:true to state in store', () => {
72
- expect(store.merge).toHaveBeenCalledWith('state', {
73
- isLoggedIn: false,
64
+ expect(currentSession).toEqual({
65
+ uuid: null,
66
+ name: null,
67
+ trackingKey: null,
74
68
  });
75
69
  });
76
70
  }
@@ -84,7 +78,7 @@ describe('Pluto service', () => {
84
78
  it('should call Pluto', () => {
85
79
  expect(fetch).toHaveBeenCalledTimes(1);
86
80
  });
87
- itShouldReturnSessionDataAndSetLoggedInTrueInState();
81
+ itShouldReturnSessionData();
88
82
  });
89
83
  describe('When cache is not empty', () => {
90
84
  beforeEach(async () => {
@@ -94,7 +88,7 @@ describe('Pluto service', () => {
94
88
  it('should not call Pluto', () => {
95
89
  expect(fetch).toHaveBeenCalledTimes(0);
96
90
  });
97
- itShouldReturnSessionDataAndSetLoggedInTrueInState();
91
+ itShouldReturnSessionData();
98
92
  });
99
93
  });
100
94
  describe('when user is not logged in', () => {
@@ -112,7 +106,7 @@ describe('Pluto service', () => {
112
106
  expect(fetch).toHaveBeenCalledTimes(1);
113
107
  });
114
108
  itShouldNotHaveLoggedAnything();
115
- itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
109
+ itShouldReturnNoSessionData();
116
110
  });
117
111
  describe('When cache is not empty', () => {
118
112
  beforeEach(async () => {
@@ -123,7 +117,7 @@ describe('Pluto service', () => {
123
117
  expect(fetch).toHaveBeenCalledTimes(0);
124
118
  });
125
119
  itShouldNotHaveLoggedAnything();
126
- itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
120
+ itShouldReturnNoSessionData();
127
121
  });
128
122
  describe('when session is invalid for domain', () => {
129
123
  beforeEach(async () => {
@@ -14,9 +14,9 @@ const ServiceSessionSchema = Partial({
14
14
  const mapSessionToUserAttributes = (
15
15
  session: Static<typeof ServiceSessionSchema>
16
16
  ): UserAttributes => ({
17
- uuid: session.user_uuid,
18
- name: session.user_name,
19
- trackingKey: session.tracking_key,
17
+ uuid: session.user_uuid || null,
18
+ name: session.user_name || null,
19
+ trackingKey: session.tracking_key || null,
20
20
  });
21
21
 
22
22
  const PlutoErrorSchema = Record({
@@ -99,12 +99,7 @@ export const getCurrentSession = async (): Promise<UserAttributes> =>
99
99
  'getCurrentSession',
100
100
  ServiceSessionSchema,
101
101
  fetchCurrentSession
102
- ).then((currentSession) => {
103
- store.merge('state', {
104
- isLoggedIn: currentSession.user_uuid !== undefined,
105
- });
106
- return mapSessionToUserAttributes(currentSession);
107
- });
102
+ ).then((currentSession) => mapSessionToUserAttributes(currentSession));
108
103
 
109
104
  export const removeSession = async (domain: string) =>
110
105
  fetch(`/api/pluto/v1/sessions/current?publication_domain=${domain}`, {
@@ -20,6 +20,7 @@ declare global {
20
20
  [AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
21
21
  [namespace: string]: object;
22
22
  }>;
23
+ // @ts-ignore
23
24
  [AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
24
25
  [AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
25
26
  [AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
@@ -12,11 +12,11 @@ async function verifyAndSetUserAccessCookie() {
12
12
  const cookieName = 'VSTOKEN';
13
13
  const publicationDomain = getCurrentPublicationDomain();
14
14
  const cookie = getCookie(cookieName);
15
- let hasVerifiedVstoken = false;
16
15
 
17
16
  new UserDataRequest()
18
17
  .withAttributes(['uuid'])
19
- .subscribe(async ({ attributes, state }) => {
18
+ .fetch()
19
+ .then(async ({ attributes, state }) => {
20
20
  if (state.isLoggedIn === false) {
21
21
  if (typeof cookie === 'string') {
22
22
  // We don't want logged out users to have lingering VSTOKEN cookies
@@ -25,17 +25,6 @@ async function verifyAndSetUserAccessCookie() {
25
25
  return;
26
26
  }
27
27
 
28
- if (
29
- !attributes.uuid ||
30
- state.isLoggedIn === undefined ||
31
- hasVerifiedVstoken
32
- ) {
33
- // We only want to perform this job once, so we wait until we have all the data we need.
34
- return;
35
- }
36
-
37
- hasVerifiedVstoken = true; // Make sure we do this only once!
38
-
39
28
  if (typeof cookie !== 'string') {
40
29
  // We want to set a VSTOKEN cookie for the user so it's ready upon next page load
41
30
  await setUserAccessCookie(publicationDomain);
package/src/types.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  export type UserAttributes = {
2
- uuid?: string;
3
- name?: string;
4
- email?: string;
5
- phone?: string;
6
- trackingKey?: string;
2
+ uuid?: string | null;
3
+ name?: string | null;
4
+ email?: string | null;
5
+ phone?: string | null;
6
+ trackingKey?: string | null;
7
7
  extraData?: { [index: string]: unknown };
8
8
  access?: string[];
9
- privacyPreferences?: PrivacyPreferences;
9
+ privacyPreferences?: PrivacyPreferences | null;
10
10
  };
11
11
 
12
12
  export type State = {