Skip to content

Commit 98b488b

Browse files
essential-randomnesshaetaehaetae-bit
authored
Haetae's Vengeance PR (#10)
* add additional example with atproto creating records * add class customization to login component in authproto * turn status page into component * modify readme and add screenshots * Update README.md with links * go a bit overboard * better class props * get records from fujocoded's PDS directly when logged out * add avatar example * some todos in the readme * small bits of changes * also export helpers * use library methods in example * Get examples folder tidied up and make plan for examples --------- Co-authored-by: haetae <haetae@32bit.cafe> Co-authored-by: haetae <haetae@disroot.org>
1 parent 99b8d4f commit 98b488b

35 files changed

+6099
-46
lines changed

astro-authproto/README.md

Lines changed: 217 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,228 @@
1-
# @fujocoded/authproto
1+
# `@fujocoded/authproto`
22

3-
Astro integration to easily authenticate your site visitors using ATproto. For
4-
Bluesky and beyond.
3+
<!-- banner -->
54

6-
> [!WARNING] This is not ready for prime time, only use it if you know what
7-
> you're doing. Come back soon for instructions for real production usage!
5+
ATproto authentication for your Astro site. Free and Easy™!
86

9-
## Tidbits
7+
<!-- badges -->
108

11-
- [ ] Remind folks they need a session configured
12-
- [ ] Remind them not to commit the keys
9+
## What is `@fujocoded/authproto`?
1310

14-
## Current TODOs
11+
`@fujocoded/authproto` allows your visitors to log into your website using their
12+
account on any ATproto services (like Bluesky) or in technical terms their PDS!
1513

