admin-lib-cms 1.0.5
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 +248 -0
- package/dist/auth.d.mts +15 -0
- package/dist/auth.d.ts +15 -0
- package/dist/auth.js +122 -0
- package/dist/auth.mjs +87 -0
- package/dist/chunk-6DZX6EAA.mjs +37 -0
- package/dist/chunk-CQW7KMB2.mjs +120 -0
- package/dist/email.d.mts +3 -0
- package/dist/email.d.ts +3 -0
- package/dist/email.js +144 -0
- package/dist/email.mjs +6 -0
- package/dist/index.d.mts +126 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.js +2344 -0
- package/dist/index.mjs +2223 -0
- package/dist/storage.d.mts +25 -0
- package/dist/storage.d.ts +25 -0
- package/dist/storage.js +412 -0
- package/dist/storage.mjs +383 -0
- package/dist/types-uBvGNd4_.d.mts +78 -0
- package/dist/types-uBvGNd4_.d.ts +78 -0
- package/dist/types.d.mts +78 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/package.json +90 -0
package/README.md
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# admin-lib
|
|
2
|
+
|
|
3
|
+
A lightweight, portable CMS library built for React/Next.js applications, offering seamless database integration, in-page editing, and subscription management.
|
|
4
|
+
|
|
5
|
+
## What is it?
|
|
6
|
+
|
|
7
|
+
`admin-lib` provides a set of components and hooks that allow you to easily drop a fully functional CMS interface into your application. It lets your clients or users edit website content directly on the page, manage collections (like blog posts or products), view form submissions, and handle billing/subscriptions, all from a unified admin interface.
|
|
8
|
+
|
|
9
|
+
## How it works
|
|
10
|
+
|
|
11
|
+
The library works by wrapping your application in a `CmsProvider`. When users access the admin portal (usually `/admin`), it loads the `AdminApp` component which provides the surrounding UI (sidebar, navigation, settings).
|
|
12
|
+
|
|
13
|
+
The actual preview of your website is loaded in an iframe. Using the `Editable` component in your application code allows the admin panel to communicate with your site via `postMessage`. When an admin clicks on an editable element, the sidebar updates to show the correct fields (text, image, rich text) and saves the overrides using the provided `StorageAdapter`.
|
|
14
|
+
|
|
15
|
+
### Key Features:
|
|
16
|
+
1. **In-Page Editing**: Use the `<Editable />` component to make any text, image, or block of content editable.
|
|
17
|
+
2. **Collections**: Manage structured data (like articles or team members) through a dynamic table view.
|
|
18
|
+
3. **Submissions**: Collect and manage form submissions (e.g., contact forms) with built-in email notifications.
|
|
19
|
+
4. **Storage Adapters**: Connect to any backend using a custom adapter, or use `LocalStorageAdapter` for quick local testing.
|
|
20
|
+
5. **Subscriptions**: Built-in UI to manage Stripe subscriptions and billing portals.
|
|
21
|
+
|
|
22
|
+
### Bring Your Own Database (MySQL, PostgreSQL, Sanity)
|
|
23
|
+
|
|
24
|
+
You are not locked into any specific backend! The library uses a standard `StorageAdapter` interface, which means you can save your content anywhere.
|
|
25
|
+
|
|
26
|
+
#### 1. Connecting to MySQL or PostgreSQL
|
|
27
|
+
You can create a custom adapter that sends data to your own API routes, which then save the content using Prisma, Drizzle, or raw SQL queries:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { StorageAdapter } from "admin-lib-cms/types";
|
|
31
|
+
|
|
32
|
+
export const sqlStorageAdapter: StorageAdapter = {
|
|
33
|
+
name: "mysql-postgres-adapter",
|
|
34
|
+
|
|
35
|
+
// Fetch data from your database (e.g. via an API route)
|
|
36
|
+
getOverrides: async () => {
|
|
37
|
+
const res = await fetch("/api/cms/content");
|
|
38
|
+
return res.json();
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
// Save data to your database
|
|
42
|
+
setOverrides: async (overrides) => {
|
|
43
|
+
await fetch("/api/cms/content", {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: JSON.stringify(overrides)
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
// Save form submissions
|
|
50
|
+
saveSubmission: async (sub) => {
|
|
51
|
+
await fetch("/api/cms/submissions", {
|
|
52
|
+
method: "POST",
|
|
53
|
+
body: JSON.stringify(sub)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### 2. Connecting to Sanity.io
|
|
60
|
+
If you prefer using Sanity as your backend, `admin-lib` provides a built-in adapter. Just import it and pass your Sanity client:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { createSanityAdapter } from "admin-lib-cms/storage";
|
|
64
|
+
import { createClient } from "@sanity/client";
|
|
65
|
+
|
|
66
|
+
const sanityClient = createClient({
|
|
67
|
+
projectId: "your-project-id",
|
|
68
|
+
dataset: "production",
|
|
69
|
+
apiVersion: "2024-01-01",
|
|
70
|
+
token: process.env.SANITY_API_TOKEN,
|
|
71
|
+
useCdn: false
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export const mySanityAdapter = createSanityAdapter({
|
|
75
|
+
client: sanityClient
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Pass whichever adapter you choose into the `<CmsProvider adapter={myAdapter}>`.
|
|
80
|
+
## Installation and Usage
|
|
81
|
+
|
|
82
|
+
1. Install the library in your main app:
|
|
83
|
+
```bash
|
|
84
|
+
npm install admin-lib-cms
|
|
85
|
+
```
|
|
86
|
+
*(If you are developing locally, you can use a workspace reference or file path).*
|
|
87
|
+
|
|
88
|
+
### 2. Define your CMS Configuration
|
|
89
|
+
Create a configuration file (e.g. `cms.config.ts`) to tell the admin panel about your site's structure, the pages that can be edited, and the data collections you want to manage.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { CmsConfig } from "admin-lib-cms/types";
|
|
93
|
+
|
|
94
|
+
export const cmsConfig: CmsConfig = {
|
|
95
|
+
siteName: "My Website",
|
|
96
|
+
pages: [
|
|
97
|
+
{ id: "home", name: "Home Page", path: "/" },
|
|
98
|
+
{ id: "about", name: "About Us", path: "/about" },
|
|
99
|
+
],
|
|
100
|
+
collections: [
|
|
101
|
+
{
|
|
102
|
+
id: "blog_posts",
|
|
103
|
+
name: "Blog Posts",
|
|
104
|
+
description: "Manage your blog articles.",
|
|
105
|
+
fields: [
|
|
106
|
+
{ id: "title", label: "Title", type: "text" },
|
|
107
|
+
{ id: "content", label: "Content", type: "richtext" },
|
|
108
|
+
{ id: "coverImage", label: "Cover Image", type: "image" },
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
globals: [
|
|
113
|
+
{
|
|
114
|
+
id: "brand",
|
|
115
|
+
label: "Site Branding",
|
|
116
|
+
fields: [
|
|
117
|
+
{ id: "global-logo", label: "Logo", type: "image", defaultValue: "" },
|
|
118
|
+
{ id: "global-phone", label: "Phone Number", type: "text", defaultValue: "(555) 123-4567" },
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
};
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 3. Wrap your application with the CmsProvider
|
|
126
|
+
Typically in your `layout.tsx` or `_app.tsx`, pass your adapter and configuration to the provider:
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
import { CmsProvider } from "admin-lib-cms";
|
|
130
|
+
import { createLocalStorageAdapter } from "admin-lib-cms/storage";
|
|
131
|
+
import { cmsConfig } from "./cms.config";
|
|
132
|
+
|
|
133
|
+
const adapter = createLocalStorageAdapter();
|
|
134
|
+
|
|
135
|
+
export default function Layout({ children }) {
|
|
136
|
+
return (
|
|
137
|
+
<CmsProvider config={cmsConfig} adapter={adapter}>
|
|
138
|
+
{children}
|
|
139
|
+
</CmsProvider>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
4. Setup Authentication Routes:
|
|
145
|
+
`admin-lib-cms` provides a fully featured authentication system (registration, login, and JWT cookies) that works directly with your Storage Adapter. Create an API route at `app/api/admin/auth/route.ts`:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
import { createAuthHandler } from "admin-lib-cms/auth";
|
|
149
|
+
import { cmsAdapter } from "@/admin-cms.config"; // Your configured adapter
|
|
150
|
+
|
|
151
|
+
const handler = createAuthHandler({
|
|
152
|
+
adapter: cmsAdapter,
|
|
153
|
+
jwtSecret: process.env.JWT_SECRET // Your secure secret string
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
export const GET = handler;
|
|
157
|
+
export const POST = handler;
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
5. Setup the Admin Pages (Dashboard & Login):
|
|
161
|
+
In your app's routing, create the following pages to render the CMS interface.
|
|
162
|
+
|
|
163
|
+
**Dashboard (`app/admin/page.tsx`):**
|
|
164
|
+
```tsx
|
|
165
|
+
import { AdminApp } from "admin-lib-cms";
|
|
166
|
+
|
|
167
|
+
export default function AdminPage() {
|
|
168
|
+
return <AdminApp />;
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Login (`app/admin/login/page.tsx`):**
|
|
173
|
+
```tsx
|
|
174
|
+
"use client";
|
|
175
|
+
import { AdminLogin } from "admin-lib-cms";
|
|
176
|
+
|
|
177
|
+
export default function LoginPage() {
|
|
178
|
+
return <AdminLogin authApiRoute="/api/admin/auth" redirectRoute="/admin" />;
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
6. Make elements editable on your pages:
|
|
183
|
+
|
|
184
|
+
You can use specific `Editable` components depending on the type of content you want to manage.
|
|
185
|
+
|
|
186
|
+
### Available Editable Components:
|
|
187
|
+
|
|
188
|
+
| Component | Description | Example Usage |
|
|
189
|
+
| --- | --- | --- |
|
|
190
|
+
| `EditableText` | For plain text or simple rich text elements. | `<EditableText id="title">My Title</EditableText>` |
|
|
191
|
+
| `EditableImage` | For images (automatically renders an `<img>` tag). | `<EditableImage id="logo" src="/logo.png" />` |
|
|
192
|
+
| `EditableLink` | For hyperlinks (automatically renders an `<a>` tag). | `<EditableLink id="btn" href="/contact">Contact</EditableLink>` |
|
|
193
|
+
| `EditableList` | For an array of items (features, team members, etc). | `<EditableList id="team" initialItems={team} renderItem={...} />` |
|
|
194
|
+
|
|
195
|
+
**Example Usage in a Page:**
|
|
196
|
+
```tsx
|
|
197
|
+
import { EditableText, EditableImage, EditableLink, EditableList } from "admin-lib-cms";
|
|
198
|
+
|
|
199
|
+
export default function HomePage() {
|
|
200
|
+
const features = [{ title: "Fast", desc: "Very fast" }];
|
|
201
|
+
|
|
202
|
+
return (
|
|
203
|
+
<main>
|
|
204
|
+
{/* Text element */}
|
|
205
|
+
<EditableText id="hero-title" as="h1" label="Hero Title">
|
|
206
|
+
Welcome to my site
|
|
207
|
+
</EditableText>
|
|
208
|
+
|
|
209
|
+
{/* Image element */}
|
|
210
|
+
<EditableImage id="hero-image" label="Hero Image" src="/default-hero.jpg" alt="Hero" />
|
|
211
|
+
|
|
212
|
+
{/* Link element */}
|
|
213
|
+
<EditableLink id="hero-cta" href="/about" label="Call to Action">
|
|
214
|
+
Learn More
|
|
215
|
+
</EditableLink>
|
|
216
|
+
|
|
217
|
+
{/* List element */}
|
|
218
|
+
<EditableList
|
|
219
|
+
id="features-list"
|
|
220
|
+
label="Features"
|
|
221
|
+
initialItems={features}
|
|
222
|
+
renderItem={(item, idx) => (
|
|
223
|
+
<div key={idx}>
|
|
224
|
+
<h3>{item.title}</h3>
|
|
225
|
+
<p>{item.desc}</p>
|
|
226
|
+
</div>
|
|
227
|
+
)}
|
|
228
|
+
/>
|
|
229
|
+
</main>
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Development
|
|
235
|
+
|
|
236
|
+
To build the library:
|
|
237
|
+
```bash
|
|
238
|
+
npm run build
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
To run in watch mode during development:
|
|
242
|
+
```bash
|
|
243
|
+
npm run dev
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
## Note on Languages
|
|
248
|
+
The entire library and its UI are currently maintained in **English** to ensure broad compatibility and standard development practices. All previous Spanish texts have been translated.
|
package/dist/auth.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { StorageAdapter } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
declare function createAuthHandler({ adapter, jwtSecret }: {
|
|
5
|
+
adapter: StorageAdapter;
|
|
6
|
+
jwtSecret?: string;
|
|
7
|
+
}): (request: Request) => Promise<NextResponse<{
|
|
8
|
+
hasUsers: boolean;
|
|
9
|
+
}> | NextResponse<{
|
|
10
|
+
ok: boolean;
|
|
11
|
+
}> | NextResponse<{
|
|
12
|
+
error: any;
|
|
13
|
+
}>>;
|
|
14
|
+
|
|
15
|
+
export { createAuthHandler };
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { StorageAdapter } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare function createAuthHandler({ adapter, jwtSecret }: {
|
|
5
|
+
adapter: StorageAdapter;
|
|
6
|
+
jwtSecret?: string;
|
|
7
|
+
}): (request: Request) => Promise<NextResponse<{
|
|
8
|
+
hasUsers: boolean;
|
|
9
|
+
}> | NextResponse<{
|
|
10
|
+
ok: boolean;
|
|
11
|
+
}> | NextResponse<{
|
|
12
|
+
error: any;
|
|
13
|
+
}>>;
|
|
14
|
+
|
|
15
|
+
export { createAuthHandler };
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/auth.ts
|
|
31
|
+
var auth_exports = {};
|
|
32
|
+
__export(auth_exports, {
|
|
33
|
+
createAuthHandler: () => createAuthHandler
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(auth_exports);
|
|
36
|
+
var import_server = require("next/server");
|
|
37
|
+
var import_bcryptjs = __toESM(require("bcryptjs"));
|
|
38
|
+
var import_jose = require("jose");
|
|
39
|
+
function createAuthHandler({ adapter, jwtSecret }) {
|
|
40
|
+
const secret = new TextEncoder().encode(jwtSecret || "fallback_secret_for_development_only");
|
|
41
|
+
return async function handler(request) {
|
|
42
|
+
try {
|
|
43
|
+
const url = new URL(request.url);
|
|
44
|
+
const action = url.searchParams.get("action");
|
|
45
|
+
if (request.method === "GET") {
|
|
46
|
+
const users = await adapter.listEntries("usersAdmin");
|
|
47
|
+
return import_server.NextResponse.json({ hasUsers: users && users.length > 0 });
|
|
48
|
+
}
|
|
49
|
+
if (request.method === "POST") {
|
|
50
|
+
let body = {};
|
|
51
|
+
if (action !== "logout") {
|
|
52
|
+
body = await request.json().catch(() => ({}));
|
|
53
|
+
}
|
|
54
|
+
if (action === "logout") {
|
|
55
|
+
const response = import_server.NextResponse.json({ ok: true });
|
|
56
|
+
response.cookies.delete("admin_token");
|
|
57
|
+
return response;
|
|
58
|
+
}
|
|
59
|
+
const { email, password } = body;
|
|
60
|
+
if (!email || !password) {
|
|
61
|
+
return import_server.NextResponse.json({ error: "Email and password are required" }, { status: 400 });
|
|
62
|
+
}
|
|
63
|
+
const users = await adapter.listEntries("usersAdmin") || [];
|
|
64
|
+
if (action === "register") {
|
|
65
|
+
if (users.length > 0) {
|
|
66
|
+
return import_server.NextResponse.json({ error: "Users already exist. Registration disabled." }, { status: 403 });
|
|
67
|
+
}
|
|
68
|
+
const hashedPassword = await import_bcryptjs.default.hash(password, 10);
|
|
69
|
+
const newUser = {
|
|
70
|
+
id: crypto.randomUUID(),
|
|
71
|
+
collectionId: "usersAdmin",
|
|
72
|
+
email,
|
|
73
|
+
password: hashedPassword
|
|
74
|
+
};
|
|
75
|
+
await adapter.putEntry("usersAdmin", newUser);
|
|
76
|
+
const token = await new import_jose.SignJWT({ email: newUser.email, id: newUser.id }).setProtectedHeader({ alg: "HS256" }).setExpirationTime("24h").sign(secret);
|
|
77
|
+
const response = import_server.NextResponse.json({ ok: true });
|
|
78
|
+
response.cookies.set({
|
|
79
|
+
name: "admin_token",
|
|
80
|
+
value: token,
|
|
81
|
+
httpOnly: true,
|
|
82
|
+
path: "/",
|
|
83
|
+
secure: process.env.NODE_ENV === "production",
|
|
84
|
+
maxAge: 60 * 60 * 24
|
|
85
|
+
// 24 hours
|
|
86
|
+
});
|
|
87
|
+
return response;
|
|
88
|
+
}
|
|
89
|
+
if (action === "login") {
|
|
90
|
+
const user = users.find((u) => u.email === email);
|
|
91
|
+
if (!user) {
|
|
92
|
+
return import_server.NextResponse.json({ error: "Invalid email or password" }, { status: 401 });
|
|
93
|
+
}
|
|
94
|
+
const isMatch = await import_bcryptjs.default.compare(password, user.password);
|
|
95
|
+
if (!isMatch) {
|
|
96
|
+
return import_server.NextResponse.json({ error: "Invalid email or password" }, { status: 401 });
|
|
97
|
+
}
|
|
98
|
+
const token = await new import_jose.SignJWT({ email: user.email, id: user.id }).setProtectedHeader({ alg: "HS256" }).setExpirationTime("24h").sign(secret);
|
|
99
|
+
const response = import_server.NextResponse.json({ ok: true });
|
|
100
|
+
response.cookies.set({
|
|
101
|
+
name: "admin_token",
|
|
102
|
+
value: token,
|
|
103
|
+
httpOnly: true,
|
|
104
|
+
path: "/",
|
|
105
|
+
secure: process.env.NODE_ENV === "production",
|
|
106
|
+
maxAge: 60 * 60 * 24
|
|
107
|
+
// 24 hours
|
|
108
|
+
});
|
|
109
|
+
return response;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return import_server.NextResponse.json({ error: "Method not allowed" }, { status: 405 });
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error("Auth handler error:", err);
|
|
115
|
+
return import_server.NextResponse.json({ error: err.message || "Internal server error" }, { status: 500 });
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
120
|
+
0 && (module.exports = {
|
|
121
|
+
createAuthHandler
|
|
122
|
+
});
|
package/dist/auth.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/auth.ts
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
import bcrypt from "bcryptjs";
|
|
4
|
+
import { SignJWT } from "jose";
|
|
5
|
+
function createAuthHandler({ adapter, jwtSecret }) {
|
|
6
|
+
const secret = new TextEncoder().encode(jwtSecret || "fallback_secret_for_development_only");
|
|
7
|
+
return async function handler(request) {
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(request.url);
|
|
10
|
+
const action = url.searchParams.get("action");
|
|
11
|
+
if (request.method === "GET") {
|
|
12
|
+
const users = await adapter.listEntries("usersAdmin");
|
|
13
|
+
return NextResponse.json({ hasUsers: users && users.length > 0 });
|
|
14
|
+
}
|
|
15
|
+
if (request.method === "POST") {
|
|
16
|
+
let body = {};
|
|
17
|
+
if (action !== "logout") {
|
|
18
|
+
body = await request.json().catch(() => ({}));
|
|
19
|
+
}
|
|
20
|
+
if (action === "logout") {
|
|
21
|
+
const response = NextResponse.json({ ok: true });
|
|
22
|
+
response.cookies.delete("admin_token");
|
|
23
|
+
return response;
|
|
24
|
+
}
|
|
25
|
+
const { email, password } = body;
|
|
26
|
+
if (!email || !password) {
|
|
27
|
+
return NextResponse.json({ error: "Email and password are required" }, { status: 400 });
|
|
28
|
+
}
|
|
29
|
+
const users = await adapter.listEntries("usersAdmin") || [];
|
|
30
|
+
if (action === "register") {
|
|
31
|
+
if (users.length > 0) {
|
|
32
|
+
return NextResponse.json({ error: "Users already exist. Registration disabled." }, { status: 403 });
|
|
33
|
+
}
|
|
34
|
+
const hashedPassword = await bcrypt.hash(password, 10);
|
|
35
|
+
const newUser = {
|
|
36
|
+
id: crypto.randomUUID(),
|
|
37
|
+
collectionId: "usersAdmin",
|
|
38
|
+
email,
|
|
39
|
+
password: hashedPassword
|
|
40
|
+
};
|
|
41
|
+
await adapter.putEntry("usersAdmin", newUser);
|
|
42
|
+
const token = await new SignJWT({ email: newUser.email, id: newUser.id }).setProtectedHeader({ alg: "HS256" }).setExpirationTime("24h").sign(secret);
|
|
43
|
+
const response = NextResponse.json({ ok: true });
|
|
44
|
+
response.cookies.set({
|
|
45
|
+
name: "admin_token",
|
|
46
|
+
value: token,
|
|
47
|
+
httpOnly: true,
|
|
48
|
+
path: "/",
|
|
49
|
+
secure: process.env.NODE_ENV === "production",
|
|
50
|
+
maxAge: 60 * 60 * 24
|
|
51
|
+
// 24 hours
|
|
52
|
+
});
|
|
53
|
+
return response;
|
|
54
|
+
}
|
|
55
|
+
if (action === "login") {
|
|
56
|
+
const user = users.find((u) => u.email === email);
|
|
57
|
+
if (!user) {
|
|
58
|
+
return NextResponse.json({ error: "Invalid email or password" }, { status: 401 });
|
|
59
|
+
}
|
|
60
|
+
const isMatch = await bcrypt.compare(password, user.password);
|
|
61
|
+
if (!isMatch) {
|
|
62
|
+
return NextResponse.json({ error: "Invalid email or password" }, { status: 401 });
|
|
63
|
+
}
|
|
64
|
+
const token = await new SignJWT({ email: user.email, id: user.id }).setProtectedHeader({ alg: "HS256" }).setExpirationTime("24h").sign(secret);
|
|
65
|
+
const response = NextResponse.json({ ok: true });
|
|
66
|
+
response.cookies.set({
|
|
67
|
+
name: "admin_token",
|
|
68
|
+
value: token,
|
|
69
|
+
httpOnly: true,
|
|
70
|
+
path: "/",
|
|
71
|
+
secure: process.env.NODE_ENV === "production",
|
|
72
|
+
maxAge: 60 * 60 * 24
|
|
73
|
+
// 24 hours
|
|
74
|
+
});
|
|
75
|
+
return response;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return NextResponse.json({ error: "Method not allowed" }, { status: 405 });
|
|
79
|
+
} catch (err) {
|
|
80
|
+
console.error("Auth handler error:", err);
|
|
81
|
+
return NextResponse.json({ error: err.message || "Internal server error" }, { status: 500 });
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
createAuthHandler
|
|
87
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
+
}) : x)(function(x) {
|
|
10
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
+
});
|
|
13
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
__require,
|
|
35
|
+
__commonJS,
|
|
36
|
+
__toESM
|
|
37
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// src/email.ts
|
|
2
|
+
function generateEmailTemplate(data, title, description) {
|
|
3
|
+
const items = Object.entries(data).filter(([_, value]) => value !== void 0 && value !== null && value !== "").map(([key, value]) => {
|
|
4
|
+
const label = key.replace(/([A-Z])/g, " $1").replace(/^./, (str) => str.toUpperCase());
|
|
5
|
+
const displayValue = Array.isArray(value) ? value.join(", ") : String(value);
|
|
6
|
+
return `
|
|
7
|
+
<div class="detail-item">
|
|
8
|
+
<span class="detail-label">${label}</span>
|
|
9
|
+
<span class="detail-value">${displayValue}</span>
|
|
10
|
+
</div>
|
|
11
|
+
`;
|
|
12
|
+
}).join("");
|
|
13
|
+
return `
|
|
14
|
+
<!DOCTYPE html>
|
|
15
|
+
<html lang="en">
|
|
16
|
+
<head>
|
|
17
|
+
<meta charset="UTF-8">
|
|
18
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
19
|
+
<title>${title}</title>
|
|
20
|
+
<style>
|
|
21
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
22
|
+
body {
|
|
23
|
+
background-color: #f4f4f5;
|
|
24
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
|
25
|
+
color: #18181b;
|
|
26
|
+
line-height: 1.5;
|
|
27
|
+
}
|
|
28
|
+
.wrapper { width: 100%; padding: 40px 20px; }
|
|
29
|
+
.container { max-width: 600px; margin: 0 auto; }
|
|
30
|
+
.card {
|
|
31
|
+
background: #ffffff;
|
|
32
|
+
border-radius: 12px;
|
|
33
|
+
overflow: hidden;
|
|
34
|
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
|
35
|
+
border: 1px solid #e4e4e7;
|
|
36
|
+
}
|
|
37
|
+
.header {
|
|
38
|
+
background: #5fa075;
|
|
39
|
+
padding: 40px 32px;
|
|
40
|
+
text-align: center;
|
|
41
|
+
border-bottom: 4px solid #48805c;
|
|
42
|
+
}
|
|
43
|
+
.header h1 {
|
|
44
|
+
color: #ffffff;
|
|
45
|
+
font-size: 24px;
|
|
46
|
+
font-weight: 700;
|
|
47
|
+
margin-bottom: 8px;
|
|
48
|
+
text-transform: uppercase;
|
|
49
|
+
letter-spacing: 0.05em;
|
|
50
|
+
}
|
|
51
|
+
.header p {
|
|
52
|
+
color: rgba(255, 255, 255, 0.9);
|
|
53
|
+
font-size: 14px;
|
|
54
|
+
}
|
|
55
|
+
.content { padding: 32px; }
|
|
56
|
+
.details-grid {
|
|
57
|
+
display: grid;
|
|
58
|
+
gap: 12px;
|
|
59
|
+
margin-bottom: 24px;
|
|
60
|
+
}
|
|
61
|
+
.detail-item {
|
|
62
|
+
background: #fafafa;
|
|
63
|
+
padding: 12px 16px;
|
|
64
|
+
border-radius: 8px;
|
|
65
|
+
border: 1px solid #f1f1f4;
|
|
66
|
+
}
|
|
67
|
+
.detail-label {
|
|
68
|
+
font-size: 11px;
|
|
69
|
+
font-weight: 700;
|
|
70
|
+
color: #71717a;
|
|
71
|
+
text-transform: uppercase;
|
|
72
|
+
letter-spacing: 0.05em;
|
|
73
|
+
display: block;
|
|
74
|
+
margin-bottom: 4px;
|
|
75
|
+
}
|
|
76
|
+
.detail-value {
|
|
77
|
+
font-size: 14px;
|
|
78
|
+
font-weight: 500;
|
|
79
|
+
color: #18181b;
|
|
80
|
+
word-break: break-word;
|
|
81
|
+
}
|
|
82
|
+
.footer {
|
|
83
|
+
padding: 24px 32px;
|
|
84
|
+
background: #f8fafc;
|
|
85
|
+
border-top: 1px solid #e2e8f0;
|
|
86
|
+
text-align: center;
|
|
87
|
+
}
|
|
88
|
+
.footer-text {
|
|
89
|
+
font-size: 12px;
|
|
90
|
+
color: #64748b;
|
|
91
|
+
}
|
|
92
|
+
</style>
|
|
93
|
+
</head>
|
|
94
|
+
<body>
|
|
95
|
+
<div class="wrapper">
|
|
96
|
+
<div class="container">
|
|
97
|
+
<div class="card">
|
|
98
|
+
<div class="header">
|
|
99
|
+
<h1>${title}</h1>
|
|
100
|
+
<p>${description}</p>
|
|
101
|
+
</div>
|
|
102
|
+
<div class="content">
|
|
103
|
+
<div class="details-grid">
|
|
104
|
+
${items}
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
<div class="footer">
|
|
108
|
+
<p class="footer-text">\xA9 ${(/* @__PURE__ */ new Date()).getFullYear()} Serenity Wellness. This is an automated notification.</p>
|
|
109
|
+
</div>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
</div>
|
|
113
|
+
</body>
|
|
114
|
+
</html>
|
|
115
|
+
`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export {
|
|
119
|
+
generateEmailTemplate
|
|
120
|
+
};
|
package/dist/email.d.mts
ADDED
package/dist/email.d.ts
ADDED