amplify-astro-adapter 0.1.0 → 0.1.3
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 +181 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# amplify-astro-adapter
|
|
2
|
+
|
|
3
|
+
AWS Amplify adapter for Astro with built-in cookie-based sessions.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Astro 4.x and 5.x support
|
|
8
|
+
- **Cookie-based sessions** - Zero config, works with serverless
|
|
9
|
+
- **Auto-generates `amplify.yml`** - Detects your package manager
|
|
10
|
+
- **envGetSecret** support for type-safe environment variables
|
|
11
|
+
- Configurable Node.js runtime (nodejs20.x, nodejs22.x)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install amplify-astro-adapter
|
|
17
|
+
# or
|
|
18
|
+
pnpm add amplify-astro-adapter
|
|
19
|
+
# or
|
|
20
|
+
yarn add amplify-astro-adapter
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
// astro.config.mjs
|
|
27
|
+
import { defineConfig } from 'astro/config';
|
|
28
|
+
import amplify from 'amplify-astro-adapter';
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
output: 'server',
|
|
32
|
+
adapter: amplify()
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
That's it! Sessions and middleware are configured automatically.
|
|
37
|
+
|
|
38
|
+
## Configuration Options
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
output: 'server',
|
|
43
|
+
adapter: amplify({
|
|
44
|
+
runtime: 'nodejs20.x' // or 'nodejs22.x'
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
| Option | Type | Default | Description |
|
|
50
|
+
|--------|------|---------|-------------|
|
|
51
|
+
| `runtime` | `'nodejs20.x' \| 'nodejs22.x'` | `'nodejs20.x'` | Node.js runtime for AWS Lambda |
|
|
52
|
+
|
|
53
|
+
## Sessions
|
|
54
|
+
|
|
55
|
+
Sessions are **enabled by default** with zero configuration. Data is stored in encrypted HTTP-only cookies.
|
|
56
|
+
|
|
57
|
+
### Environment Variable
|
|
58
|
+
|
|
59
|
+
Set a session secret for encryption (32+ characters):
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
AMPLIFY_SESSION_PASSWORD=your-32-character-or-longer-secret
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
> In development, a secret is auto-generated. **Always set this in production.**
|
|
66
|
+
|
|
67
|
+
### Using Sessions
|
|
68
|
+
|
|
69
|
+
```astro
|
|
70
|
+
---
|
|
71
|
+
// src/pages/dashboard.astro
|
|
72
|
+
const username = await Astro.session?.get('username');
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
<h1>Welcome {username}!</h1>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### API Routes
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
// src/pages/api/login.ts
|
|
82
|
+
import type { APIRoute } from 'astro';
|
|
83
|
+
|
|
84
|
+
export const POST: APIRoute = async ({ session, request, redirect }) => {
|
|
85
|
+
const formData = await request.formData();
|
|
86
|
+
const username = formData.get('username') as string;
|
|
87
|
+
|
|
88
|
+
session?.set('username', username);
|
|
89
|
+
|
|
90
|
+
return redirect('/');
|
|
91
|
+
};
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// src/pages/api/logout.ts
|
|
96
|
+
import type { APIRoute } from 'astro';
|
|
97
|
+
|
|
98
|
+
export const POST: APIRoute = async ({ session, redirect }) => {
|
|
99
|
+
await session?.destroy();
|
|
100
|
+
return redirect('/');
|
|
101
|
+
};
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Custom Session Configuration
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
import { defineConfig } from 'astro/config';
|
|
108
|
+
import amplify from 'amplify-astro-adapter';
|
|
109
|
+
import { createCookieSessionDriver } from 'amplify-astro-adapter/session';
|
|
110
|
+
|
|
111
|
+
export default defineConfig({
|
|
112
|
+
output: 'server',
|
|
113
|
+
adapter: amplify(),
|
|
114
|
+
session: {
|
|
115
|
+
driver: createCookieSessionDriver({
|
|
116
|
+
password: process.env.AMPLIFY_SESSION_PASSWORD,
|
|
117
|
+
ttl: 86400, // 1 day in seconds
|
|
118
|
+
cookieOptions: {
|
|
119
|
+
httpOnly: true,
|
|
120
|
+
secure: true,
|
|
121
|
+
sameSite: 'lax',
|
|
122
|
+
path: '/',
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Session Limitations
|
|
130
|
+
|
|
131
|
+
- **4KB per cookie** - Large sessions auto-chunk across multiple cookies
|
|
132
|
+
- **Client-stored** - Encrypted, but don't store sensitive server-only data
|
|
133
|
+
|
|
134
|
+
## AWS Amplify Hosting
|
|
135
|
+
|
|
136
|
+
### amplify.yml Auto-Generation
|
|
137
|
+
|
|
138
|
+
On first build, the adapter generates `amplify.yml` for your package manager. Just commit it and deploy!
|
|
139
|
+
|
|
140
|
+
### Build Settings
|
|
141
|
+
|
|
142
|
+
Set the custom image environment variable in Amplify Console:
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
_CUSTOM_IMAGE=amplify:al2023
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## envGetSecret Support
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import { getSecret } from 'astro:env/server';
|
|
152
|
+
|
|
153
|
+
const apiKey = await getSecret('API_KEY');
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Migration from astro-aws-amplify
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
npm uninstall astro-aws-amplify
|
|
160
|
+
npm install amplify-astro-adapter
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```diff
|
|
164
|
+
- import awsAmplify from 'astro-aws-amplify';
|
|
165
|
+
+ import amplify from 'amplify-astro-adapter';
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Package Exports
|
|
169
|
+
|
|
170
|
+
| Export | Description |
|
|
171
|
+
|--------|-------------|
|
|
172
|
+
| `amplify-astro-adapter` | Main adapter function |
|
|
173
|
+
| `amplify-astro-adapter/session` | Session driver for custom configuration |
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
MIT
|
|
178
|
+
|
|
179
|
+
## Author
|
|
180
|
+
|
|
181
|
+
Zach Handley <zachhandley@gmail.com>
|