16-
- [x] Turn into a proper package
17-
- [x] Check whether you can do unstorage for saving data
18-
- [ ] Write a README
19-
- [ ] Comment the whole Auth files
20-
- [ ] Improve config and make it make sense
21-
- [x] Figure out how to turn AstroDB into an option
22-
- [x] Add option to redirect logged in/logged out user to a chosen page
23-
- [ ] Explain how to intercept logged out/logged in events so you can do things
24-
- [ ] Figure out how to make types correctly signal Astro.locals.loggedInUser can be null
25-
- [x] Fix the state bits in the oauth login route
26-
- [x] Figure out what's up with weird amount of implementations in lib/
27-
- [x] Clean up client-metadata duplication
28-
- [ ] Rename genericData to writeData
29-
- [ ] Add lists of current permissions
14+
Under the hood, `@fujocoded/authproto` adds OAuth authentication to your site
15+
through
16+
[`@atproto/oauth-client-node`](https://www.npmjs.com/package/@atproto/oauth-client-node),
17+
then uses Astro's session adapters (based on
18+
[`@unjs/unstorage`](https://github.com/unjs/unstorage)) to store your visitor's
19+
credentials.
3020

31-
// Tip!
32-
Want to see what gets saved by the storage? Use
21+
<!-- screenshot of oauth x'd out -->
22+
<!-- screenshot of fujocoded thumbsup -->
3323

24+
## What's included in `@fujocoded/authproto`?
25+
26+
In this package, you'll find:
27+
28+
- `@fujocoded/authproto`, an Astro integration that:
29+
- Adds the authentication routes you need
30+
- Lets you easily access the DID and handle of a logged in user, if any
31+
- `@fujocoded/authproto/components`, which includes:
32+
- A basic login/logout component to get started quickly
33+
- `@fujocoded/authproto/helpers`,
34+
- `getPdsAgent` etc.?
35+
- `friendsOnly` function (or similar)
36+
37+
## What can you do with `@fujocoded/authproto`?
38+
39+
- **Let visitors log in to your site** with their ATproto account (such as a Bluesky
40+
account). With this, you can:
41+
- Build private, friends-only spaces and pages
42+
- Gate certain content from the public
43+
- **Read data from other ATproto services**, including [Bluesky](https://bsky.app/),
44+
[Streamplace](https://stream.place/), [Teal.FM](https://teal.fm/), and more!
45+
Among the many uses, you can:
46+
- Show a list of your favorite [Bluesky](https://bsky.app/) posts, or
47+
embed [Leaflet publications](https://leaflet.pub/)
48+
- Tell everyone about the music you love by listing
49+
your [Rocksky](https://rocksky.app/) stats
50+
- **Create, update, and delete data** on ATproto services, both existing ones or
51+
_your own_!
52+
- Post on [Bluesky](https://bsky.app/) from your own website
53+
- Build [guestbooks](https://github.com/FujoWebDev/lexicon-guestbook/) that
54+
others on ATproto can interact with
55+
- Make your _own_ ATproto app that shares data with the rest of the network
56+
57+
<!-- replace this with a fancier display -->
58+
59+
## Built with Authproto
60+
61+
- [Guestbook lexicons](https://github.com/FujoWebDev/lexicon-guestbook/)
62+
- [Fanfic archive](https://github.com/haetae-bit/fanfic-atproto)
63+
64+
# Getting started
65+
66+
## Pre-requisites
67+
68+
- Node
69+
- NPM/pnpm/yarn
70+
- Terminal
71+
72+
> [!IMPORTANT]
73+
> `deno` requires a workaround due to a CJS/ESM import issue within
74+
> `@atproto/oauth-client-node`.
75+
76+
// TODO: we can move this in a details tab
77+
78+
Requires some familiarity with Astro, but if you want to jump in head first:
79+
80+
1. Install Astro by [following their official
81+
tutorial](https://docs.astro.build/en/install-and-setup/#install-from-the-cli-wizard).
82+
Once you do, [set your Astro site to server
83+
mode](https://docs.astro.build/en/guides/on-demand-rendering/#server-mode).
84+
2. Install [any of the
85+
adapters](https://docs.astro.build/en/guides/on-demand-rendering/#server-adapters).
86+
87+
- You can start out with
88+
[Node](https://docs.astro.build/en/guides/integrations-guide/node/), since
89+
that's a widely supported runtime.
90+
91+
3. Run the following command:
92+
93+
```bash
94+
npm install @fujocoded/authproto
3495
```
35-
driver: {
36-
name: "fs-lite",
37-
options: {
38-
base: process.cwd() + "/test/",
39-
},
40-
},
96+
97+
```bash
98+
npx astro add @fujocoded/authproto
4199
```
42100

43-
Make sure not to commit!
101+
4. Add the integration to your `astro.config.mjs` file, like this:
102+
103+
// TODO: add a note that this requires a server and an adapter that supports //
104+
some type of storage...? I'm unsure how it works on e.g. netlify for the //
105+
various session handlers
106+
107+
// TODO: we might also want to make sure people do not set certain adapters //
108+
or even better just disallow the ones they shouldn't.
109+
110+
```typescript
111+
import { defineConfig } from "astro/config";
112+
+ import node from "@astrojs/node";
113+
+ import authproto from "@fujocoded/authproto";
114+
115+
export default defineConfig({
116+
output: "server",
117+
+ adapter: node({ mode: "standalone" }), // ... or whichever adapter you're using!
118+
+ integrations: [
119+
+ authproto({
120+
+ // config options here
121+
+ }),
122+
+ ],
123+
});
124+
```
125+
126+
> [!TIP] You can take a look at all the [possible configuration options
127+
> below](#configuration-options).
128+
129+
5. Add the `<Login />` component to your site, like this:
130+
131+
```
132+
// src/pages/index.astro
133+
---
134+
import { Login } from "@fujocoded/authproto/components";
135+
---
136+
137+
<Login />
138+
```
139+
140+
> [!TIP] You might run into a naming collision issue if you also have a page
141+
> named `login`. You can fix this by replacing `{ Login }` with `{ Login as
142+
LoginComponent }`.
143+
144+
It'll look like a plain form:
145+
146+
<!-- screenshot -->
147+
148+
To make a page only visible to logged in users:
149+
150+
```ts
151+
// src/pages/secret.astro
152+
---
153+
const loggedInUser = Astro.locals.loggedInUser;
154+
155+
// if they're not logged in, send them back to a login page
156+
if (!loggedInUser) {
157+
return Astro.redirect("/login");
158+
}
159+
---
160+
161+
<h1>Secret</h1>
162+
<p>This is a secret page that only authenticated users can see!</p>
163+
```
164+
165+
<!-- screenshots here -->
166+
167+
... And you've got authentication working on your Astro site!
168+
169+
# Okay how do I _actually_ do stuff with this?
170+
171+
Check out the example sites included:
172+
173+
- [`__example__`](./__example__) shows you how to set up a login flow.
174+
- `__example_status__` has some examples of creating new records on a PDS.
175+
176+
# Configuration options
177+
178+
- `applicationName`, required. The name of your application. For example, you
179+
can set this to `"My personal guestbook"`!
180+
- `applicationDomain`, required. It should be a domain that your site is on, or
181+
you can just put in `"localhost:4321"` for now.
182+
- `defaultDevUser`, optional. The default handle that gets filled out in the
183+
`<Login />` component during development.
184+
- `driver`, optional. The driver used to store data about OAuth sessions. This
185+
takes Astro's [session driver
186+
options](https://docs.astro.build/en/reference/configuration-reference/#sessiondriver).
187+
You can also set this with `name: "astro:db"` to utilize [Astro's DB
188+
integration](https://docs.astro.build/en/guides/integrations-guide/db/) for
189+
OAuth sessions. This will set up tables for sessions in your database.
190+
- NOTE: The default driver is `memory`. This is fine for development, but it's
191+
recommended that you switch to a more reliable solution for production.
192+
- NOTE: Using either `localStorage` or `sessionStorage` will result in a
193+
["Session storage could not be initialized."
194+
error](https://docs.astro.build/en/reference/errors/session-storage-init-error/)
195+
(and is considered insecure for handling sessions anyway). Consider other
196+
options, like a database.
197+
- `scopes`, optional. By default, only the `"atproto"` scope is added. This
198+
scope is included with any other scope that's enabled. See [ATproto's
199+
documentation for OAuth
200+
scopes](https://atproto.com/specs/oauth#authorization-scopes).
201+
- `email`: boolean, optional. Only used to identify you by email. Does nothing
202+
to a PDS.
203+
- `genericData`: boolean, optional. Allows you to read/write data to a user's
204+
PDS, but does not access BlueSky direct messages.
205+
- `directMessages`: boolean, optional. Allows you to access BlueSky direct
206+
messages for a user's account. Requires `genericData` to be enabled.
207+
- `additionalScopes`: array, optional. This is used in case you need to expand
208+
permissions to include other services. This should be an array of strings,
209+
like this: `["scope1", "scope2"]`
210+
211+
# Support Us
212+
213+
You can check out more of our plugins here:
214+
215+
- [Socials
216+
plugin](https://github.com/FujoWebDev/fujocoded-plugins/tree/main/zod-transform-socials)
217+
- [Alt text files
218+
plugin](https://github.com/FujoWebDev/fujocoded-plugins/tree/main/remark-alt-text-files)
219+
220+
You can also become a patron or buy some merch:
221+
222+
- [Monthly Support](https://fujocoded.com/support)
223+
- [Merch Shop](https://store.fujocoded.com/)
224+
- [RobinBoob](https://www.robinboob.com/)
225+
226+
# Follow Us
227+
228+
<p align="center"><a href="https://twitter.com/fujoc0ded"><img width="35" src="https://raw.githubusercontent.com/FujoWebDev/.github/main/profile/images/twitter.svg" /></a><a href="https://www.tumblr.com/fujocoded"><img width="35" src="https://raw.githubusercontent.com/FujoWebDev/.github/main/profile/images/tumblr.svg" /></a><a href="https://bsky.app/profile/fujocoded.bsky.social"><img width="35" src="https://raw.githubusercontent.com/FujoWebDev/.github/main/profile/images/bluesky.svg" /></a><a href="https://blorbo.social/@fujocoded"><img width="35" src="https://raw.githubusercontent.com/FujoWebDev/.github/main/profile/images/mastodon.svg" /></a><a href="https://fujocoded.dreamwidth.org/"><img width="17" src="https://raw.githubusercontent.com/FujoWebDev/.github/main/profile/images/dreamwidth.svg" /></a></p>

astro-authproto/README_OLD.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# @fujocoded/authproto
2+
3+
Astro integration to easily authenticate your site visitors using ATproto. For
4+
Bluesky and beyond.
5+
6+
> [!WARNING] This is not ready for prime time, only use it if you know what
7+
> you're doing. Come back soon for instructions for real production usage!
8+
9+
## Tidbits
10+
11+
- [ ] Remind folks they need a session configured
12+
- [ ] Remind them not to commit the keys
13+
14+
## Current TODOs
15+
16+
- [x] Turn into a proper package
17+
- [x] Check whether you can do unstorage for saving data
18+
- [ ] Write a README
19+
- [ ] Comment the whole Auth files
20+
- [ ] Improve config and make it make sense
21+
- [x] Figure out how to turn AstroDB into an option
22+
- [ ] Add option to redirect logged in/logged out user to a chosen page
23+
- [ ] Explain how to intercept logged out/logged in events so you can do things
24+
- [ ] Figure out how to make types correctly signal Astro.locals.loggedInUser can be null
25+
- [ ] Fix the state bits in the oauth login route
26+
- [x] Figure out what's up with weird amount of implementations in lib/
27+
- [ ] Clean up client-metadata duplication
28+
- [ ] Rename genericData to writeData
29+
30+
// Tip!
31+
Want to see what gets saved by the storage? Use
32+
33+
```
34+
driver: {
35+
name: "fs-lite",
36+
options: {
37+
base: process.cwd() + "/test/",
38+
},
39+
},
40+
```
41+
42+
Make sure not to commit!
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `@fujocoded/authproto` examples
2+
3+
In this folder, you'll find examples that will help you understand
4+
how to use `@fujocoded/authproto` to achieve all your dreams.
5+
6+
## Our Examples
7+
8+
1. TODO: the simplest example => Just shows you how to log in/out
9+
2. TODO: the almost-simplest example =>
10+
- Logged out: Show FujoCoded's Bluesky profile
11+
- Logged in: Show the logged-in user Bluesky profile
12+
- Stretch: get someone to sponsor a paid ad for another service
13+
3. Stepping it up: Reading and Writing Records from/to a PDS
14+
- Add bits scopes here
15+
16+
### Extras
17+
18+
4. Redirect configuration
19+
5. Restrict pages to e.g. friends
20+
21+
// FUTURE
22+
// Astro DB?
File renamed without changes.

0 commit comments

Comments
 (0)