@nitra/vite-boot 1.0.11 → 1.0.12
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/package.json +1 -1
- package/src/pinia.js +1 -1
- package/src/token.js +40 -15
package/package.json
CHANGED
package/src/pinia.js
CHANGED
package/src/token.js
CHANGED
|
@@ -1,27 +1,52 @@
|
|
|
1
|
+
import { createLogger } from '@nitra/consola'
|
|
2
|
+
|
|
3
|
+
const consola = createLogger(import.meta.url)
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Ініціалізація Sentry
|
|
3
7
|
*/
|
|
4
8
|
export const refreshToken = async () => {
|
|
5
9
|
try {
|
|
6
|
-
const
|
|
7
|
-
method: 'POST',
|
|
8
|
-
headers: {
|
|
9
|
-
'Content-Type': 'application/json'
|
|
10
|
-
},
|
|
11
|
-
body: JSON.stringify({ token: localStorage.getItem('token') })
|
|
12
|
-
})
|
|
13
|
-
const result = await response.json()
|
|
10
|
+
const token = localStorage.getItem('token')
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
consola.debug('token refresh', token)
|
|
13
|
+
|
|
14
|
+
if (!token) {
|
|
15
|
+
// Чекаємо, можливо це паралельний JWTExpired
|
|
16
|
+
consola.debug('begin wait 5 sec')
|
|
17
|
+
await sleep(5000)
|
|
18
|
+
consola.debug('end wait, start logout')
|
|
22
19
|
window.location.replace('/logout')
|
|
20
|
+
} else {
|
|
21
|
+
// Прибираємо токен, щоб інші паралельні JWTExpired не виконувались
|
|
22
|
+
localStorage.removeItem('token')
|
|
23
|
+
consola.debug('token cleaned')
|
|
24
|
+
|
|
25
|
+
const response = await fetch('/refresh-token', {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
headers: {
|
|
28
|
+
'Content-Type': 'application/json'
|
|
29
|
+
},
|
|
30
|
+
body: JSON.stringify({ token })
|
|
31
|
+
})
|
|
32
|
+
const result = await response.json()
|
|
33
|
+
consola.debug('result refresh', result)
|
|
34
|
+
|
|
35
|
+
if (result.token) {
|
|
36
|
+
// якщо токен прийшов - замінюємо його
|
|
37
|
+
localStorage.setItem('token', result.token)
|
|
38
|
+
// та перезавантажуємо щоб він вступив в дію
|
|
39
|
+
window.location.reload()
|
|
40
|
+
} else {
|
|
41
|
+
// інакше вихід
|
|
42
|
+
window.location.replace('/logout')
|
|
43
|
+
}
|
|
23
44
|
}
|
|
24
45
|
} catch (error) {
|
|
25
46
|
console.error('Error:', error)
|
|
26
47
|
}
|
|
27
48
|
}
|
|
49
|
+
|
|
50
|
+
function sleep(ms) {
|
|
51
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
52
|
+
}
|