@dpgradio/creative 7.0.3 → 7.0.5

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/README.md CHANGED
@@ -201,6 +201,26 @@ const radioToken = await hybrid.appLoaded()
201
201
  hybrid.decodeRadioToken(radioToken)
202
202
  ```
203
203
 
204
+ ## Authentication
205
+
206
+ ```js
207
+ import { authentication } from '@dpgradio/creative'
208
+
209
+ // Call once, at least before requiring authentication somewhere
210
+ authentication.initialize()
211
+
212
+ // Main usage: prompts for login if needed and sets the token in the API client
213
+ await authentication.require()
214
+
215
+
216
+ // Potentially helpful methods (but not required for typical usage)
217
+ authentication.isLoggedIn()
218
+ authentication.onRadioTokenChange(callback)
219
+ authentication.onLogin(callback)
220
+ authentication.askForLogin() // Does the same as require(), but does not await the result
221
+ authentication.radioToken
222
+ ```
223
+
204
224
  ## Sharing Generator
205
225
 
206
226
  Example:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dpgradio/creative",
3
- "version": "7.0.3",
3
+ "version": "7.0.5",
4
4
  "description": "Support package for standalone Javascript applications",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -4,6 +4,14 @@ import { onLocalStorageChange } from '../utils/onLocalStorageChange.js'
4
4
 
5
5
  export const RADIO_TOKEN_LOCAL_STORAGE_KEY = 'radio-auth-token'
6
6
 
7
+ const inDevelopment = () => {
8
+ try {
9
+ return import.meta.env?.DEV
10
+ } catch (error) {
11
+ return false
12
+ }
13
+ }
14
+
7
15
  class Authentication {
8
16
  constructor() {
9
17
  this.radioToken = null
@@ -19,7 +27,14 @@ class Authentication {
19
27
  this.setToken(radioToken)
20
28
  }
21
29
  })
22
- hybrid.on('authenticated', ({ radioToken }) => this.setToken(radioToken))
30
+ hybrid.on('authenticated', ({ radioToken }) => {
31
+ this.setToken(radioToken)
32
+ })
33
+ // When a user logs in or out in another view of the app than the one we're currently in,
34
+ // we need to update the token once the user returns to this view.
35
+ hybrid.on('didAppear', ({ radioToken }) => {
36
+ this.setToken(radioToken ?? null)
37
+ })
23
38
  onLocalStorageChange(RADIO_TOKEN_LOCAL_STORAGE_KEY, (token) => this.setToken(token), true)
24
39
  }
25
40
 
@@ -43,7 +58,7 @@ class Authentication {
43
58
  askForLogin() {
44
59
  this.markAskingForLogin(true)
45
60
 
46
- if (import.meta?.env?.DEV) {
61
+ if (inDevelopment()) {
47
62
  this.setToken(prompt('[DEVELOPMENT] Please enter your radio token:'))
48
63
  } else if (hybrid.isNativeApp()) {
49
64
  hybrid.call('showAuthentication', { tier: 'light' })
@@ -70,7 +85,11 @@ class Authentication {
70
85
  return new Promise((resolve, reject) => {
71
86
  const onCompletion = () => (this.radioToken ? resolve() : reject('There is no authenticated user.'))
72
87
  this.onRadioTokenChange(onCompletion)
73
- this.askingForLoginListeners.push(onCompletion)
88
+ this.askingForLoginListeners.push((askingForLogin) => {
89
+ if (!askingForLogin) {
90
+ onCompletion()
91
+ }
92
+ })
74
93
 
75
94
  this.askForLogin()
76
95
  })
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'