@autenticar-me/vue 0.16.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/README.md CHANGED
@@ -38,6 +38,100 @@ app.mount('#app')
38
38
  - **`useAuth()`** - Gerenciamento de autenticação
39
39
  - **`useTheme()`** - Controle de tema (light/dark)
40
40
 
41
+ ## Verificação Periódica de Autenticação
42
+
43
+ O pacote inclui um sistema automático de verificação de autenticação que monitora o estado de login do usuário a cada 3 segundos. Se a sessão expirar ou o usuário for deslogado no servidor, o logout é executado automaticamente e um evento é emitido.
44
+
45
+ ### Como Funciona
46
+
47
+ - Após o login, a verificação inicia automaticamente
48
+ - A cada 3 segundos, faz uma requisição à rota de perfil do usuário
49
+ - Se a requisição falhar (token inválido, sessão expirada, etc.):
50
+ - Executa logout automático
51
+ - Limpa os dados do usuário
52
+ - Remove tokens dos cookies
53
+ - Emite evento de logout
54
+
55
+ ### Capturando o Evento de Logout
56
+
57
+ Use o método `onLogout()` do composable `useAtm()` para registrar callbacks que serão executados quando o logout ocorrer (manual ou automático):
58
+
59
+ ```vue
60
+ <template>
61
+ <div>
62
+ <h1>Minha Aplicação</h1>
63
+ <UserProfile />
64
+ </div>
65
+ </template>
66
+
67
+ <script setup>
68
+ import { onMounted, onUnmounted } from 'vue'
69
+ import { useAtm } from '@autenticar-me/vue'
70
+ import { useRouter } from 'vue-router'
71
+
72
+ const { onLogout } = useAtm()
73
+ const router = useRouter()
74
+
75
+ onMounted(() => {
76
+ const unsubscribe = onLogout(() => {
77
+ console.log('Usuário foi deslogado')
78
+ router.push('/login')
79
+ })
80
+
81
+ onUnmounted(() => {
82
+ unsubscribe()
83
+ })
84
+ })
85
+ </script>
86
+ ```
87
+
88
+ ### Exemplo com Notificação ao Usuário
89
+
90
+ ```vue
91
+ <script setup>
92
+ import { onMounted, onUnmounted } from 'vue'
93
+ import { useAtm } from '@autenticar-me/vue'
94
+ import { useRouter } from 'vue-router'
95
+
96
+ const { onLogout } = useAtm()
97
+ const router = useRouter()
98
+
99
+ onMounted(() => {
100
+ const unsubscribe = onLogout(() => {
101
+ alert('Sua sessão expirou. Por favor, faça login novamente.')
102
+ router.push('/login')
103
+ })
104
+
105
+ onUnmounted(() => {
106
+ unsubscribe()
107
+ })
108
+ })
109
+ </script>
110
+ ```
111
+
112
+ ### Controle Manual do Polling
113
+
114
+ Caso precise controlar manualmente quando o polling inicia ou para:
115
+
116
+ ```vue
117
+ <script setup>
118
+ import { onMounted, onUnmounted } from 'vue'
119
+ import { useAuth } from '@autenticar-me/vue'
120
+
121
+ const { startAuthPolling, stopAuthPolling } = useAuth()
122
+
123
+ onMounted(() => {
124
+ startAuthPolling()
125
+ })
126
+
127
+ onUnmounted(() => {
128
+ stopAuthPolling()
129
+ })
130
+ </script>
131
+ ```
132
+
133
+ **Nota:** Na maioria dos casos, não é necessário controlar manualmente o polling, pois ele inicia automaticamente após o login e para automaticamente no logout.
134
+
41
135
  ## Componentes
42
136
 
43
137
  ### Formulários de Autenticação
package/dist/index.cjs CHANGED
@@ -71,7 +71,10 @@ const startAuthPolling = (client, user, emitLogout) => {
71
71
  return;
72
72
  }
73
73
  authPollingInterval = window.setInterval(async () => {
74
- if (user.value === null || user.value === void 0) {
74
+ const hasToken = document.cookie.includes("atm_access_token") || document.cookie.includes("atm_refresh_token");
75
+ if (!hasToken || user.value === null || user.value === void 0) {
76
+ stopAuthPolling();
77
+ emitLogout();
75
78
  return;
76
79
  }
77
80
  try {