|
| 1 | +# AT Astro |
| 2 | + |
| 3 | +An integration to build AT Protocol AppViews using Astro. This package implements the OAuth flow with Astro and exposes helpful |
| 4 | +utilities to get an authenticated ATProto client and manage sign in and sign out. |
| 5 | + |
| 6 | +## Note on Port |
| 7 | + |
| 8 | +By default, Astro exposes the dev server using port 3000; however, OAuth redirects require a non-localhost URL, so I recommend running Astro with `astro dev --host 127.0.0.1`. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +npm i at-astro |
| 14 | +# or |
| 15 | +yarn add at-astro |
| 16 | +# or |
| 17 | +pnpm i at-astro |
| 18 | +# or |
| 19 | +bun i at-astro |
| 20 | +``` |
| 21 | + |
| 22 | +Then, in your `astro.config.ts`: |
| 23 | + |
| 24 | +```ts |
| 25 | +// Add this import |
| 26 | +import atproto from "at-astro" |
| 27 | + |
| 28 | +export default defineConfig({ |
| 29 | + // Ensure site is defined |
| 30 | + site: "https://pixl.pics/", |
| 31 | + // Ensure you have an adapter set up (this example uses Cloudflare) |
| 32 | + adapter: cloudflare(), |
| 33 | + |
| 34 | + integrations: [ |
| 35 | + // Add this integration to your Astro config |
| 36 | + atproto({ |
| 37 | + // Add the OAuth scopes your app needs to access. Typically this is your app's lexicon namespace. |
| 38 | + scopes: ["repo:com.myapp.mylexicon"], |
| 39 | + }), |
| 40 | + ], |
| 41 | +}) |
| 42 | +``` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### Routes |
| 47 | + |
| 48 | +This package adds the following routes: |
| 49 | +- `/oauth-client-metadata.json` - OAuth2 client metadata |
| 50 | +- `/oauth/login` - OAuth2 login route |
| 51 | +- `/oauth/callback` - OAuth2 callback route |
| 52 | +- `/oauth/logout` - Sign out route |
| 53 | + |
| 54 | +To add sign in, create a sign in page and add a standard HTML form that submits to `/oauth/login`: |
| 55 | +```html |
| 56 | +<form |
| 57 | + action="/oauth/login" |
| 58 | + method="post" |
| 59 | +> |
| 60 | + <label> |
| 61 | + Handle |
| 62 | + <input |
| 63 | + name="handle" |
| 64 | + placeholder="you.bsky.social" |
| 65 | + required |
| 66 | + /> |
| 67 | + </label> |
| 68 | + <button> |
| 69 | + Sign in |
| 70 | + </button> |
| 71 | +</form> |
| 72 | +``` |
| 73 | + |
| 74 | +Sign out is just as simple: |
| 75 | +```html |
| 76 | +<form action="/oauth/logout" method="post"> |
| 77 | + <button> |
| 78 | + Sign out |
| 79 | + </button> |
| 80 | +</form> |
| 81 | +``` |
| 82 | + |
| 83 | +It supports GET requests as well, so an alternative would be: |
| 84 | +```html |
| 85 | +<a href="/oauth/logout">Sign out</a> |
| 86 | +``` |
| 87 | + |
| 88 | +### Client |
| 89 | + |
| 90 | +After OAuth, you will have access to an authenticated ATProto client using the `getClient` function. |
0 commit comments