@autenticar-me/vue 0.14.0 → 0.15.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.
@@ -9,5 +9,7 @@ export type Atm = {
9
9
  configurations: Ref<undefined | null | Configurations>;
10
10
  isSignedIn: ComputedRef<boolean>;
11
11
  isLoaded: Ref<boolean>;
12
+ onLogout: (callback: () => void) => (() => void);
13
+ emitLogout: () => void;
12
14
  };
13
15
  //# sourceMappingURL=useAtm.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useAtm.d.ts","sourceRoot":"","sources":["../../src/composables/useAtm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,WAAW,EAAE,KAAK,GAAG,EAAE,MAAM,KAAK,CAAC;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAEnE,wBAAgB,MAAM,IAAI,GAAG,CAQ5B;AAED,MAAM,MAAM,GAAG,GAAG;IACd,MAAM,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC;IACvB,IAAI,EAAE,GAAG,CAAC,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC;IACnC,cAAc,EAAE,GAAG,CAAC,SAAS,GAAG,IAAI,GAAG,cAAc,CAAC,CAAC;IACvD,UAAU,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;CAC1B,CAAA"}
1
+ {"version":3,"file":"useAtm.d.ts","sourceRoot":"","sources":["../../src/composables/useAtm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,WAAW,EAAE,KAAK,GAAG,EAAE,MAAM,KAAK,CAAC;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAEnE,wBAAgB,MAAM,IAAI,GAAG,CAQ5B;AAED,MAAM,MAAM,GAAG,GAAG;IACd,MAAM,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC;IACvB,IAAI,EAAE,GAAG,CAAC,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC;IACnC,cAAc,EAAE,GAAG,CAAC,SAAS,GAAG,IAAI,GAAG,cAAc,CAAC,CAAC;IACvD,UAAU,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IACvB,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;IACjD,UAAU,EAAE,MAAM,IAAI,CAAC;CAC1B,CAAA"}
@@ -4,5 +4,7 @@ export declare function useAuth(): {
4
4
  getAccessToken: () => Promise<string | null>;
5
5
  fetchUser: (client: import('vue').Ref<import('@autenticar-me/client-js').AtmClient>, user: import('vue').Ref) => Promise<void>;
6
6
  logout: () => Promise<void>;
7
+ startAuthPolling: () => void;
8
+ stopAuthPolling: () => void;
7
9
  };
8
10
  //# sourceMappingURL=useAuth.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/composables/useAuth.ts"],"names":[],"mappings":"AAIA,wBAAgB,OAAO;;uCAG6B,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC;;;kBAK7C,OAAO,CAAC,IAAI,CAAC;EAqBzC"}
1
+ {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/composables/useAuth.ts"],"names":[],"mappings":"AAMA,wBAAgB,OAAO;;uCAG6B,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC;;;kBAK7C,OAAO,CAAC,IAAI,CAAC;4BAkBT,IAAI;2BAkBL,IAAI;EAgBnC"}
package/dist/index.cjs CHANGED
@@ -37,21 +37,26 @@ const fetchUser = async (client, user) => {
37
37
  user.value = null;
38
38
  return;
39
39
  }
40
- const response = await client.value.account(accessToken).getProfile();
41
- user.value = {
42
- id: response.id,
43
- username: response.username,
44
- firstName: response.first_name,
45
- lastName: response.last_name,
46
- primaryEmailAddress: response.primary_email_address,
47
- primaryEmailAddressId: response.primary_email_address_id,
48
- profileImage: response.profile_image,
49
- attributes: response.attributes.map((atribute) => ({
50
- key: atribute.key,
51
- mode: atribute.mode,
52
- value: atribute.value
53
- }))
54
- };
40
+ try {
41
+ const response = await client.value.account(accessToken).getProfile();
42
+ user.value = {
43
+ id: response.id,
44
+ username: response.username,
45
+ firstName: response.first_name,
46
+ lastName: response.last_name,
47
+ primaryEmailAddress: response.primary_email_address,
48
+ primaryEmailAddressId: response.primary_email_address_id,
49
+ profileImage: response.profile_image,
50
+ attributes: response.attributes.map((atribute) => ({
51
+ key: atribute.key,
52
+ mode: atribute.mode,
53
+ value: atribute.value
54
+ }))
55
+ };
56
+ } catch (error) {
57
+ user.value = null;
58
+ throw error;
59
+ }
55
60
  };
56
61
  function useAtm() {
57
62
  const ctx = vue.inject("atm");
@@ -60,13 +65,15 @@ function useAtm() {
60
65
  }
61
66
  return ctx;
62
67
  }
68
+ let authPollingInterval = null;
63
69
  function useAuth() {
64
- const { client, user } = useAtm();
70
+ const { client, user, isSignedIn, emitLogout } = useAtm();
65
71
  const signInByAccessToken = async (accessToken) => {
66
72
  writeAccessTokenInCookie(accessToken);
67
73
  await fetchUser(client, user);
68
74
  };
69
75
  const logout = async () => {
76
+ stopAuthPolling();
70
77
  const accessToken = await getAccessToken();
71
78
  if (accessToken == null) {
72
79
  return;
@@ -74,13 +81,37 @@ function useAuth() {
74
81
  await client.value.account(accessToken).logout();
75
82
  user.value = null;
76
83
  writeAccessTokenInCookie(null);
84
+ emitLogout();
85
+ };
86
+ const startAuthPolling = () => {
87
+ if (authPollingInterval !== null) {
88
+ return;
89
+ }
90
+ authPollingInterval = window.setInterval(async () => {
91
+ if (!isSignedIn.value) {
92
+ return;
93
+ }
94
+ try {
95
+ await fetchUser(client, user);
96
+ } catch (error) {
97
+ await logout();
98
+ }
99
+ }, 3e3);
100
+ };
101
+ const stopAuthPolling = () => {
102
+ if (authPollingInterval !== null) {
103
+ clearInterval(authPollingInterval);
104
+ authPollingInterval = null;
105
+ }
77
106
  };
78
107
  return {
79
108
  user,
80
109
  signInByAccessToken,
81
110
  getAccessToken,
82
111
  fetchUser,
83
- logout
112
+ logout,
113
+ startAuthPolling,
114
+ stopAuthPolling
84
115
  };
85
116
  }
86
117
  const _hoisted_1$8 = { class: "atm-overlay" };
@@ -2461,6 +2492,19 @@ const atmPlugin = {
2461
2492
  const configurations = vue.ref(null);
2462
2493
  const isSignedIn = vue.computed(() => user.value !== void 0 && user.value !== null);
2463
2494
  const isLoaded = vue.ref(false);
2495
+ const logoutCallbacks = [];
2496
+ const onLogout = (callback) => {
2497
+ logoutCallbacks.push(callback);
2498
+ return () => {
2499
+ const index = logoutCallbacks.indexOf(callback);
2500
+ if (index > -1) {
2501
+ logoutCallbacks.splice(index, 1);
2502
+ }
2503
+ };
2504
+ };
2505
+ const emitLogout = () => {
2506
+ logoutCallbacks.forEach((callback) => callback());
2507
+ };
2464
2508
  Promise.all([
2465
2509
  fetchUser(client, user),
2466
2510
  fetchConfigurations(client, configurations)
@@ -2470,7 +2514,9 @@ const atmPlugin = {
2470
2514
  user,
2471
2515
  configurations,
2472
2516
  isSignedIn,
2473
- isLoaded
2517
+ isLoaded,
2518
+ onLogout,
2519
+ emitLogout
2474
2520
  });
2475
2521
  }
2476
2522
  };