@bbthorson/atproto-cf-auth 0.4.0 → 0.5.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 +158 -11
- package/dist/client.d.ts +16 -1
- package/dist/client.js +25 -2
- package/dist/client.js.map +1 -1
- package/dist/controller.d.ts +10 -0
- package/dist/controller.js +19 -4
- package/dist/controller.js.map +1 -1
- package/dist/element.css +29 -0
- package/dist/element.js +40 -4
- package/dist/element.js.map +1 -1
- package/dist/server/auth.d.ts +141 -6
- package/dist/server/auth.js +193 -59
- package/dist/server/auth.js.map +1 -1
- package/dist/server/handoff.d.ts +1 -1
- package/dist/server/handoff.js +1 -1
- package/dist/server/index.d.ts +3 -2
- package/dist/server/index.js +3 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/lock.d.ts +1 -1
- package/dist/server/stores/postgres.d.ts +30 -0
- package/dist/server/stores/postgres.js +54 -0
- package/dist/server/stores/postgres.js.map +1 -0
- package/dist/shared.d.ts +7 -1
- package/dist/shared.js +25 -2
- package/dist/shared.js.map +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -11,13 +11,13 @@ Worker.
|
|
|
11
11
|
## Server
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import {
|
|
14
|
+
import { createAtprotoAuth, d1Store } from "@bbthorson/atproto-cf-auth/server";
|
|
15
15
|
|
|
16
|
-
let auth: ReturnType<typeof
|
|
16
|
+
let auth: ReturnType<typeof createAtprotoAuth> | undefined;
|
|
17
17
|
|
|
18
18
|
export default {
|
|
19
19
|
async fetch(request: Request, env: Env) {
|
|
20
|
-
auth ??=
|
|
20
|
+
auth ??= createAtprotoAuth({
|
|
21
21
|
appName: "Bardcast",
|
|
22
22
|
store: d1Store(env.DB), // or kvStore(env.KV)
|
|
23
23
|
secret: env.SESSION_SECRET, // Worker secret, 32+ random characters
|
|
@@ -43,7 +43,9 @@ With Astro: call `auth.handle(context.request)` from middleware or from an
|
|
|
43
43
|
`conditionalScopes`, `privateKeys` (see [Confidential client](#confidential-client)),
|
|
44
44
|
`origin` (see [Outside a request](#outside-a-request)), `sessionTtlSeconds`
|
|
45
45
|
(default 30 days), `cookieName`, `metadata` (logo, terms and privacy URLs for the
|
|
46
|
-
consent screen), `
|
|
46
|
+
consent screen), `mode`, `callbackPaths`, `onSignIn` and `onSignInError` (see
|
|
47
|
+
[Identity only](#identity-only)), `signUpServers` (see [Sign-up](#sign-up)),
|
|
48
|
+
`handleResolver` and `requestLock` (see [Lock](#lock)).
|
|
47
49
|
|
|
48
50
|
**Identity lookups on Workers.** Workers refuse `redirect: "error"`, in `fetch()`
|
|
49
51
|
and in `new Request()`, and AT-Proto's stock resolvers ask for it when they load
|
|
@@ -59,6 +61,111 @@ then `new Agent(session)` from `@atproto/api`. Tokens refresh on demand. Before
|
|
|
59
61
|
you do this from concurrent requests (two `fetch`es from one page is enough),
|
|
60
62
|
add the lock below.
|
|
61
63
|
|
|
64
|
+
### Identity only
|
|
65
|
+
|
|
66
|
+
Apps that already have sessions of their own often need only one thing from
|
|
67
|
+
AT-Proto: proof that this person controls this DID. Set `mode: "identity"` and
|
|
68
|
+
the library proves it, then hands over. It keeps nothing: it sets no session
|
|
69
|
+
cookie, serves no `/session` or `/logout`, and revokes the OAuth tokens as soon
|
|
70
|
+
as the sign-in is verified.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
const auth = createAtprotoAuth({
|
|
74
|
+
appName: "Inbox",
|
|
75
|
+
store: postgresStore((text, params) => sql.query(text, params)),
|
|
76
|
+
secret: env.AUTH_SECRET,
|
|
77
|
+
basePath: "/api/v1/atproto",
|
|
78
|
+
mode: "identity",
|
|
79
|
+
async onSignIn({ user, flow }) {
|
|
80
|
+
const account = await findOrCreateAccount(user.did, user.handle);
|
|
81
|
+
if (account.disabled) return redirectTo("/login?error=account_disabled");
|
|
82
|
+
return startAppSession(account, flow.returnTo ?? "/"); // a Response that sets your cookie
|
|
83
|
+
},
|
|
84
|
+
onSignInError: ({ code, flow }) => redirectTo(`/login?error=${code}`),
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`onSignIn` (in either mode) may return a `Response` to answer the callback
|
|
89
|
+
itself; in the default `"session"` mode the library's session cookie is added to
|
|
90
|
+
it. A throw fails the sign-in as `callback_failed`. `onSignInError` gets every
|
|
91
|
+
failed callback (`access_denied`, `browser_mismatch`, `callback_failed`) with the
|
|
92
|
+
flow when it could be read, and may return a `Response` too; otherwise the
|
|
93
|
+
browser goes back with `?auth_error=<code>`.
|
|
94
|
+
|
|
95
|
+
### Starting a sign-in from your own route
|
|
96
|
+
|
|
97
|
+
`POST {basePath}/login` covers the common case. For your own API shape, or to
|
|
98
|
+
carry something through the sign-in, call `authorize`:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
app.post("/api/v1/sign-in", async (c) => {
|
|
102
|
+
const { handle } = await c.req.json();
|
|
103
|
+
const result = await auth.authorize(c.req.raw, {
|
|
104
|
+
handle,
|
|
105
|
+
returnTo: "/inbox",
|
|
106
|
+
context: { flow: "creator" }, // handed back as `flow.context`
|
|
107
|
+
});
|
|
108
|
+
if (!result.ok) return c.json({ error: result.error }, result.status);
|
|
109
|
+
const res = c.json({ url: result.url });
|
|
110
|
+
for (const cookie of result.cookies) res.headers.append("set-cookie", cookie);
|
|
111
|
+
return res;
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`context` is any JSON value. It is kept with the OAuth state in your store,
|
|
116
|
+
sealed, and never travels through the browser or the authorization server, so it
|
|
117
|
+
may hold a secret. Keep it small.
|
|
118
|
+
|
|
119
|
+
**Callback paths.** List further paths in `callbackPaths` (`["/callback/mobile"]`)
|
|
120
|
+
and pick one per sign-in with `callbackPath`. Each is published as a redirect URI
|
|
121
|
+
and answered by `handle()`; a sign-in must come back to the path it was sent to.
|
|
122
|
+
Use one where flows need telling apart before their state is read, such as a
|
|
123
|
+
path a native app claims as a universal link.
|
|
124
|
+
|
|
125
|
+
**Unbound flows.** Every sign-in is tied to the browser that started it by a
|
|
126
|
+
nonce cookie, which is what stops login CSRF: someone finishing their own
|
|
127
|
+
sign-in in your browser. A native app that hands off to the system browser
|
|
128
|
+
can't keep that cookie. For that case only, `bindToBrowser: false` drops the
|
|
129
|
+
cookie, and the app must bind the flow itself: put a verifier only it holds in
|
|
130
|
+
`context`, and have `onSignIn` hand the result over only to whoever presents it.
|
|
131
|
+
|
|
132
|
+
### Sign-up
|
|
133
|
+
|
|
134
|
+
Someone without an account can create one without leaving your sign-in flow:
|
|
135
|
+
the host's own sign-up page opens, and they come back signed in through the
|
|
136
|
+
same callback as any sign-in. This uses OAuth's `prompt=create`, which an
|
|
137
|
+
authorization server lists in its `prompt_values_supported` when it offers
|
|
138
|
+
sign-up (`https://bsky.social` does).
|
|
139
|
+
|
|
140
|
+
From the browser, `client.signUp()` or the element's `sign-up` attribute (see
|
|
141
|
+
[Browser](#browser)) posts `{ signUp: true }` to `/login`. The server starts at
|
|
142
|
+
the first of `signUpServers` (default `["https://bsky.social"]`); a request may
|
|
143
|
+
name another only if it's on that list, so a browser can't make your Worker
|
|
144
|
+
fetch an arbitrary URL. `signUpServers: []` turns sign-up off on that route.
|
|
145
|
+
|
|
146
|
+
From your own route, pass a server instead of a handle:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
const result = await auth.authorize(request, {
|
|
150
|
+
server: "https://bsky.social",
|
|
151
|
+
prompt: "create",
|
|
152
|
+
returnTo: "/welcome",
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Before anyone is sent away, the server's metadata is checked for `create`; a
|
|
157
|
+
server without it answers `sign_up_unavailable`. `authorize` takes any https
|
|
158
|
+
server, because the app chose it; don't pass one straight from a request.
|
|
159
|
+
|
|
160
|
+
A sign-up asks for `scope` only: `conditionalScopes` are tested against an
|
|
161
|
+
account's PDS, and there isn't one yet.
|
|
162
|
+
|
|
163
|
+
`server` without `prompt: "create"` also works: the host shows its own choice
|
|
164
|
+
between signing in and creating an account, which suits someone who knows their
|
|
165
|
+
host but not their handle. Other prompts pass through for any sign-in:
|
|
166
|
+
`"login"` to sign in again despite an existing session, `"select_account"` to
|
|
167
|
+
pick between accounts.
|
|
168
|
+
|
|
62
169
|
### Scopes
|
|
63
170
|
|
|
64
171
|
`scope` is asked for at every sign-in. To act on someone's PDS, add granular
|
|
@@ -74,7 +181,7 @@ signing that person in.
|
|
|
74
181
|
```ts
|
|
75
182
|
import { pdsServesMethod } from "@bbthorson/atproto-cf-auth/server";
|
|
76
183
|
|
|
77
|
-
|
|
184
|
+
createAtprotoAuth({
|
|
78
185
|
// ...
|
|
79
186
|
scope: "atproto repo:com.example.post",
|
|
80
187
|
conditionalScopes: [
|
|
@@ -105,7 +212,7 @@ named in the metadata. Loopback development stays a public client.
|
|
|
105
212
|
|
|
106
213
|
```ts
|
|
107
214
|
// once: node -e "import('@atproto/jwk-jose').then(async ({JoseKey}) => console.log(JSON.stringify((await JoseKey.generate(['ES256'], 'k1')).privateJwk)))"
|
|
108
|
-
|
|
215
|
+
createAtprotoAuth({ /* ... */ privateKeys: [env.OAUTH_PRIVATE_KEY] });
|
|
109
216
|
```
|
|
110
217
|
|
|
111
218
|
To rotate, put the new key first and keep the old one until its sessions have
|
|
@@ -154,11 +261,11 @@ tokens in time. `durableObjectLock` closes the race: each user's restores queue
|
|
|
154
261
|
at one Durable Object, wherever they run.
|
|
155
262
|
|
|
156
263
|
```ts
|
|
157
|
-
import { AuthLock,
|
|
264
|
+
import { AuthLock, createAtprotoAuth, durableObjectLock } from "@bbthorson/atproto-cf-auth/server";
|
|
158
265
|
|
|
159
266
|
export { AuthLock }; // the Durable Object class, exported from your Worker's entry
|
|
160
267
|
|
|
161
|
-
auth ??=
|
|
268
|
+
auth ??= createAtprotoAuth({
|
|
162
269
|
// ...
|
|
163
270
|
requestLock: durableObjectLock(env.AUTH_LOCK),
|
|
164
271
|
});
|
|
@@ -194,6 +301,7 @@ request at another location may read the old tokens for up to a minute.
|
|
|
194
301
|
| Store | Use when |
|
|
195
302
|
|---|---|
|
|
196
303
|
| `d1Store(env.DB)` | You have D1. Strongly consistent; creates a `bsky_auth` table on first use. |
|
|
304
|
+
| `postgresStore(query)` | You have Postgres: Neon (`(text, params) => sql.query(text, params)`), Hyperdrive or node-postgres (`pool.query`), PGlite. Strongly consistent. Creates a `bsky_auth` table on first use, or pass `createTable: false` and a `table` your migrations own. |
|
|
197
305
|
| `kvStore(env.KV)` | You'd rather not add a database. Eventually consistent, so a sign-in that starts and finishes at different Cloudflare locations can fail. |
|
|
198
306
|
| `memoryStore()` | Tests only. |
|
|
199
307
|
|
|
@@ -206,7 +314,9 @@ as a user. Changing the secret signs everyone out.
|
|
|
206
314
|
- The session cookie is an opaque random id (`HttpOnly`, `SameSite=Lax`, `Secure`
|
|
207
315
|
on https). The DID never sits in a cookie.
|
|
208
316
|
- A short-lived nonce cookie ties each callback to the browser that started it,
|
|
209
|
-
which blocks login CSRF.
|
|
317
|
+
which blocks login CSRF. (Unless a flow opts out with `bindToBrowser: false`;
|
|
318
|
+
see [Unbound flows](#starting-a-sign-in-from-your-own-route).)
|
|
319
|
+
- In `"identity"` mode no OAuth token outlives the callback.
|
|
210
320
|
- `/login` and `/logout` refuse cross-site POSTs.
|
|
211
321
|
- `returnTo` only accepts same-site paths, so it can't become an open redirect.
|
|
212
322
|
- The client_id is derived from the request origin, so one deploy works on
|
|
@@ -226,6 +336,7 @@ import { createAuthClient, takeAuthError, HANDLE_INPUT_ATTRIBUTES } from "@bbtho
|
|
|
226
336
|
const auth = createAuthClient(); // { basePath: "/auth" }
|
|
227
337
|
const user = await auth.getUser(); // null when signed out
|
|
228
338
|
await auth.signIn(handle, { returnTo: "/campaigns" }); // navigates to Bluesky; throws AuthError on failure
|
|
339
|
+
await auth.signUp({ returnTo: "/welcome" }); // someone without an account; see Sign-up
|
|
229
340
|
await auth.signOut();
|
|
230
341
|
|
|
231
342
|
// After a failed redirect, read ?auth_error= once (it's removed from the URL):
|
|
@@ -305,18 +416,54 @@ remembered accounts, and `data-bsky-divider` to show only when there are any.
|
|
|
305
416
|
The form still works with JavaScript off, because it posts to `/auth/login`.
|
|
306
417
|
|
|
307
418
|
Attributes: `base-path`, `return-to`, `label`, `placeholder`, `button-label`,
|
|
308
|
-
`loading-label`, `divider-label`, `typeahead`, `remember-accounts`,
|
|
309
|
-
`ignore-redirect-errors`. Event: `bsky-sign-in:error` with `{ code, message }`.
|
|
419
|
+
`loading-label`, `divider-label`, `typeahead`, `remember-accounts`, `sign-up`
|
|
420
|
+
(with `sign-up-prompt` and `sign-up-label`), `ignore-redirect-errors`. Event: `bsky-sign-in:error` with `{ code, message }`.
|
|
310
421
|
Call `preventDefault()` on it to show the error yourself. The input is an ARIA
|
|
311
422
|
combobox when `typeahead` is on: arrow keys move through suggestions, Enter
|
|
312
423
|
signs in as the highlighted one, Escape closes the list.
|
|
313
424
|
|
|
425
|
+
`sign-up` adds "Don't have an account? Create one" under the form (class
|
|
426
|
+
`bsky-sign-in__sign-up`, button `__sign-up-button`). Bare, it uses the server's
|
|
427
|
+
default sign-up host; `sign-up="https://pds.example"` names one, which must be on
|
|
428
|
+
the server's `signUpServers`. The button is a second submit button, so it works
|
|
429
|
+
without JavaScript as well. In your own markup, give a submit button
|
|
430
|
+
`data-bsky-sign-up name="signUp" value="1"`.
|
|
431
|
+
|
|
314
432
|
`remember-accounts` only shows accounts; remembering one is the call to
|
|
315
433
|
`accounts.remember(user)` above, on a page where you know the sign-in worked.
|
|
316
434
|
|
|
317
435
|
In React, render `<bsky-sign-in>` like any element. Importing the module in a
|
|
318
436
|
client component registers it, and the import is a no-op during SSR.
|
|
319
437
|
|
|
438
|
+
## Upgrading
|
|
439
|
+
|
|
440
|
+
### From 0.4 to 0.5
|
|
441
|
+
|
|
442
|
+
Three breaking changes, each a mechanical edit, and two new error codes:
|
|
443
|
+
|
|
444
|
+
- `createBlueskyAuth` is now `createAtprotoAuth`, and the types `BlueskyAuth`
|
|
445
|
+
and `BlueskyAuthOptions` are `AtprotoAuth` and `AtprotoAuthOptions`. Nothing in
|
|
446
|
+
the library is specific to Bluesky's servers.
|
|
447
|
+
- `onSignIn` takes one object: `onSignIn: (user, request) => …` becomes
|
|
448
|
+
`onSignIn: ({ user, request }) => …`. It also gets `flow`, and may now return a
|
|
449
|
+
`Response`.
|
|
450
|
+
- The default error copy for `invalid_handle` and `authorize_failed` no longer
|
|
451
|
+
says "Bluesky", and `<bsky-sign-in>`'s default label is
|
|
452
|
+
"Bluesky or Atmosphere handle". Apps that show `AUTH_ERROR_MESSAGES` or the
|
|
453
|
+
default label change wording; apps with their own copy see nothing.
|
|
454
|
+
|
|
455
|
+
`AuthErrorCode` gains `invalid_server` and `sign_up_unavailable`. A `switch`
|
|
456
|
+
over it that the compiler checks for exhaustiveness needs the two cases.
|
|
457
|
+
|
|
458
|
+
New, and opt-in: `mode: "identity"`, `authorize()`, `callbackPaths`,
|
|
459
|
+
`bindToBrowser: false`, `onSignInError`, sign-up (`server`, `prompt`,
|
|
460
|
+
`signUpServers`, `client.signUp()`, the element's `sign-up`), and
|
|
461
|
+
`postgresStore`.
|
|
462
|
+
|
|
463
|
+
Unchanged on purpose, so nobody is signed out and no stylesheet breaks: the
|
|
464
|
+
cookie names (`bsky_sid`, `bsky_auth_nonce`), the routes under `basePath`, the
|
|
465
|
+
`<bsky-sign-in>` tag and its class names, and what's in the store.
|
|
466
|
+
|
|
320
467
|
## Develop
|
|
321
468
|
|
|
322
469
|
```bash
|
package/dist/client.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* wrap it in whatever state model they use.
|
|
5
5
|
*/
|
|
6
6
|
import { AuthError, type User } from "./shared.js";
|
|
7
|
-
export { AUTH_ERROR_MESSAGES, AUTH_ERROR_PARAM, AuthError, isAuthErrorCode, isValidHandle, normalizeHandle, type AuthErrorCode, type User, } from "./shared.js";
|
|
7
|
+
export { AUTH_ERROR_MESSAGES, AUTH_ERROR_PARAM, AuthError, isAuthErrorCode, isValidHandle, normalizeHandle, normalizeServer, type AuthErrorCode, type User, } from "./shared.js";
|
|
8
8
|
export { createRecentAccounts, lookupProfile, RECENT_ACCOUNTS_KEY, type AccountInput, type AccountStorage, type ProfileSummary, type RecentAccount, type RecentAccounts, type RecentAccountsOptions, } from "./accounts.js";
|
|
9
9
|
export { searchHandles, type HandleSuggestion } from "./search.js";
|
|
10
10
|
export { createSignInController, type SignInController, type SignInControllerOptions, type SignInState, type SignInStatus, } from "./controller.js";
|
|
@@ -19,6 +19,14 @@ export interface SignInOptions {
|
|
|
19
19
|
/** Same-site path to land on after sign-in. Default: the server's `redirectTo`. */
|
|
20
20
|
returnTo?: string;
|
|
21
21
|
}
|
|
22
|
+
export interface SignUpOptions extends SignInOptions {
|
|
23
|
+
/**
|
|
24
|
+
* The server to create the account on. It must be on the server's
|
|
25
|
+
* `signUpServers` list; default: the first one there (`https://bsky.social`
|
|
26
|
+
* unless the app changed it).
|
|
27
|
+
*/
|
|
28
|
+
server?: string;
|
|
29
|
+
}
|
|
22
30
|
export interface AuthClient {
|
|
23
31
|
/** The signed-in user, or `null`. Throws `AuthError("network_error")` if the server is unreachable. */
|
|
24
32
|
getUser(): Promise<User | null>;
|
|
@@ -28,6 +36,13 @@ export interface AuthClient {
|
|
|
28
36
|
* failure throws an `AuthError` whose `message` is ready to show.
|
|
29
37
|
*/
|
|
30
38
|
signIn(handle: string, options?: SignInOptions): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* For someone without an account: asks the server to start a sign-up
|
|
41
|
+
* (`prompt=create`) and navigates to the host's account-creation page. They
|
|
42
|
+
* come back signed in, through the same callback as a sign-in. Throws an
|
|
43
|
+
* `AuthError` (`sign_up_unavailable`, `invalid_server`, …) on failure.
|
|
44
|
+
*/
|
|
45
|
+
signUp(options?: SignUpOptions): Promise<void>;
|
|
31
46
|
signOut(): Promise<void>;
|
|
32
47
|
}
|
|
33
48
|
export declare function createAuthClient(options?: AuthClientOptions): AuthClient;
|
package/dist/client.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* `fetch` and `location`, so React, Astro, Svelte or a bare `<script>` can
|
|
4
4
|
* wrap it in whatever state model they use.
|
|
5
5
|
*/
|
|
6
|
-
import { AUTH_ERROR_PARAM, AuthError, isAuthErrorCode, isValidHandle, normalizeHandle, } from "./shared.js";
|
|
7
|
-
export { AUTH_ERROR_MESSAGES, AUTH_ERROR_PARAM, AuthError, isAuthErrorCode, isValidHandle, normalizeHandle, } from "./shared.js";
|
|
6
|
+
import { AUTH_ERROR_PARAM, AuthError, isAuthErrorCode, isValidHandle, normalizeHandle, normalizeServer, } from "./shared.js";
|
|
7
|
+
export { AUTH_ERROR_MESSAGES, AUTH_ERROR_PARAM, AuthError, isAuthErrorCode, isValidHandle, normalizeHandle, normalizeServer, } from "./shared.js";
|
|
8
8
|
export { createRecentAccounts, lookupProfile, RECENT_ACCOUNTS_KEY, } from "./accounts.js";
|
|
9
9
|
export { searchHandles } from "./search.js";
|
|
10
10
|
export { createSignInController, } from "./controller.js";
|
|
@@ -44,6 +44,29 @@ export function createAuthClient(options = {}) {
|
|
|
44
44
|
}
|
|
45
45
|
throw new AuthError(isAuthErrorCode(body?.error) ? body.error : res.status === 503 ? "not_configured" : "authorize_failed");
|
|
46
46
|
},
|
|
47
|
+
async signUp(signUpOptions = {}) {
|
|
48
|
+
let server;
|
|
49
|
+
if (signUpOptions.server !== undefined) {
|
|
50
|
+
server = normalizeServer(signUpOptions.server);
|
|
51
|
+
if (!server)
|
|
52
|
+
throw new AuthError("invalid_server");
|
|
53
|
+
}
|
|
54
|
+
const res = await call("/login", {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: { "content-type": "application/json" },
|
|
57
|
+
body: JSON.stringify({
|
|
58
|
+
signUp: true,
|
|
59
|
+
...(server ? { server } : {}),
|
|
60
|
+
...(signUpOptions.returnTo ? { returnTo: signUpOptions.returnTo } : {}),
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
const body = (await res.json().catch(() => null));
|
|
64
|
+
if (res.ok && body?.url) {
|
|
65
|
+
navigate(body.url);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
throw new AuthError(isAuthErrorCode(body?.error) ? body.error : res.status === 503 ? "not_configured" : "authorize_failed");
|
|
69
|
+
},
|
|
47
70
|
async signOut() {
|
|
48
71
|
await call("/logout", { method: "POST" });
|
|
49
72
|
},
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,GAEhB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,GAGhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,mBAAmB,GAOpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAyB,MAAM,aAAa,CAAC;AACnE,OAAO,EACL,sBAAsB,GAKvB,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,EACf,eAAe,GAEhB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,EACf,eAAe,GAGhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,mBAAmB,GAOpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAyB,MAAM,aAAa,CA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AM,KAAK,WAAW;QAAE,OAAO,IAAI,CAAC;IAC/C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC1C,MAAM,IAAI,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACpD,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAC/B,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;IAC1C,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5F,OAAO,IAAI,SAAS,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC;AACzE,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,IAAI,EAAE,QAAQ;IACd,IAAI,EAAE,MAAM;IACZ,YAAY,EAAE,UAAU;IACxB,cAAc,EAAE,MAAM;IACtB,WAAW,EAAE,KAAK;IAClB,UAAU,EAAE,OAAO;IACnB,SAAS,EAAE,KAAK;IAChB,YAAY,EAAE,IAAI;IAClB,WAAW,EAAE,iBAAiB;CACtB,CAAC"}
|
package/dist/controller.d.ts
CHANGED
|
@@ -36,10 +36,18 @@ export interface SignInState {
|
|
|
36
36
|
error: AuthError | null;
|
|
37
37
|
/** The handle or DID a sign-in is under way for, while `loading`. */
|
|
38
38
|
pending: string | null;
|
|
39
|
+
/** What's under way while `loading`: a sign-in, or a sign-up. `null` otherwise. */
|
|
40
|
+
action: "sign-in" | "sign-up" | null;
|
|
39
41
|
}
|
|
40
42
|
export interface SignInControllerOptions {
|
|
41
43
|
/** Start the sign-in for a normalised, valid handle or DID. Navigate away, or throw. */
|
|
42
44
|
signIn: (identifier: string) => Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Start a sign-up for someone without an account (`client.signUp()`, or
|
|
47
|
+
* your own route that calls `authorize` with `prompt: "create"`). Navigate
|
|
48
|
+
* away, or throw. Needed only for `controller.signUp()`.
|
|
49
|
+
*/
|
|
50
|
+
signUp?: () => Promise<void>;
|
|
43
51
|
/** Remembered accounts to offer above the field. */
|
|
44
52
|
accounts?: RecentAccounts | null;
|
|
45
53
|
/**
|
|
@@ -74,6 +82,8 @@ export interface SignInController {
|
|
|
74
82
|
closeSuggestions(): void;
|
|
75
83
|
/** Sign in as the highlighted suggestion if there is one, else as what's typed. */
|
|
76
84
|
submit(): Promise<void>;
|
|
85
|
+
/** Start a sign-up through the `signUp` option. Shares the loading and error state with sign-in. */
|
|
86
|
+
signUp(): Promise<void>;
|
|
77
87
|
/** Sign in as a suggestion or remembered account. */
|
|
78
88
|
choose(account: HandleSuggestion | RecentAccount): Promise<void>;
|
|
79
89
|
/** Drop a remembered account. */
|
package/dist/controller.js
CHANGED
|
@@ -19,6 +19,7 @@ export function createSignInController(options) {
|
|
|
19
19
|
status: "idle",
|
|
20
20
|
error: null,
|
|
21
21
|
pending: null,
|
|
22
|
+
action: null,
|
|
22
23
|
};
|
|
23
24
|
const listeners = new Set();
|
|
24
25
|
let timer;
|
|
@@ -44,7 +45,7 @@ export function createSignInController(options) {
|
|
|
44
45
|
// out of the back/forward cache still showing "Signing in…".
|
|
45
46
|
const onPageShow = (event) => {
|
|
46
47
|
if (event.persisted && state.status === "loading")
|
|
47
|
-
set({ status: "idle", pending: null });
|
|
48
|
+
set({ status: "idle", pending: null, action: null });
|
|
48
49
|
};
|
|
49
50
|
if (typeof window !== "undefined")
|
|
50
51
|
window.addEventListener("pageshow", onPageShow);
|
|
@@ -57,13 +58,13 @@ export function createSignInController(options) {
|
|
|
57
58
|
set({ status: "error", error: new AuthError("invalid_handle"), suggestions: [], activeIndex: -1 });
|
|
58
59
|
return;
|
|
59
60
|
}
|
|
60
|
-
set({ status: "loading", error: null, pending: identifier, suggestions: [], activeIndex: -1 });
|
|
61
|
+
set({ status: "loading", error: null, pending: identifier, action: "sign-in", suggestions: [], activeIndex: -1 });
|
|
61
62
|
try {
|
|
62
63
|
await options.signIn(identifier);
|
|
63
64
|
// Navigation is under way; stay loading until the page goes.
|
|
64
65
|
}
|
|
65
66
|
catch (error) {
|
|
66
|
-
set({ status: "error", error: toError(error), pending: null });
|
|
67
|
+
set({ status: "error", error: toError(error), pending: null, action: null });
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
return {
|
|
@@ -125,6 +126,20 @@ export function createSignInController(options) {
|
|
|
125
126
|
}
|
|
126
127
|
return start(state.query);
|
|
127
128
|
},
|
|
129
|
+
async signUp() {
|
|
130
|
+
if (!options.signUp)
|
|
131
|
+
throw new Error("createSignInController: pass a `signUp` option to use signUp()");
|
|
132
|
+
if (state.status === "loading")
|
|
133
|
+
return;
|
|
134
|
+
cancelSearch();
|
|
135
|
+
set({ status: "loading", error: null, pending: null, action: "sign-up", suggestions: [], activeIndex: -1 });
|
|
136
|
+
try {
|
|
137
|
+
await options.signUp();
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
set({ status: "error", error: toError(error), action: null });
|
|
141
|
+
}
|
|
142
|
+
},
|
|
128
143
|
async choose(account) {
|
|
129
144
|
if (state.status === "loading")
|
|
130
145
|
return;
|
|
@@ -139,7 +154,7 @@ export function createSignInController(options) {
|
|
|
139
154
|
await options.accounts.forget(did);
|
|
140
155
|
},
|
|
141
156
|
setError(error) {
|
|
142
|
-
set(error ? { status: "error", error, pending: null } : { status: "idle", error: null });
|
|
157
|
+
set(error ? { status: "error", error, pending: null, action: null } : { status: "idle", error: null });
|
|
143
158
|
},
|
|
144
159
|
destroy() {
|
|
145
160
|
cancelSearch();
|
package/dist/controller.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,aAAa,EAAyB,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,aAAa,EAAyB,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AA4ExE,MAAM,UAAU,sBAAsB,CAAC,OAAgC;IACrE,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,GAAG,CAAC;IAC7C,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,CAAC,CAAC;IACnD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;IACjC,MAAM,MAAM,GACV,OAAO,OAAO,CAAC,SAAS,KAAK,UAAU;QACrC,CAAC,CAAC,OAAO,CAAC,SAAS;QACnB,CAAC,CAAC,OAAO,CAAC,SAAS;YACjB,CAAC,CAAC,CAAC,KAAa,EAAE,EAAE,MAAM,EAA2B,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;YACjG,CAAC,CAAC,IAAI,CAAC;IACb,MAAM,OAAO,GACX,OAAO,CAAC,OAAO,IAAI,CAAC,CAAC,KAAc,EAAE,EAAE,CAAC,CAAC,KAAK,YAAY,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC;IACpH,MAAM,gBAAgB,GAAG,OAAO,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,CAAC,aAAa,EAAE,CAAC;IAE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AC,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC;gBAC/D,OAAO;YACT,CAAC;YACD,MAAM,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACrC,CAAC;QAED,QAAQ,CAAC,KAAK;YACZ,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACzG,CAAC;QAED,OAAO;YACL,YAAY,EAAE,CAAC;YACf,mBAAmB,EAAE,EAAE,CAAC;YACxB,IAAI,OAAO,MAAM,KAAK,WAAW;gBAAE,MAAM,CAAC,mBAAmB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;YACtF,SAAS,CAAC,KAAK,EAAE,CAAC;YAClB,SAAS,GAAG,IAAI,CAAC;QACnB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/element.css
CHANGED
|
@@ -240,6 +240,35 @@ bsky-sign-in {
|
|
|
240
240
|
color: var(--_error);
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
+
.bsky-sign-in__sign-up {
|
|
244
|
+
margin: 0;
|
|
245
|
+
text-align: center;
|
|
246
|
+
color: var(--_muted);
|
|
247
|
+
font-size: 0.92em;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
.bsky-sign-in__sign-up-button {
|
|
251
|
+
all: unset;
|
|
252
|
+
color: var(--_accent);
|
|
253
|
+
font-weight: 600;
|
|
254
|
+
cursor: pointer;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.bsky-sign-in__sign-up-button:hover {
|
|
258
|
+
text-decoration: underline;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
.bsky-sign-in__sign-up-button:focus-visible {
|
|
262
|
+
outline: 2px solid var(--_accent);
|
|
263
|
+
outline-offset: 2px;
|
|
264
|
+
border-radius: 4px;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
.bsky-sign-in__sign-up-button:disabled {
|
|
268
|
+
opacity: 0.6;
|
|
269
|
+
cursor: progress;
|
|
270
|
+
}
|
|
271
|
+
|
|
243
272
|
.bsky-sign-in__error[hidden],
|
|
244
273
|
.bsky-sign-in__accounts[hidden],
|
|
245
274
|
.bsky-sign-in__divider[hidden],
|
package/dist/element.js
CHANGED
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
* </form>
|
|
25
25
|
* </bsky-sign-in>
|
|
26
26
|
*
|
|
27
|
+
* `sign-up` adds a "Don't have an account? Create one" button, which starts an
|
|
28
|
+
* account on the server's default sign-up host (or the one the attribute
|
|
29
|
+
* names, `sign-up="https://bsky.social"`). In your own markup, give a submit
|
|
30
|
+
* button `data-bsky-sign-up` (and `name="signUp" value="1"` for the no-JS post).
|
|
31
|
+
*
|
|
27
32
|
* Without JavaScript the form still works: it posts to `{base-path}/login`,
|
|
28
33
|
* which redirects to the person's authorization server.
|
|
29
34
|
*
|
|
@@ -77,7 +82,9 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
77
82
|
this.renderDefault();
|
|
78
83
|
this.form = this.querySelector("form");
|
|
79
84
|
this.input = this.querySelector('input[name="handle"]');
|
|
80
|
-
this.button =
|
|
85
|
+
this.button =
|
|
86
|
+
this.form?.querySelector('button:not([type="button"]):not([data-bsky-sign-up]), input[type="submit"]:not([data-bsky-sign-up])') ??
|
|
87
|
+
null;
|
|
81
88
|
this.errorEl = this.querySelector("[data-bsky-error]");
|
|
82
89
|
this.accountsEl = this.querySelector("[data-bsky-accounts]");
|
|
83
90
|
this.dividerEl = this.querySelector("[data-bsky-divider]");
|
|
@@ -120,8 +127,11 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
120
127
|
if (this.dividerEl)
|
|
121
128
|
this.dividerEl.hidden = true;
|
|
122
129
|
const client = createAuthClient({ basePath: this.basePath });
|
|
130
|
+
// `sign-up` alone uses the server's default; `sign-up="https://…"` names one.
|
|
131
|
+
const signUpServer = this.getAttribute("sign-up") || undefined;
|
|
123
132
|
this.controller = createSignInController({
|
|
124
133
|
signIn: (handle) => client.signIn(handle, returnTo ? { returnTo } : {}),
|
|
134
|
+
signUp: () => client.signUp({ ...(signUpServer ? { server: signUpServer } : {}), ...(returnTo ? { returnTo } : {}) }),
|
|
125
135
|
accounts: this.hasAttribute("remember-accounts") && this.accountsEl ? createRecentAccounts() : null,
|
|
126
136
|
typeahead,
|
|
127
137
|
});
|
|
@@ -161,7 +171,7 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
161
171
|
return;
|
|
162
172
|
const label = this.querySelector(`.${C}__label`);
|
|
163
173
|
if (label)
|
|
164
|
-
label.textContent = this.getAttribute("label") ?? "Bluesky handle";
|
|
174
|
+
label.textContent = this.getAttribute("label") ?? "Bluesky or Atmosphere handle";
|
|
165
175
|
if (this.input && this.hasAttribute("placeholder"))
|
|
166
176
|
this.input.placeholder = this.getAttribute("placeholder");
|
|
167
177
|
if (this.button?.classList.contains(`${C}__button`)) {
|
|
@@ -181,7 +191,7 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
181
191
|
const divider = el("p", `${C}__divider`);
|
|
182
192
|
divider.setAttribute("data-bsky-divider", "");
|
|
183
193
|
divider.append(el("span", "", this.getAttribute("divider-label") ?? "or"));
|
|
184
|
-
const label = el("label", `${C}__label`, this.getAttribute("label") ?? "Bluesky handle");
|
|
194
|
+
const label = el("label", `${C}__label`, this.getAttribute("label") ?? "Bluesky or Atmosphere handle");
|
|
185
195
|
label.htmlFor = id;
|
|
186
196
|
const field = el("div", `${C}__field`);
|
|
187
197
|
const input = el("input", `${C}__input`);
|
|
@@ -203,6 +213,27 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
203
213
|
error.setAttribute("role", "alert");
|
|
204
214
|
error.hidden = true;
|
|
205
215
|
form.append(accounts, divider, label, field, button, error);
|
|
216
|
+
// Someone without an account. A submit button of its own, so it works
|
|
217
|
+
// without JavaScript too: the form posts `signUp=1` to /login.
|
|
218
|
+
if (this.hasAttribute("sign-up")) {
|
|
219
|
+
const row = el("p", `${C}__sign-up`, `${this.getAttribute("sign-up-prompt") ?? "Don't have an account?"} `);
|
|
220
|
+
const signUp = el("button", `${C}__sign-up-button`, this.getAttribute("sign-up-label") ?? "Create one");
|
|
221
|
+
signUp.type = "submit";
|
|
222
|
+
signUp.name = "signUp";
|
|
223
|
+
signUp.value = "1";
|
|
224
|
+
signUp.formNoValidate = true;
|
|
225
|
+
signUp.setAttribute("data-bsky-sign-up", "");
|
|
226
|
+
row.append(signUp);
|
|
227
|
+
const server = this.getAttribute("sign-up");
|
|
228
|
+
if (server) {
|
|
229
|
+
const hidden = el("input", "");
|
|
230
|
+
hidden.type = "hidden";
|
|
231
|
+
hidden.name = "server";
|
|
232
|
+
hidden.value = server;
|
|
233
|
+
row.append(hidden);
|
|
234
|
+
}
|
|
235
|
+
form.append(row);
|
|
236
|
+
}
|
|
206
237
|
this.append(form);
|
|
207
238
|
}
|
|
208
239
|
/** Bring the DOM in line with the controller's state, touching only what changed. */
|
|
@@ -228,7 +259,7 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
228
259
|
this.toggleAttribute("aria-busy", loading);
|
|
229
260
|
if (this.input)
|
|
230
261
|
this.input.readOnly = loading;
|
|
231
|
-
for (const b of this.querySelectorAll(`[data-bsky-choose], [data-bsky-forget]`))
|
|
262
|
+
for (const b of this.querySelectorAll(`[data-bsky-choose], [data-bsky-forget], [data-bsky-sign-up]`))
|
|
232
263
|
b.disabled = loading;
|
|
233
264
|
if (this.button) {
|
|
234
265
|
this.button.disabled = loading;
|
|
@@ -294,6 +325,11 @@ export function defineSignInElement(tagName = "bsky-sign-in") {
|
|
|
294
325
|
if (!this.controller)
|
|
295
326
|
return;
|
|
296
327
|
// The field is the source of truth: autofill doesn't always fire `input`.
|
|
328
|
+
const submitter = event.submitter;
|
|
329
|
+
if (submitter?.hasAttribute("data-bsky-sign-up")) {
|
|
330
|
+
void this.controller.signUp();
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
297
333
|
if (this.input && this.input.value !== this.controller.getState().query)
|
|
298
334
|
this.controller.setQuery(this.input.value);
|
|
299
335
|
void this.controller.submit();
|