@nitra/vite-boot 2.0.0 → 2.1.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitra/vite-boot",
3
- "version": "2.0.0",
3
+ "version": "2.1.2",
4
4
  "description": "Vite boot",
5
5
  "type": "module",
6
6
  "exports": {
@@ -41,6 +41,7 @@
41
41
  "dependencies": {
42
42
  "@apollo/client": "^3.5.10",
43
43
  "@nitra/consola": "^1.7.0",
44
+ "@nitra/jwt-decode": "^1.1.3",
44
45
  "@sentry/tracing": "^6.16.1",
45
46
  "@sentry/vue": "^6.16.1",
46
47
  "graphql-ws": "^5.5.5",
package/src/apollo.js CHANGED
@@ -4,9 +4,22 @@ import { createClient } from 'graphql-ws'
4
4
  import { createLogger } from '@nitra/consola'
5
5
  import { refreshToken } from './token.js'
6
6
  import { GraphQLWsLink } from '@apollo/client/link/subscriptions'
7
+ import jwtDecode from '@nitra/jwt-decode'
7
8
 
8
9
  const consola = createLogger(import.meta.url)
9
10
 
11
+ const token = localStorage.getItem('token')
12
+
13
+ if (!token) {
14
+ window.location.replace('/logout')
15
+ }
16
+
17
+ // Перевіряємо чи не старий токен знаходиться в localStorage
18
+ const tokenDecoded = jwtDecode(token)
19
+ if (tokenDecoded.exp < Math.floor(Date.now() / 1000)) {
20
+ refreshToken()
21
+ }
22
+
10
23
  const wsClient = createClient({
11
24
  url: `${import.meta.env.VITE_HASURA_PROTOCOL}://${import.meta.env.VITE_HASURA_HOST}/v1/graphql`,
12
25
  disablePong: true,
@@ -15,8 +28,6 @@ const wsClient = createClient({
15
28
  // if (!session) {
16
29
  // return {};
17
30
  // }
18
- const token = localStorage.getItem('token')
19
-
20
31
  return {
21
32
  headers: {
22
33
  Authorization: `Bearer ${token}`,
package/src/token.js CHANGED
@@ -23,6 +23,7 @@ export const refreshToken = async () => {
23
23
  consola.debug('token cleaned')
24
24
 
25
25
  const response = await fetch('/refresh-token', {
26
+ credentials: 'include',
26
27
  method: 'POST',
27
28
  headers: {
28
29
  'Content-Type': 'application/json'