@dpgradio/creative 9.0.0 → 9.0.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.
@@ -13,7 +13,7 @@ jobs:
13
13
  cache: npm
14
14
  registry-url: "https://registry.npmjs.org" # This is needed to make npm publish work...
15
15
  - run: npm ci
16
- - run: npm version ${{ github.event.release.tag_name }} --no-git-tag-version
16
+ - run: npm version ${{ github.event.release.tag_name }} --no-git-tag-version --allow-same-version
17
17
  - run: npm publish
18
18
  env:
19
19
  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dpgradio/creative",
3
- "version": "9.0.0",
3
+ "version": "9.0.2",
4
4
  "description": "Support package for standalone Javascript applications",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  import api from '../api/api.js'
2
2
  import hybrid from './hybrid.js'
3
3
  import { onLocalStorageChange } from '../utils/onLocalStorageChange.js'
4
+ import decodeRadioToken from '../utils/decodeRadioToken.js'
4
5
 
5
6
  export const RADIO_TOKEN_LOCAL_STORAGE_KEY = 'radio-auth-token'
6
7
 
@@ -19,6 +20,18 @@ class Authentication {
19
20
 
20
21
  this.radioTokenListeners = []
21
22
  this.askingForLoginListeners = []
23
+
24
+ this._refreshTokenPromise = null
25
+ }
26
+
27
+ _isValidRadioToken(token) {
28
+ try {
29
+ decodeRadioToken(token)
30
+ return true
31
+ } catch (error) {
32
+ console.warn('Invalid radio token detected, will be removed from localStorage:', error)
33
+ return false
34
+ }
22
35
  }
23
36
 
24
37
  initialize() {
@@ -38,7 +51,22 @@ class Authentication {
38
51
  hybrid.on('didAppear', ({ radioToken }) => {
39
52
  this.setToken(radioToken ?? null)
40
53
  })
41
- onLocalStorageChange(RADIO_TOKEN_LOCAL_STORAGE_KEY, (token) => this.setToken(token), true)
54
+ onLocalStorageChange(
55
+ RADIO_TOKEN_LOCAL_STORAGE_KEY,
56
+ (token) => {
57
+ if (!token) {
58
+ this.setToken(null)
59
+ } else if (this._isValidRadioToken(token)) {
60
+ this.setToken(token)
61
+ } else {
62
+ // Token might be broken. Remove it and try a refresh. This will also ask for login.
63
+ localStorage.removeItem(RADIO_TOKEN_LOCAL_STORAGE_KEY)
64
+ this.setToken(null)
65
+ this.refreshToken().catch(() => {})
66
+ }
67
+ },
68
+ true
69
+ )
42
70
  }
43
71
 
44
72
  markAskingForLogin(state) {
@@ -114,7 +142,7 @@ class Authentication {
114
142
  })
115
143
  }
116
144
 
117
- async refreshToken() {
145
+ async _doRefreshToken() {
118
146
  if (hybrid.isNativeApp()) {
119
147
  const updatedToken = Promise.race([
120
148
  new Promise((resolve) => {
@@ -136,6 +164,9 @@ class Authentication {
136
164
  'Content-Type': 'application/json',
137
165
  },
138
166
  })
167
+ if (!response.ok) {
168
+ throw new Error(`Failed to refresh token: ${response.status} ${response.statusText}`)
169
+ }
139
170
  const token = (await response.json()).radioToken
140
171
  localStorage.setItem(RADIO_TOKEN_LOCAL_STORAGE_KEY, token)
141
172
  return token
@@ -145,6 +176,18 @@ class Authentication {
145
176
  }
146
177
  }
147
178
  }
179
+
180
+ async refreshToken() {
181
+ if (this._refreshTokenPromise) {
182
+ return this._refreshTokenPromise
183
+ }
184
+ this._refreshTokenPromise = this._doRefreshToken()
185
+ try {
186
+ return await this._refreshTokenPromise
187
+ } finally {
188
+ this._refreshTokenPromise = null
189
+ }
190
+ }
148
191
  }
149
192
 
150
193
  export default new Authentication()