@dpgradio/creative 7.0.2 → 7.0.4
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 +1 -1
- package/src/app/authentication.js +96 -0
- package/src/index.js +2 -0
package/package.json
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import api from '../api/api.js'
|
|
2
|
+
import hybrid from './hybrid.js'
|
|
3
|
+
import { onLocalStorageChange } from '../utils/onLocalStorageChange.js'
|
|
4
|
+
|
|
5
|
+
export const RADIO_TOKEN_LOCAL_STORAGE_KEY = 'radio-auth-token'
|
|
6
|
+
|
|
7
|
+
class Authentication {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.radioToken = null
|
|
10
|
+
this.askingForLogin = false
|
|
11
|
+
|
|
12
|
+
this.radioTokenListeners = []
|
|
13
|
+
this.askingForLoginListeners = []
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
initialize() {
|
|
17
|
+
hybrid.appLoaded().then((radioToken) => {
|
|
18
|
+
if (radioToken) {
|
|
19
|
+
this.setToken(radioToken)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
hybrid.on('authenticated', ({ radioToken }) => this.setToken(radioToken))
|
|
23
|
+
onLocalStorageChange(RADIO_TOKEN_LOCAL_STORAGE_KEY, (token) => this.setToken(token), true)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
markAskingForLogin(state) {
|
|
27
|
+
this.askingForLogin = state
|
|
28
|
+
this.askingForLoginListeners.forEach((listener) => listener(state))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setToken(token) {
|
|
32
|
+
this.radioToken = token
|
|
33
|
+
|
|
34
|
+
api.setRadioToken(token)
|
|
35
|
+
|
|
36
|
+
if (token) {
|
|
37
|
+
this.markAskingForLogin(false)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.radioTokenListeners.forEach((listener) => listener(token))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
askForLogin() {
|
|
44
|
+
this.markAskingForLogin(true)
|
|
45
|
+
|
|
46
|
+
if (import.meta?.env?.DEV) {
|
|
47
|
+
this.setToken(prompt('[DEVELOPMENT] Please enter your radio token:'))
|
|
48
|
+
} else if (hybrid.isNativeApp()) {
|
|
49
|
+
hybrid.call('showAuthentication', { tier: 'light' })
|
|
50
|
+
} else {
|
|
51
|
+
window.location.href = '/login'
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Ensure that the user is authenticated by awaiting this method.
|
|
57
|
+
*
|
|
58
|
+
* Example usage:
|
|
59
|
+
*
|
|
60
|
+
* ```js
|
|
61
|
+
* await authentication.require()
|
|
62
|
+
* like(track)
|
|
63
|
+
* ```
|
|
64
|
+
**/
|
|
65
|
+
require() {
|
|
66
|
+
if (this.radioToken) {
|
|
67
|
+
return Promise.resolve()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const onCompletion = () => (this.radioToken ? resolve() : reject('There is no authenticated user.'))
|
|
72
|
+
this.onRadioTokenChange(onCompletion)
|
|
73
|
+
this.askingForLoginListeners.push(onCompletion)
|
|
74
|
+
|
|
75
|
+
this.askForLogin()
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
isLoggedIn() {
|
|
80
|
+
return !!this.radioToken
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
onRadioTokenChange(listener) {
|
|
84
|
+
this.radioTokenListeners.push(listener)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
onLogin(listener) {
|
|
88
|
+
this.onRadioTokenChange(() => {
|
|
89
|
+
if (this.radioToken) {
|
|
90
|
+
listener()
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default new Authentication()
|
package/src/index.js
CHANGED
|
@@ -9,6 +9,8 @@ export { default as configuration, config } from './config/config.js'
|
|
|
9
9
|
|
|
10
10
|
export { default as api } from './api/api.js'
|
|
11
11
|
|
|
12
|
+
export { default as authentication } from './app/authentication.js'
|
|
13
|
+
|
|
12
14
|
export { default as loadScript } from './utils/loadScript.js'
|
|
13
15
|
export { default as openLink } from './utils/openLink.js'
|
|
14
16
|
export { default as tap } from './utils/tap.js'
|