@data-fair/lib-vue 1.10.4 → 1.12.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/session.js +15 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.10.4",
3
+ "version": "1.12.0",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
package/session.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { FetchError } from 'ofetch'
1
2
  import { reactive, computed, watch, inject, ref } from 'vue'
2
3
  import { ofetch } from 'ofetch'
3
4
  import { jwtDecode } from 'jwt-decode'
@@ -13,13 +14,12 @@ function jwtDecodeAlive (jwt) {
13
14
  if (!decoded) { return }
14
15
  const now = Math.ceil(Date.now().valueOf() / 1000)
15
16
  if (typeof decoded.exp !== 'undefined' && decoded.exp < now) {
16
- console.error(`token expired: ${decoded.exp}<${now}, ${JSON.stringify(decoded)}`)
17
+ // token expired
17
18
  return
18
19
  }
19
20
  if (typeof decoded.nbf !== 'undefined' && decoded.nbf > now) {
20
21
  console.warn(`token not yet valid: ${decoded.nbf}>${now}, ${JSON.stringify(decoded)}`)
21
- // do not return null here, this is probably a false flag due to a slightly mismatched clock
22
- // return null
22
+ // do not return here, this is probably a false flag due to a slightly mismatched clock
23
23
  }
24
24
  return decoded
25
25
  }
@@ -211,8 +211,16 @@ export async function getSession (initOptions) {
211
211
  if (!ssr) {
212
212
  window.localStorage.setItem('sd-keepalive' + options.sitePath, `${new Date().getTime()}`)
213
213
  }
214
- await customFetch(`${options.directoryUrl}/api/auth/keepalive`, { method: 'POST' })
215
- readState()
214
+ try {
215
+ await customFetch(`${options.directoryUrl}/api/auth/keepalive`, { method: 'POST' })
216
+ } catch (err) {
217
+ if (err instanceof FetchError && err.statusCode === 401) {
218
+ console.warn('session was expired or deleted server side')
219
+ } else {
220
+ throw err
221
+ }
222
+ readState()
223
+ }
216
224
  }
217
225
  const refreshSiteInfo = async () => {
218
226
  const siteInfo = await customFetch(`${options.directoryUrl}/api/sites/_public`) ?? null
@@ -224,7 +232,8 @@ export async function getSession (initOptions) {
224
232
  // also run an auto-refresh loop
225
233
  if (!ssr && !inIframe) {
226
234
  const lastKeepalive = window.localStorage.getItem('sd-keepalive' + options.sitePath)
227
- if (state.user && (!lastKeepalive || (new Date().getTime() - Number(lastKeepalive)) > 10000)) {
235
+ // check cookies.get('id_token') not state.user so that we do a keepalive on expired id tokens
236
+ if (cookies.get('id_token') && (!lastKeepalive || (new Date().getTime() - Number(lastKeepalive)) > 10000)) {
228
237
  await keepalive()
229
238
  }
230
239
  const refreshLoopDelay = 10 * 60 * 1000 // 10 minutes