Skip to content

Commit b728333

Browse files
various changes to authproto
1 parent 66f93f0 commit b728333

6 files changed

Lines changed: 87 additions & 29 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fujocoded/authproto": minor
3+
---
4+
5+
Add loggedInClient to Astro.locals and expose the full OAuthSession for the new lex package.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@fujocoded/authproto": patch
3+
---
4+
5+
Add input attributes to Login component for better mobile UX (placeholder, inputmode, autocorrect, autocapitalize, autocomplete, spellcheck).
6+
Allow user to pass down form props to form.
7+
Partially based on https://tangled.org/strings/did:plc:4gt3dbmp4pydjiemob4konzm/3mihke6jals22

astro-authproto/README.md

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ In this package, you'll find:
7272
- [Server adapter](https://docs.astro.build/en/guides/on-demand-rendering/#server-adapters) to set up sessions
7373
- (Optional) [session driver](https://docs.astro.build/en/reference/configuration-reference/#sessiondriver) to allow users to log in or log out
7474

75-
> [!IMPORTANT]
76-
> `deno` requires a workaround due to a CJS/ESM import issue within
75+
> [!IMPORTANT] > `deno` requires a workaround due to a CJS/ESM import issue within
7776
> `@atproto/oauth-client-node`. For now, avoid using `deno` and use other package managers.
7877
7978
> [!IMPORTANT]
@@ -92,8 +91,8 @@ npx astro add @fujocoded/authproto
9291
This will automatically install `@fujocoded/authproto` and add the integration to your `astro.config.mjs` file.
9392

9493
> [!TIP]
95-
>
96-
> You can take a look at all the [possible configuration options below](#configuration-options).
94+
>
95+
> You can take a look at [all the settings you can tweak below](#configuring-authproto).
9796
9897
### Manual Installation
9998

@@ -123,10 +122,12 @@ export default defineConfig({
123122

124123
> [!TIP]
125124
>
126-
> You can take a look at all the [possible configuration options below](#configuration-options).
125+
> You can take a look at [all the settings you can tweak below](#configuring-authproto).
127126
128127
# Using `@fujocoded/authproto`
129128

129+
## Add your login form
130+
130131
Add the `<Login />` component to your site, like this:
131132

132133
```jsx
@@ -138,14 +139,14 @@ import { Login } from "@fujocoded/authproto/components";
138139
<Login />
139140
```
140141

141-
> [!TIP]
142-
>
143-
> You might run into a naming collision issue if you also have a page named `login`. You can fix this by replacing `{ Login }` with `{ Login as LoginComponent }`.
144-
145142
It'll look like a plain form:
146143

147144
<!-- screenshot -->
148145

146+
See [Customizing the login form](#customizing-the-login-form) for ways to change how it looks and where it sends people after they log in.
147+
148+
## Make a page only visible to logged in users
149+
149150
To make a page only visible to logged in users:
150151

151152
```jsx
@@ -167,18 +168,31 @@ if (!loggedInUser) {
167168

168169
... And you've got authentication working on your Astro site!
169170

171+
> [!TIP]
172+
>
173+
> If you also have a page file named `login.astro`, you'll see the TypeScript error `Import declaration conflicts with local declaration of 'Login'` on the import line. Fix it by renaming the import:
174+
>
175+
> ```js
176+
> import { Login as LoginComponent } from "@fujocoded/authproto/components";
177+
> ```
178+
170179
# Okay how do I _actually_ do stuff with this?
171180
172181
Check out the example sites included under the [examples folder](./__examples__/).
173182
174-
# Configuration options
183+
# Configuring authproto
184+
185+
These settings go inside the `authproto({ ... })` call in your
186+
`astro.config.mjs`.
175187
176188
- `applicationName`, required. The name of your application. For example, you
177189
can set this to `"My personal guestbook"`!
178190
- `applicationDomain`, required. It should be a domain that your site is on, or
179191
you can just put in `"localhost:4321"` for now.
180-
- `defaultDevUser`, optional. The default handle that gets filled out in the
181-
`<Login />` component during development.
192+
- `defaultDevUser`, optional. A handle that gets pre-filled into the
193+
[login form](#customizing-the-login-form) while you're developing your site
194+
locally (never in production). Saves you from re-typing your handle every
195+
time you restart the dev server.
182196
- `driver`, optional. The driver used to store data about OAuth sessions. This
183197
takes Astro's [session driver options](https://docs.astro.build/en/reference/configuration-reference/#sessiondriver).
184198
You can also set this with `name: "astro:db"` to utilize [Astro's DB
@@ -200,6 +214,34 @@ Check out the example sites included under the [examples folder](./__examples__/
200214
permissions to include other services. This should be an array of strings,
201215
like this: `["scope1", "scope2"]`
202216
217+
# Customizing the login form
218+
219+
You can change how `<Login />` looks and behaves by passing it these options:
220+
221+
- `redirect`, optional. Where to send the user after they successfully log in
222+
or log out.
223+
- `placeholder`, optional. The hint text shown inside the input when it's
224+
empty. Defaults to `"handle.bsky.social"`.
225+
- Any standard HTML `<form>` attribute: `class`, `class:list`, `id`,
226+
`aria-*`, `data-*`, `style`, and so on. These get applied directly to the
227+
form, so you can style it, label it for screen readers, or use any field other libraries may require.
228+
- NOTE: `action` and `method` are set by the component — they're what makes
229+
login and logout work, so they can't be changed.
230+
231+
> [!TIP]
232+
>
233+
> During development, you can pre-fill the input with a default handle by
234+
> setting [`defaultDevUser`](#configuring-authproto) in your `astro.config.mjs`.
235+
236+
```jsx
237+
<Login
238+
class="my-login-form"
239+
aria-label="Sign in with your Atmosphere account"
240+
redirect="/dashboard"
241+
placeholder="you.bsky.social"
242+
/>
243+
```
244+
203245
# Support Us
204246

205247
You can check out more of our plugins here:

astro-authproto/src/components/Login.astro

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
---
2+
import type { HTMLAttributes } from "astro/types";
23
import { defaultDevUser } from "fujocoded:authproto/config";
34
4-
interface Props {
5+
interface Props extends Omit<HTMLAttributes<"form">, "action" | "method"> {
56
/** Optional redirect URL after successful login/logout */
67
redirect?: string;
8+
placeholder?: string;
79
}
810
9-
const { redirect: redirectUrl } = Astro.props;
11+
const {
12+
redirect: redirectUrl,
13+
placeholder = "handle.bsky.social",
14+
...formAttrs
15+
} = Astro.props;
1016
const currentUser = Astro.locals.loggedInUser;
1117
const currentError =
1218
Astro.locals.authproto?.errorDescription ?? Astro.locals.authproto?.errorCode;
13-
14-
// @ts-expect-error
15-
const { class: cssClass, "class:list": cssClassList } = Astro.props;
1619
---
1720

1821
{
1922
currentUser ? (
2023
// If there's a current user, show the log out button
21-
<form
22-
action="/oauth/logout"
23-
method="post"
24-
class={cssClass}
25-
class:list={cssClassList}
26-
>
24+
<form {...formAttrs} action="/oauth/logout" method="post">
2725
{redirectUrl && (
2826
<input type="hidden" name="redirect" value={redirectUrl} />
2927
)}
3028
<button type="submit">Logout</button>
3129
</form>
3230
) : (
3331
// If there's no current user, show the login button
34-
<form
35-
action="/oauth/login"
36-
method="post"
37-
class={cssClass}
38-
class:list={cssClassList}
39-
>
32+
<form {...formAttrs} action="/oauth/login" method="post">
4033
<input
34+
type="text"
4135
name="atproto-id"
36+
placeholder={placeholder}
37+
inputmode="email"
38+
autocorrect="off"
39+
autocapitalize="off"
40+
autocomplete="username"
41+
spellcheck="false"
4242
value={import.meta.env.DEV ? defaultDevUser : undefined}
4343
/>
4444
{redirectUrl && (

astro-authproto/src/routes/middleware.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const onRequest: MiddlewareHandler = async (
2323

2424
if (!session || !userDid) {
2525
locals.loggedInUser = null;
26+
locals.loggedInClient = null;
2627
return next();
2728
}
2829

@@ -36,10 +37,12 @@ export const onRequest: MiddlewareHandler = async (
3637
handle: await didToHandle(loggedInClient.did),
3738
fetchHandler: loggedInClient.fetchHandler.bind(loggedInClient),
3839
};
40+
locals.loggedInClient = loggedInClient;
3941
}
4042
} catch (e) {
4143
await session.delete("atproto-did");
4244
locals.loggedInUser = null;
45+
locals.loggedInClient = null;
4346
}
4447

4548
return next();

astro-authproto/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare global {
1313
handle: string;
1414
fetchHandler: import("@atproto/oauth-client-node").OAuthSession["fetchHandler"];
1515
} | null;
16+
loggedInClient: import("@atproto/oauth-client-node").OAuthSession | null;
1617
authproto: {
1718
attemptedHandle?: string;
1819
errorDescription?: string;

0 commit comments

Comments
 (0)