@inglorious/web 6.24.0 → 6.25.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.
- package/README.md +18 -1
- package/package.json +1 -1
- package/src/auth/index.js +46 -16
- package/src/decorators/with-debounce/index.js +9 -11
- package/src/decorators/with-throttle/index.js +8 -14
- package/types/auth.d.ts +12 -0
package/README.md
CHANGED
|
@@ -1594,9 +1594,26 @@ watchAuth(adapter, store) // keeps `user` in step with the session
|
|
|
1594
1594
|
|
|
1595
1595
|
The entity's `step` says what to ask for: `"email"`, `"password"`, `"signUp"`, `"code"` or `"reset"`. Its events take what the person typed: `emailSubmit(email)`, `signInSubmit(password)`, `signUpSubmit({ password, data })`, `codeSubmit(code)`, `resetSubmit({ code, password })`, and move between steps: `signUpShow`, `signInShow`, `codeRequest`, `codeResend`, `resetRequest`, `back`. Signed in, `signOut` and `accountEraseAsk`, then `accountErase` or `accountEraseCancel`. `isReady` turns true once the saved session is read: until then, render nothing rather than the sign-in screen.
|
|
1596
1596
|
|
|
1597
|
+
Whoever signs in, by password, code, link or password reset, the entity gets `signedIn` with `{ user, isNew }`, `isNew` for an account just created. Add a handler to move on:
|
|
1598
|
+
|
|
1599
|
+
```javascript
|
|
1600
|
+
const types = {
|
|
1601
|
+
Auth: [
|
|
1602
|
+
createAuth({ adapter }),
|
|
1603
|
+
{
|
|
1604
|
+
signedIn(entity, { isNew }, api) {
|
|
1605
|
+
api.notify("#router:navigate", isNew ? "/onboarding" : "/")
|
|
1606
|
+
},
|
|
1607
|
+
},
|
|
1608
|
+
],
|
|
1609
|
+
}
|
|
1610
|
+
```
|
|
1611
|
+
|
|
1612
|
+
For a page that a confirmation email's link opens, `linkSubmit({ tokenHash })` signs in with the link instead of the code. Ask for a tap first: mail scanners open links too.
|
|
1613
|
+
|
|
1597
1614
|
`status` is `"loading"` while waiting, and `error` is a key to put into words: `"invalidEmail"`, `"invalidCode"`, `"weakPassword"`, `"invalidCredentials"`, `"accountExists"`, `"rateLimited"`, `"captcha"`, `"network"` or `"unknown"`. An account not confirmed yet goes straight to the code.
|
|
1598
1615
|
|
|
1599
|
-
The adapter talks to the backend. `supabaseAuth` from `@inglorious/supabase` is one; another backend needs `onSessionChange`, `signIn`, `signUp`, `sendCode`, `verifyCode`, `resendCode`, `sendResetCode`, `updatePassword`, `signOut`, `eraseAccount` and `errorCode`, plus `accountExists` to skip straight to the password or the new account.
|
|
1616
|
+
The adapter talks to the backend. `supabaseAuth` from `@inglorious/supabase` is one; another backend needs `onSessionChange`, `signIn`, `signUp`, `sendCode`, `verifyCode`, `verifyLink`, `resendCode`, `sendResetCode`, `updatePassword`, `signOut`, `eraseAccount` and `errorCode`, plus `accountExists` to skip straight to the password or the new account, and `prepare(action)`, called once the next step is known (`"signIn"` or `"signUp"`), such as to solve a CAPTCHA before the person submits.
|
|
1600
1617
|
|
|
1601
1618
|
`create-app --auth` generates the screen, the account actions in the app bar, the Edge Function that erases accounts and the email templates.
|
|
1602
1619
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.25.0",
|
|
4
4
|
"description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"contributors": [
|
package/src/auth/index.js
CHANGED
|
@@ -21,10 +21,20 @@ const CODE = /^\d{6,10}$/
|
|
|
21
21
|
* `"invalidCredentials"`, `"accountExists"`, `"rateLimited"`, `"captcha"`,
|
|
22
22
|
* `"network"` or `"unknown"`.
|
|
23
23
|
*
|
|
24
|
+
* Whoever signs in, by password, code, link or password reset, the entity
|
|
25
|
+
* gets `#id:signedIn` with `{ user, isNew }`, `isNew` for an account just
|
|
26
|
+
* created. Give the type a `signedIn` handler to move on, such as to an
|
|
27
|
+
* onboarding for new accounts.
|
|
28
|
+
*
|
|
29
|
+
* `linkSubmit({ tokenHash, purpose })` signs in with the link of an email
|
|
30
|
+
* instead of its code, for a page the link opens.
|
|
31
|
+
*
|
|
24
32
|
* The `adapter` talks to the backend: `signIn`, `signUp`, `sendCode`,
|
|
25
|
-
* `verifyCode`, `
|
|
26
|
-
* `eraseAccount`, `errorCode`, and optionally
|
|
27
|
-
* straight to the password or the new account
|
|
33
|
+
* `verifyCode`, `verifyLink`, `resendCode`, `sendResetCode`,
|
|
34
|
+
* `updatePassword`, `signOut`, `eraseAccount`, `errorCode`, and optionally
|
|
35
|
+
* `accountExists`, which skips straight to the password or the new account,
|
|
36
|
+
* and `prepare(action)`, called as soon as the next step is known, such as
|
|
37
|
+
* to solve a CAPTCHA before the person submits. `supabaseAuth` from
|
|
28
38
|
* `@inglorious/supabase` is one.
|
|
29
39
|
*
|
|
30
40
|
* @param {Object} config
|
|
@@ -77,7 +87,11 @@ export function createAuth({
|
|
|
77
87
|
|
|
78
88
|
...request("emailCheck", {
|
|
79
89
|
async run(email) {
|
|
80
|
-
|
|
90
|
+
const exists = adapter.accountExists
|
|
91
|
+
? await adapter.accountExists(email)
|
|
92
|
+
: true
|
|
93
|
+
adapter.prepare?.(exists ? "signIn" : "signUp")
|
|
94
|
+
return exists
|
|
81
95
|
},
|
|
82
96
|
success(entity, exists) {
|
|
83
97
|
entity.step = exists ? "password" : "signUp"
|
|
@@ -104,9 +118,8 @@ export function createAuth({
|
|
|
104
118
|
|
|
105
119
|
...request("signIn", {
|
|
106
120
|
run: ({ email, password }) => adapter.signIn(email, password),
|
|
107
|
-
success(entity, user) {
|
|
108
|
-
entity
|
|
109
|
-
reset(entity)
|
|
121
|
+
success(entity, user, api) {
|
|
122
|
+
signedIn(entity, { user, isNew: false }, api)
|
|
110
123
|
},
|
|
111
124
|
error(entity, code) {
|
|
112
125
|
if (code !== "emailNotConfirmed") return
|
|
@@ -129,10 +142,9 @@ export function createAuth({
|
|
|
129
142
|
|
|
130
143
|
...request("signUp", {
|
|
131
144
|
run: ({ email, password, data }) => adapter.signUp(email, password, data),
|
|
132
|
-
success(entity, user) {
|
|
145
|
+
success(entity, user, api) {
|
|
133
146
|
if (user) {
|
|
134
|
-
entity
|
|
135
|
-
reset(entity)
|
|
147
|
+
signedIn(entity, { user, isNew: true }, api)
|
|
136
148
|
return
|
|
137
149
|
}
|
|
138
150
|
showCode(entity, "signUp")
|
|
@@ -167,9 +179,22 @@ export function createAuth({
|
|
|
167
179
|
...request("codeVerify", {
|
|
168
180
|
run: ({ email, code, purpose }) =>
|
|
169
181
|
adapter.verifyCode(email, code, purpose),
|
|
170
|
-
success(entity, user) {
|
|
171
|
-
entity
|
|
172
|
-
|
|
182
|
+
success(entity, user, api) {
|
|
183
|
+
signedIn(entity, { user, isNew: entity.codeFor === "signUp" }, api)
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
|
|
187
|
+
linkSubmit(entity, { tokenHash, purpose = "signUp" } = {}, api) {
|
|
188
|
+
api.notify(`#${entity.id}:linkVerify`, { tokenHash, purpose })
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
...request("linkVerify", {
|
|
192
|
+
async run({ tokenHash, purpose }) {
|
|
193
|
+
const user = await adapter.verifyLink(tokenHash, purpose)
|
|
194
|
+
return { user, isNew: purpose === "signUp" }
|
|
195
|
+
},
|
|
196
|
+
success(entity, signIn, api) {
|
|
197
|
+
signedIn(entity, signIn, api)
|
|
173
198
|
},
|
|
174
199
|
}),
|
|
175
200
|
|
|
@@ -223,9 +248,8 @@ export function createAuth({
|
|
|
223
248
|
await adapter.verifyCode(email, code, "reset")
|
|
224
249
|
return adapter.updatePassword(password)
|
|
225
250
|
},
|
|
226
|
-
success(entity, user) {
|
|
227
|
-
entity
|
|
228
|
-
reset(entity)
|
|
251
|
+
success(entity, user, api) {
|
|
252
|
+
signedIn(entity, { user, isNew: false }, api)
|
|
229
253
|
},
|
|
230
254
|
}),
|
|
231
255
|
|
|
@@ -277,6 +301,12 @@ export function watchAuth(adapter, store, { id = DEFAULT_ID } = {}) {
|
|
|
277
301
|
)
|
|
278
302
|
}
|
|
279
303
|
|
|
304
|
+
function signedIn(entity, { user, isNew }, api) {
|
|
305
|
+
entity.user = user
|
|
306
|
+
reset(entity)
|
|
307
|
+
api.notify(`#${entity.id}:signedIn`, { user, isNew })
|
|
308
|
+
}
|
|
309
|
+
|
|
280
310
|
function reset(entity, email = "") {
|
|
281
311
|
entity.step = "email"
|
|
282
312
|
entity.email = email
|
|
@@ -3,14 +3,11 @@ import { debounce } from "@inglorious/utils/functions"
|
|
|
3
3
|
/**
|
|
4
4
|
* Wraps selected event handlers of a type with debouncing, scoped per-entity.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* debounced callback actually runs, we re-fetch a live entity from the
|
|
12
|
-
* current state via `api.getEntity(entityId)`. Primitives and `payload` are safe to
|
|
13
|
-
* hold onto as-is; only draft/entity references are not.
|
|
6
|
+
* A debounced call fires later, on its own timer, outside any update: the
|
|
7
|
+
* entity the event brought is a draft revoked by then. So the timer notifies
|
|
8
|
+
* `#id:<handler>Debounced`, which runs the original handler inside an update
|
|
9
|
+
* of its own, with a live draft whose changes reach subscribers. An entity
|
|
10
|
+
* destroyed in the meantime gets nothing.
|
|
14
11
|
*
|
|
15
12
|
* @param {number|Object} config - Either a single delay (ms) applied to all
|
|
16
13
|
* handlers listed in `handlerNames`, or a map of handlerName -> delay (ms).
|
|
@@ -45,6 +42,9 @@ export function withDebounce(config, handlerNames) {
|
|
|
45
42
|
const originalHandler = type[handlerName]
|
|
46
43
|
if (!originalHandler) continue
|
|
47
44
|
|
|
45
|
+
const debouncedEvent = `${handlerName}Debounced`
|
|
46
|
+
behavior[debouncedEvent] = originalHandler
|
|
47
|
+
|
|
48
48
|
behavior[handlerName] = (entity, payload, api) => {
|
|
49
49
|
const entityId = entity.id
|
|
50
50
|
|
|
@@ -56,9 +56,7 @@ export function withDebounce(config, handlerNames) {
|
|
|
56
56
|
|
|
57
57
|
if (!perEntity[handlerName]) {
|
|
58
58
|
perEntity[handlerName] = debounce((entityId, payload, api) => {
|
|
59
|
-
|
|
60
|
-
if (!liveEntity) return
|
|
61
|
-
originalHandler(liveEntity, payload, api)
|
|
59
|
+
api.notify(`#${entityId}:${debouncedEvent}`, payload)
|
|
62
60
|
}, delays[handlerName])
|
|
63
61
|
}
|
|
64
62
|
|
|
@@ -9,17 +9,10 @@ import { throttle } from "@inglorious/utils/functions"
|
|
|
9
9
|
* the handler once more after `delay`, using the most recent suppressed
|
|
10
10
|
* call's arguments, so the final state of a burst is never dropped.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* but a trailing call (when `hasTrailing: true`) fires *later*, on its own
|
|
17
|
-
* timer, after that draft is dead. To keep one code path safe for both
|
|
18
|
-
* cases, we never close over `entity` itself across the throttle boundary —
|
|
19
|
-
* only its primitive `id`. Every time the wrapped handler actually runs, we
|
|
20
|
-
* re-fetch a live entity from the current state via `api.getEntity(entityId)`.
|
|
21
|
-
* Primitives and `payload` are safe to hold onto as-is; only draft/entity
|
|
22
|
-
* references are not.
|
|
12
|
+
* Every call that gets through notifies `#id:<handler>Throttled`, which runs
|
|
13
|
+
* the original handler inside an update with a live draft: a trailing call
|
|
14
|
+
* fires on its own timer, outside any update, after the event's draft is
|
|
15
|
+
* revoked. An entity destroyed in the meantime gets nothing.
|
|
23
16
|
*
|
|
24
17
|
* @param {number|Object} config - Either a single delay (ms) applied to all
|
|
25
18
|
* handlers listed in `handlerNames`, or a map of handlerName -> delay (ms).
|
|
@@ -63,6 +56,9 @@ export function withThrottle(
|
|
|
63
56
|
const originalHandler = type[handlerName]
|
|
64
57
|
if (!originalHandler) continue
|
|
65
58
|
|
|
59
|
+
const throttledEvent = `${handlerName}Throttled`
|
|
60
|
+
behavior[throttledEvent] = originalHandler
|
|
61
|
+
|
|
66
62
|
behavior[handlerName] = (entity, payload, api) => {
|
|
67
63
|
const entityId = entity.id
|
|
68
64
|
|
|
@@ -75,9 +71,7 @@ export function withThrottle(
|
|
|
75
71
|
if (!perEntity[handlerName]) {
|
|
76
72
|
perEntity[handlerName] = throttle(
|
|
77
73
|
(entityId, payload, api) => {
|
|
78
|
-
|
|
79
|
-
if (!liveEntity) return
|
|
80
|
-
originalHandler(liveEntity, payload, api)
|
|
74
|
+
api.notify(`#${entityId}:${throttledEvent}`, payload)
|
|
81
75
|
},
|
|
82
76
|
delays[handlerName],
|
|
83
77
|
{ hasTrailing },
|
package/types/auth.d.ts
CHANGED
|
@@ -46,6 +46,8 @@ export interface AuthAdapter {
|
|
|
46
46
|
code: string,
|
|
47
47
|
purpose: AuthCodePurpose,
|
|
48
48
|
): Promise<AuthUser>
|
|
49
|
+
/** Signs in with the token of an email's link instead of its code. */
|
|
50
|
+
verifyLink(tokenHash: string, purpose: AuthCodePurpose): Promise<AuthUser>
|
|
49
51
|
resendCode(email: string, purpose: AuthCodePurpose): Promise<void>
|
|
50
52
|
sendResetCode(email: string): Promise<void>
|
|
51
53
|
/** Sets the signed-in user's new password. */
|
|
@@ -55,6 +57,15 @@ export interface AuthAdapter {
|
|
|
55
57
|
eraseAccount(): Promise<void>
|
|
56
58
|
/** The key for a failure. */
|
|
57
59
|
errorCode?(error: unknown): AuthError
|
|
60
|
+
/** Called as soon as the next step is known, such as to solve a CAPTCHA early. */
|
|
61
|
+
prepare?(action: "signIn" | "signUp"): void
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What `#id:signedIn` carries, whoever signed in and however. */
|
|
65
|
+
export interface AuthSignIn {
|
|
66
|
+
user: AuthUser
|
|
67
|
+
/** The account was just created. */
|
|
68
|
+
isNew: boolean
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
export interface AuthConfig {
|
|
@@ -103,6 +114,7 @@ export type AuthType = {
|
|
|
103
114
|
codeRequest: Handler
|
|
104
115
|
codeSubmit: Handler<string>
|
|
105
116
|
codeResend: Handler
|
|
117
|
+
linkSubmit: Handler<{ tokenHash: string; purpose?: AuthCodePurpose }>
|
|
106
118
|
resetRequest: Handler
|
|
107
119
|
resetSubmit: Handler<{ code: string; password: string }>
|
|
108
120
|
back: Handler
|