@nitra/vite-boot 2.1.7 → 2.1.8
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 +2 -1
- package/src/apollo.js +65 -64
- package/src/auto-login.js +18 -0
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nitra/vite-boot",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.8",
|
|
4
4
|
"description": "Vite boot",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
+
"./auto-login": "./src/auto-login.js",
|
|
7
8
|
"./apollo": "./src/apollo.js",
|
|
8
9
|
"./i18n": "./src/i18n.js",
|
|
9
10
|
"./pinia": "./src/pinia.js",
|
package/src/apollo.js
CHANGED
|
@@ -4,78 +4,79 @@ 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'
|
|
8
|
-
|
|
9
|
-
export let apolloClient
|
|
10
7
|
|
|
11
8
|
const consola = createLogger(import.meta.url)
|
|
12
9
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
const wsClient = createClient({
|
|
11
|
+
url: `${import.meta.env.VITE_HASURA_PROTOCOL}://${import.meta.env.VITE_HASURA_HOST}/v1/graphql`,
|
|
12
|
+
disablePong: true,
|
|
13
|
+
connectionParams: () => {
|
|
14
|
+
// const session = getSession();
|
|
15
|
+
// if (!session) {
|
|
16
|
+
// return {};
|
|
17
|
+
// }
|
|
18
|
+
const token = localStorage.getItem('token')
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
window.location.replace('/logout')
|
|
25
|
-
} else if (tokenDecoded.exp < Math.floor(Date.now() / 1000)) {
|
|
26
|
-
// Перевіряємо чи не старий токен знаходиться в localStorage
|
|
27
|
-
await refreshToken()
|
|
28
|
-
} else {
|
|
29
|
-
const wsClient = createClient({
|
|
30
|
-
url: `${import.meta.env.VITE_HASURA_PROTOCOL}://${import.meta.env.VITE_HASURA_HOST}/v1/graphql`,
|
|
31
|
-
disablePong: true,
|
|
32
|
-
connectionParams: () => {
|
|
33
|
-
return {
|
|
34
|
-
headers: {
|
|
35
|
-
Authorization: `Bearer ${token}`,
|
|
36
|
-
'x-hasura-role': import.meta.env.VITE_HASURA_ROLE
|
|
37
|
-
}
|
|
38
|
-
}
|
|
20
|
+
return {
|
|
21
|
+
headers: {
|
|
22
|
+
Authorization: `Bearer ${token}`,
|
|
23
|
+
'x-hasura-role': import.meta.env.VITE_HASURA_ROLE
|
|
39
24
|
}
|
|
40
|
-
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
})
|
|
41
28
|
|
|
42
|
-
|
|
29
|
+
const wsLink = new GraphQLWsLink(wsClient)
|
|
43
30
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
}
|
|
31
|
+
const errorLink = onError(({ graphQLErrors, networkError, _operation, _forward }) => {
|
|
32
|
+
if (graphQLErrors) {
|
|
33
|
+
for (const err of graphQLErrors) {
|
|
34
|
+
consola.debug('graphQLErrors: ', err)
|
|
50
35
|
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
}
|
|
36
|
+
// switch (err.extensions.code) {
|
|
37
|
+
// // Apollo Server sets code to UNAUTHENTICATED
|
|
38
|
+
// // when an AuthenticationError is thrown in a resolver
|
|
39
|
+
// case 'UNAUTHENTICATED':
|
|
40
|
+
// // Modify the operation context with a new token
|
|
41
|
+
// const oldHeaders = operation.getContext().headers
|
|
42
|
+
// operation.setContext({
|
|
43
|
+
// headers: {
|
|
44
|
+
// ...oldHeaders,
|
|
45
|
+
// authorization: getNewToken()
|
|
46
|
+
// }
|
|
47
|
+
// })
|
|
48
|
+
// // Retry the request, returning the new observable
|
|
49
|
+
// return forward(operation)
|
|
50
|
+
// }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
67
53
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
54
|
+
// To retry on network errors, we recommend the RetryLink
|
|
55
|
+
// instead of the onError link. This just logs the error.
|
|
56
|
+
if (networkError) {
|
|
57
|
+
if (networkError?.message?.match('JWTExpired')) {
|
|
58
|
+
// ретрай токен
|
|
59
|
+
// и якщо цей користувач ще валідний в БД
|
|
60
|
+
// то видавати йому новий токен
|
|
61
|
+
// тобто ця перевірка універсальна
|
|
62
|
+
// не в залежності який типом авторизувався користувач
|
|
63
|
+
// а в залежності від того чи він валідний в БД
|
|
64
|
+
refreshToken()
|
|
65
|
+
} else {
|
|
66
|
+
console.log(`[Network error]: ${networkError}`)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
})
|
|
78
70
|
|
|
79
|
-
|
|
71
|
+
// Create the apollo client
|
|
72
|
+
export const apolloClient = new ApolloClient({
|
|
73
|
+
link: from([errorLink, wsLink]),
|
|
74
|
+
cache: new InMemoryCache(),
|
|
75
|
+
defaultOptions: {
|
|
76
|
+
watchQuery: {
|
|
77
|
+
fetchPolicy: 'cache-and-network'
|
|
78
|
+
}
|
|
80
79
|
}
|
|
81
|
-
}
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
consola.debug('End Apollo boot')
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { refreshToken } from './token.js'
|
|
2
|
+
import jwtDecode from '@nitra/jwt-decode'
|
|
3
|
+
|
|
4
|
+
const token = localStorage.getItem('token')
|
|
5
|
+
|
|
6
|
+
if (token) {
|
|
7
|
+
const tokenDecoded = jwtDecode(token)
|
|
8
|
+
|
|
9
|
+
if (
|
|
10
|
+
!tokenDecoded['https://hasura.io/jwt/claims']['x-hasura-allowed-roles'].includes(import.meta.env.VITE_HASURA_ROLE)
|
|
11
|
+
) {
|
|
12
|
+
// Не дозволена роль поточного користувача в поточному прикладенні
|
|
13
|
+
window.location.replace('/logout')
|
|
14
|
+
} else if (tokenDecoded.exp < Math.floor(Date.now() / 1000)) {
|
|
15
|
+
// Перевіряємо чи не старий токен знаходиться в localStorage
|
|
16
|
+
await refreshToken()
|
|
17
|
+
}
|
|
18
|
+
}
|