@dpgradio/creative 9.0.1 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dpgradio/creative",
3
- "version": "9.0.1",
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
 
@@ -23,6 +24,16 @@ class Authentication {
23
24
  this._refreshTokenPromise = null
24
25
  }
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
+ }
35
+ }
36
+
26
37
  initialize() {
27
38
  hybrid
28
39
  .appLoaded()
@@ -40,7 +51,22 @@ class Authentication {
40
51
  hybrid.on('didAppear', ({ radioToken }) => {
41
52
  this.setToken(radioToken ?? null)
42
53
  })
43
- 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
+ )
44
70
  }
45
71
 
46
72
  markAskingForLogin(state) {