@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.
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,15 @@ function useAtm() {
58
63
  }
59
64
  return ctx;
60
65
  }
66
+ let authPollingInterval = null;
61
67
  function useAuth() {
62
- const { client, user } = useAtm();
68
+ const { client, user, isSignedIn, emitLogout } = useAtm();
63
69
  const signInByAccessToken = async (accessToken) => {
64
70
  writeAccessTokenInCookie(accessToken);
65
71
  await fetchUser(client, user);
66
72
  };
67
73
  const logout = async () => {
74
+ stopAuthPolling();
68
75
  const accessToken = await getAccessToken();
69
76
  if (accessToken == null) {
70
77
  return;
@@ -72,13 +79,37 @@ function useAuth() {
72
79
  await client.value.account(accessToken).logout();
73
80
  user.value = null;
74
81
  writeAccessTokenInCookie(null);
82
+ emitLogout();
83
+ };
84
+ const startAuthPolling = () => {
85
+ if (authPollingInterval !== null) {
86
+ return;
87
+ }
88
+ authPollingInterval = window.setInterval(async () => {
89
+ if (!isSignedIn.value) {
90
+ return;
91
+ }
92
+ try {
93
+ await fetchUser(client, user);
94
+ } catch (error) {
95
+ await logout();
96
+ }
97
+ }, 3e3);
98
+ };
99
+ const stopAuthPolling = () => {
100
+ if (authPollingInterval !== null) {
101
+ clearInterval(authPollingInterval);
102
+ authPollingInterval = null;
103
+ }
75
104
  };
76
105
  return {
77
106
  user,
78
107
  signInByAccessToken,
79
108
  getAccessToken,
80
109
  fetchUser,
81
- logout
110
+ logout,
111
+ startAuthPolling,
112
+ stopAuthPolling
82
113
  };
83
114
  }
84
115
  const _hoisted_1$8 = { class: "atm-overlay" };
@@ -2459,6 +2490,19 @@ const atmPlugin = {
2459
2490
  const configurations = ref(null);
2460
2491
  const isSignedIn = computed(() => user.value !== void 0 && user.value !== null);
2461
2492
  const isLoaded = ref(false);
2493
+ const logoutCallbacks = [];
2494
+ const onLogout = (callback) => {
2495
+ logoutCallbacks.push(callback);
2496
+ return () => {
2497
+ const index = logoutCallbacks.indexOf(callback);
2498
+ if (index > -1) {
2499
+ logoutCallbacks.splice(index, 1);
2500
+ }
2501
+ };
2502
+ };
2503
+ const emitLogout = () => {
2504
+ logoutCallbacks.forEach((callback) => callback());
2505
+ };
2462
2506
  Promise.all([
2463
2507
  fetchUser(client, user),
2464
2508
  fetchConfigurations(client, configurations)
@@ -2468,7 +2512,9 @@ const atmPlugin = {
2468
2512
  user,
2469
2513
  configurations,
2470
2514
  isSignedIn,
2471
- isLoaded
2515
+ isLoaded,
2516
+ onLogout,
2517
+ emitLogout
2472
2518
  });
2473
2519
  }
2474
2520
  };