@autenticar-me/vue 0.14.0 → 0.16.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/dist/index.js CHANGED
@@ -35,21 +35,26 @@ const fetchUser = async (client, user) => {
35
35
  user.value = null;
36
36
  return;
37
37
  }
38
- const response = await client.value.account(accessToken).getProfile();
39
- user.value = {
40
- id: response.id,
41
- username: response.username,
42
- firstName: response.first_name,
43
- lastName: response.last_name,
44
- primaryEmailAddress: response.primary_email_address,
45
- primaryEmailAddressId: response.primary_email_address_id,
46
- profileImage: response.profile_image,
47
- attributes: response.attributes.map((atribute) => ({
48
- key: atribute.key,
49
- mode: atribute.mode,
50
- value: atribute.value
51
- }))
52
- };
38
+ try {
39
+ const response = await client.value.account(accessToken).getProfile();
40
+ user.value = {
41
+ id: response.id,
42
+ username: response.username,
43
+ firstName: response.first_name,
44
+ lastName: response.last_name,
45
+ primaryEmailAddress: response.primary_email_address,
46
+ primaryEmailAddressId: response.primary_email_address_id,
47
+ profileImage: response.profile_image,
48
+ attributes: response.attributes.map((atribute) => ({
49
+ key: atribute.key,
50
+ mode: atribute.mode,
51
+ value: atribute.value
52
+ }))
53
+ };
54
+ } catch (error) {
55
+ user.value = null;
56
+ throw error;
57
+ }
53
58
  };
54
59
  function useAtm() {
55
60
  const ctx = inject("atm");
@@ -58,13 +63,40 @@ function useAtm() {
58
63
  }
59
64
  return ctx;
60
65
  }
66
+ let authPollingInterval = null;
67
+ const startAuthPolling = (client, user, emitLogout) => {
68
+ if (authPollingInterval !== null) {
69
+ return;
70
+ }
71
+ authPollingInterval = window.setInterval(async () => {
72
+ if (user.value === null || user.value === void 0) {
73
+ return;
74
+ }
75
+ try {
76
+ await fetchUser(client, user);
77
+ } catch (error) {
78
+ stopAuthPolling();
79
+ user.value = null;
80
+ writeAccessTokenInCookie(null);
81
+ emitLogout();
82
+ }
83
+ }, 3e3);
84
+ };
85
+ const stopAuthPolling = () => {
86
+ if (authPollingInterval !== null) {
87
+ clearInterval(authPollingInterval);
88
+ authPollingInterval = null;
89
+ }
90
+ };
61
91
  function useAuth() {
62
- const { client, user } = useAtm();
92
+ const { client, user, emitLogout } = useAtm();
63
93
  const signInByAccessToken = async (accessToken) => {
64
94
  writeAccessTokenInCookie(accessToken);
65
95
  await fetchUser(client, user);
96
+ startAuthPolling(client, user, emitLogout);
66
97
  };
67
98
  const logout = async () => {
99
+ stopAuthPolling();
68
100
  const accessToken = await getAccessToken();
69
101
  if (accessToken == null) {
70
102
  return;
@@ -72,13 +104,22 @@ function useAuth() {
72
104
  await client.value.account(accessToken).logout();
73
105
  user.value = null;
74
106
  writeAccessTokenInCookie(null);
107
+ emitLogout();
108
+ };
109
+ const startAuthPolling$1 = () => {
110
+ startAuthPolling(client, user, emitLogout);
111
+ };
112
+ const stopAuthPolling$1 = () => {
113
+ stopAuthPolling();
75
114
  };
76
115
  return {
77
116
  user,
78
117
  signInByAccessToken,
79
118
  getAccessToken,
80
119
  fetchUser,
81
- logout
120
+ logout,
121
+ startAuthPolling: startAuthPolling$1,
122
+ stopAuthPolling: stopAuthPolling$1
82
123
  };
83
124
  }
84
125
  const _hoisted_1$8 = { class: "atm-overlay" };
@@ -2459,16 +2500,36 @@ const atmPlugin = {
2459
2500
  const configurations = ref(null);
2460
2501
  const isSignedIn = computed(() => user.value !== void 0 && user.value !== null);
2461
2502
  const isLoaded = ref(false);
2503
+ const logoutCallbacks = [];
2504
+ const onLogout = (callback) => {
2505
+ logoutCallbacks.push(callback);
2506
+ return () => {
2507
+ const index = logoutCallbacks.indexOf(callback);
2508
+ if (index > -1) {
2509
+ logoutCallbacks.splice(index, 1);
2510
+ }
2511
+ };
2512
+ };
2513
+ const emitLogout = () => {
2514
+ logoutCallbacks.forEach((callback) => callback());
2515
+ };
2462
2516
  Promise.all([
2463
2517
  fetchUser(client, user),
2464
2518
  fetchConfigurations(client, configurations)
2465
- ]).then(() => isLoaded.value = true);
2519
+ ]).then(() => {
2520
+ isLoaded.value = true;
2521
+ if (user.value !== null && user.value !== void 0) {
2522
+ startAuthPolling(client, user, emitLogout);
2523
+ }
2524
+ });
2466
2525
  app.provide("atm", {
2467
2526
  client,
2468
2527
  user,
2469
2528
  configurations,
2470
2529
  isSignedIn,
2471
- isLoaded
2530
+ isLoaded,
2531
+ onLogout,
2532
+ emitLogout
2472
2533
  });
2473
2534
  }
2474
2535
  };