@inglorious/web 6.24.1 → 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 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.24.1",
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`, `resendCode`, `sendResetCode`, `updatePassword`, `signOut`,
26
- * `eraseAccount`, `errorCode`, and optionally `accountExists`, which skips
27
- * straight to the password or the new account. `supabaseAuth` from
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
- return adapter.accountExists ? await adapter.accountExists(email) : true
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.user = user
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.user = user
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.user = user
172
- reset(entity)
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.user = user
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
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