@autenticar-me/vue 0.15.0 → 0.17.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
@@ -64,11 +64,39 @@ function useAtm() {
64
64
  return ctx;
65
65
  }
66
66
  let authPollingInterval = null;
67
+ const startAuthPolling = (client, user, emitLogout) => {
68
+ if (authPollingInterval !== null) {
69
+ return;
70
+ }
71
+ authPollingInterval = window.setInterval(async () => {
72
+ const hasToken = document.cookie.includes("atm_access_token") || document.cookie.includes("atm_refresh_token");
73
+ if (!hasToken || user.value === null || user.value === void 0) {
74
+ stopAuthPolling();
75
+ emitLogout();
76
+ return;
77
+ }
78
+ try {
79
+ await fetchUser(client, user);
80
+ } catch (error) {
81
+ stopAuthPolling();
82
+ user.value = null;
83
+ writeAccessTokenInCookie(null);
84
+ emitLogout();
85
+ }
86
+ }, 3e3);
87
+ };
88
+ const stopAuthPolling = () => {
89
+ if (authPollingInterval !== null) {
90
+ clearInterval(authPollingInterval);
91
+ authPollingInterval = null;
92
+ }
93
+ };
67
94
  function useAuth() {
68
- const { client, user, isSignedIn, emitLogout } = useAtm();
95
+ const { client, user, emitLogout } = useAtm();
69
96
  const signInByAccessToken = async (accessToken) => {
70
97
  writeAccessTokenInCookie(accessToken);
71
98
  await fetchUser(client, user);
99
+ startAuthPolling(client, user, emitLogout);
72
100
  };
73
101
  const logout = async () => {
74
102
  stopAuthPolling();
@@ -81,26 +109,11 @@ function useAuth() {
81
109
  writeAccessTokenInCookie(null);
82
110
  emitLogout();
83
111
  };
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);
112
+ const startAuthPolling$1 = () => {
113
+ startAuthPolling(client, user, emitLogout);
98
114
  };
99
- const stopAuthPolling = () => {
100
- if (authPollingInterval !== null) {
101
- clearInterval(authPollingInterval);
102
- authPollingInterval = null;
103
- }
115
+ const stopAuthPolling$1 = () => {
116
+ stopAuthPolling();
104
117
  };
105
118
  return {
106
119
  user,
@@ -108,8 +121,8 @@ function useAuth() {
108
121
  getAccessToken,
109
122
  fetchUser,
110
123
  logout,
111
- startAuthPolling,
112
- stopAuthPolling
124
+ startAuthPolling: startAuthPolling$1,
125
+ stopAuthPolling: stopAuthPolling$1
113
126
  };
114
127
  }
115
128
  const _hoisted_1$8 = { class: "atm-overlay" };
@@ -2506,7 +2519,12 @@ const atmPlugin = {
2506
2519
  Promise.all([
2507
2520
  fetchUser(client, user),
2508
2521
  fetchConfigurations(client, configurations)
2509
- ]).then(() => isLoaded.value = true);
2522
+ ]).then(() => {
2523
+ isLoaded.value = true;
2524
+ if (user.value !== null && user.value !== void 0) {
2525
+ startAuthPolling(client, user, emitLogout);
2526
+ }
2527
+ });
2510
2528
  app.provide("atm", {
2511
2529
  client,
2512
2530
  user,