create-thexjs-app 1.2.2 → 1.3.0
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 +19 -20
- package/dist/index.js +21 -4
- package/package.json +3 -3
- package/templates/addons/auth/src/api/auth/[...auth].ts +13 -0
- package/templates/addons/auth/src/api/auth/signup.ts +25 -0
- package/templates/addons/auth/src/lib/auth.ts +66 -61
- package/templates/addons/auth/src/lib/migrations/001_create_users.sql +7 -0
- package/templates/addons/auth/src/pages/dashboard/_middleware.ts +2 -15
- package/templates/addons/auth/src/pages/dashboard/index.tsx +2 -2
- package/templates/addons/auth/src/pages/login.tsx +12 -12
- package/templates/addons/auth/src/pages/signup.tsx +84 -0
- package/templates/addons/content/content/hello-world.md +1 -0
- package/templates/base/AGENTS.md +1 -1
- package/templates/addons/auth/src/api/auth/login.ts +0 -28
- package/templates/addons/auth/src/api/auth/logout.ts +0 -12
- package/templates/addons/auth/src/lib/migrations/001_create_sessions.sql +0 -7
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://github.com/
|
|
2
|
+
<img src="https://github.com/thexjs/x/raw/main/examples/landing/public/icon-512.png" alt="Thexjs logo" width="96" height="96">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<h1 align="center">create-thexjs-app</h1>
|
|
@@ -25,7 +25,6 @@ bun create thexjs-app@latest
|
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
You'll be guided through interactive prompts:
|
|
28
|
-
|
|
29
28
|
- **Project name** — e.g. `my-app`
|
|
30
29
|
- **Features** — choose from Tailwind CSS, shadcn/ui, Auth, Content collections, and Hooks
|
|
31
30
|
- **Install & git** — confirm dependency install and `git init`
|
|
@@ -43,13 +42,13 @@ Your app runs at `http://localhost:3000` with hot reload.
|
|
|
43
42
|
The scaffolder ships a single lean base template. Every feature is optional and
|
|
44
43
|
selected at prompt time (or via CLI flags).
|
|
45
44
|
|
|
46
|
-
| Feature
|
|
47
|
-
|
|
|
48
|
-
| **Tailwind CSS**
|
|
49
|
-
| **shadcn/ui**
|
|
50
|
-
| **Auth**
|
|
51
|
-
| **Content**
|
|
52
|
-
| **Hooks**
|
|
45
|
+
| Feature | Adds |
|
|
46
|
+
| ---------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
47
|
+
| **Tailwind CSS** | Utility-first styling + the x dev/build Tailwind pipeline |
|
|
48
|
+
| **shadcn/ui** | Accessible React components in `src/components/ui` (requires Tailwind) |
|
|
49
|
+
| **Auth** | SQLite-backed demo sessions, `/login` and a protected `/dashboard` with middleware |
|
|
50
|
+
| **Content** | Markdown/MDX with frontmatter in `content/`, auto-routed and indexed on `/blog` |
|
|
51
|
+
| **Hooks** | `@thexjs/hooks` dependency — SSR-safe React hooks (debounce, media query, localStorage, forms, server actions) |
|
|
53
52
|
|
|
54
53
|
Enabling **shadcn/ui** automatically enables **Tailwind CSS**.
|
|
55
54
|
|
|
@@ -64,17 +63,17 @@ Skips the interactive prompts — useful for scripting and CI:
|
|
|
64
63
|
bun create thexjs-app@latest my-app --tailwind --auth --content --shadcn
|
|
65
64
|
```
|
|
66
65
|
|
|
67
|
-
| Flag
|
|
68
|
-
|
|
|
69
|
-
| `<name>`
|
|
70
|
-
| `--tailwind`
|
|
71
|
-
| `--shadcn`
|
|
72
|
-
| `--auth`
|
|
73
|
-
| `--content`
|
|
74
|
-
| `--hooks`
|
|
75
|
-
| `--no-install`
|
|
76
|
-
| `--no-git`
|
|
77
|
-
| `--dev`
|
|
66
|
+
| Flag | Description |
|
|
67
|
+
| -------------- | ----------------------------------------------- |
|
|
68
|
+
| `<name>` | Project name (creates a directory named `slug`) |
|
|
69
|
+
| `--tailwind` | Include Tailwind CSS |
|
|
70
|
+
| `--shadcn` | Include shadcn/ui (implies `--tailwind`) |
|
|
71
|
+
| `--auth` | Include demo auth |
|
|
72
|
+
| `--content` | Include content collections |
|
|
73
|
+
| `--hooks` | Include the `@thexjs/hooks` package |
|
|
74
|
+
| `--no-install` | Skip `bun install` |
|
|
75
|
+
| `--no-git` | Skip `git init` |
|
|
76
|
+
| `--dev` | Start the dev server after install |
|
|
78
77
|
|
|
79
78
|
## Development
|
|
80
79
|
|
package/dist/index.js
CHANGED
|
@@ -37,7 +37,7 @@ function initGitRepo(cwd, env) {
|
|
|
37
37
|
|
|
38
38
|
// src/templates.ts
|
|
39
39
|
import { cpSync, existsSync, mkdirSync, readdirSync, statSync } from "fs";
|
|
40
|
-
import { dirname, join } from "path";
|
|
40
|
+
import { basename, dirname, join } from "path";
|
|
41
41
|
import { fileURLToPath } from "url";
|
|
42
42
|
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
43
43
|
var TEMPLATES_ROOT = join(__dirname, "..", "templates");
|
|
@@ -76,9 +76,11 @@ var FEATURES = [
|
|
|
76
76
|
{
|
|
77
77
|
id: "auth",
|
|
78
78
|
label: "Auth (sessions + login)",
|
|
79
|
-
hint: "
|
|
79
|
+
hint: "Real @thexjs/auth credentials provider -- /login, /signup, and a protected /dashboard",
|
|
80
80
|
default: false,
|
|
81
|
-
dependencies: {
|
|
81
|
+
dependencies: {
|
|
82
|
+
"@thexjs/auth": "^3.0.0"
|
|
83
|
+
},
|
|
82
84
|
devDependencies: {}
|
|
83
85
|
},
|
|
84
86
|
{
|
|
@@ -136,6 +138,16 @@ function normalizeFeatures(selected) {
|
|
|
136
138
|
function ensureDir(path) {
|
|
137
139
|
if (!existsSync(path)) mkdirSync(path, { recursive: true });
|
|
138
140
|
}
|
|
141
|
+
function createTargetDir(path) {
|
|
142
|
+
try {
|
|
143
|
+
mkdirSync(path, { recursive: false });
|
|
144
|
+
} catch (err) {
|
|
145
|
+
if (err.code !== "EEXIST") throw err;
|
|
146
|
+
if (readdirSync(path).length > 0) {
|
|
147
|
+
throw new Error(`Directory "${basename(path)}" already exists and is not empty.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
139
151
|
function mergeTree(src, dest) {
|
|
140
152
|
ensureDir(dest);
|
|
141
153
|
for (const entry of readdirSync(src)) {
|
|
@@ -373,7 +385,12 @@ async function main() {
|
|
|
373
385
|
}
|
|
374
386
|
const spin = spinner();
|
|
375
387
|
spin.start(`Scaffolding "${slug}"`);
|
|
376
|
-
|
|
388
|
+
try {
|
|
389
|
+
createTargetDir(targetDir);
|
|
390
|
+
} catch (err) {
|
|
391
|
+
spin.stop("Scaffolding failed");
|
|
392
|
+
return abort(err.message);
|
|
393
|
+
}
|
|
377
394
|
mergeTree(BASE_TEMPLATE, targetDir);
|
|
378
395
|
finalizeGitignore(targetDir);
|
|
379
396
|
finalizeAgentConfigs(targetDir);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-thexjs-app",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Scaffold a new x framework app (npm create thexjs-app@latest).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"@clack/prompts": "^1.7.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@types/node": "^26.
|
|
67
|
-
"tsup": "^
|
|
66
|
+
"@types/node": "^26.6.5",
|
|
67
|
+
"tsup": "^9.0.0"
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Catch-all for every @thexjs/auth route:
|
|
2
|
+
// POST /api/auth/signin/local -- credentials sign-in (form data)
|
|
3
|
+
// POST /api/auth/signout -- sign out
|
|
4
|
+
// GET /api/auth/session -- { user } or 401
|
|
5
|
+
import { auth } from "../../lib/auth";
|
|
6
|
+
|
|
7
|
+
export async function GET(req: Request): Promise<Response> {
|
|
8
|
+
return auth.handleRequest(req);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function POST(req: Request): Promise<Response> {
|
|
12
|
+
return auth.handleRequest(req);
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { auth, createUser } from "../../lib/auth";
|
|
2
|
+
|
|
3
|
+
export async function POST(req: Request): Promise<Response> {
|
|
4
|
+
try {
|
|
5
|
+
const body = (await req.json()) as { email?: string; password?: string; name?: string };
|
|
6
|
+
const { email, password, name } = body;
|
|
7
|
+
|
|
8
|
+
if (!email || !password) {
|
|
9
|
+
return Response.json({ error: "email and password required" }, { status: 400 });
|
|
10
|
+
}
|
|
11
|
+
if (password.length < 8) {
|
|
12
|
+
return Response.json({ error: "password must be at least 8 characters" }, { status: 400 });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const user = await createUser(email, password, name);
|
|
16
|
+
const res = Response.json({ user });
|
|
17
|
+
return auth.setSessionCookie(res, user, "local", req);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
const message = err instanceof Error ? err.message : "Signup failed";
|
|
20
|
+
// createUser's "already exists" check is the only expected failure mode;
|
|
21
|
+
// anything else is a real server error, not a 400 to the client.
|
|
22
|
+
const status = message.includes("already exists") ? 409 : 500;
|
|
23
|
+
return Response.json({ error: status === 409 ? message : "Signup failed" }, { status });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Real auth, backed by @thexjs/auth -- a credentials (email + password)
|
|
2
|
+
// provider against a local `users` table, Argon2id password hashing, and
|
|
3
|
+
// HMAC'd sessions in a SQLite-backed store. See @thexjs/auth's README for
|
|
4
|
+
// OAuth providers, roles/permissions, and swapping in Postgres.
|
|
2
5
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
6
|
+
// Set AUTH_SECRET in production (any stable random string) -- without it,
|
|
7
|
+
// sessions are HMAC'd with a per-process random secret and every restart
|
|
8
|
+
// invalidates them. defineAuth throws on boot if it's missing in production;
|
|
9
|
+
// in development a random one is generated for you automatically.
|
|
7
10
|
|
|
8
11
|
import type { Database } from "bun:sqlite";
|
|
9
|
-
import { randomUUID } from "node:crypto";
|
|
10
12
|
import { mkdirSync } from "node:fs";
|
|
11
13
|
import { dirname, join } from "node:path";
|
|
14
|
+
import {
|
|
15
|
+
type AuthUser,
|
|
16
|
+
createSQLiteSessionStore,
|
|
17
|
+
defineAuth,
|
|
18
|
+
hashPassword,
|
|
19
|
+
verifyPassword,
|
|
20
|
+
} from "@thexjs/auth";
|
|
12
21
|
import { connectSQLite, runSQLiteMigrations } from "@thexjs/core/data";
|
|
13
22
|
|
|
14
|
-
const SESSION_COOKIE = "x_session";
|
|
15
|
-
const SESSION_MAX_AGE = 60 * 60 * 24 * 7;
|
|
16
|
-
|
|
17
|
-
export interface Session {
|
|
18
|
-
id: string;
|
|
19
|
-
token: string;
|
|
20
|
-
user_id: string;
|
|
21
|
-
username: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
23
|
let db: Database | null = null;
|
|
25
24
|
|
|
26
25
|
function getDb(): Database {
|
|
@@ -32,54 +31,60 @@ function getDb(): Database {
|
|
|
32
31
|
return db;
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
database.run("INSERT INTO sessions (id, token, user_id, username) VALUES (?1, ?2, ?3, ?4)", [
|
|
42
|
-
id,
|
|
43
|
-
token,
|
|
44
|
-
userId,
|
|
45
|
-
username,
|
|
46
|
-
] as unknown as string[]);
|
|
47
|
-
|
|
48
|
-
return { id, token, user_id: userId, username };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function getSession(token: string | undefined): Session | null {
|
|
52
|
-
if (!token) return null;
|
|
53
|
-
const database = getDb();
|
|
54
|
-
const row = database
|
|
55
|
-
.prepare("SELECT id, token, user_id, username FROM sessions WHERE token = ?1")
|
|
56
|
-
.get(token) as { id: string; token: string; user_id: string; username: string } | null;
|
|
57
|
-
if (!row) return null;
|
|
58
|
-
return { id: row.id, token: row.token, user_id: row.user_id, username: row.username };
|
|
34
|
+
interface UserRow {
|
|
35
|
+
id: string;
|
|
36
|
+
email: string;
|
|
37
|
+
password_hash: string;
|
|
38
|
+
name: string | null;
|
|
59
39
|
}
|
|
60
40
|
|
|
61
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Creates a user with a hashed password. `defineAuth` itself has no
|
|
43
|
+
* "create user" API -- that's always app-specific -- so this is what
|
|
44
|
+
* `src/api/auth/signup.ts` calls. Throws if the email is already taken.
|
|
45
|
+
*/
|
|
46
|
+
export async function createUser(
|
|
47
|
+
email: string,
|
|
48
|
+
password: string,
|
|
49
|
+
name?: string,
|
|
50
|
+
): Promise<AuthUser> {
|
|
62
51
|
const database = getDb();
|
|
63
|
-
database.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
export function parseSessionCookie(cookieHeader: string | null): string | undefined {
|
|
67
|
-
if (!cookieHeader) return undefined;
|
|
68
|
-
for (const part of cookieHeader.split(";")) {
|
|
69
|
-
const trimmed = part.trim();
|
|
70
|
-
if (trimmed.startsWith(`${SESSION_COOKIE}=`)) {
|
|
71
|
-
return trimmed.slice(SESSION_COOKIE.length + 1);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const SECURE_COOKIE_FLAG = process.env.NODE_ENV === "production" ? "; Secure" : "";
|
|
52
|
+
const existing = database.prepare("SELECT id FROM users WHERE email = ?1").get(email);
|
|
53
|
+
if (existing) throw new Error("An account with that email already exists");
|
|
78
54
|
|
|
79
|
-
|
|
80
|
-
|
|
55
|
+
const id = crypto.randomUUID();
|
|
56
|
+
const passwordHash = await hashPassword(password);
|
|
57
|
+
database.run("INSERT INTO users (id, email, password_hash, name) VALUES (?1, ?2, ?3, ?4)", [
|
|
58
|
+
id,
|
|
59
|
+
email,
|
|
60
|
+
passwordHash,
|
|
61
|
+
name ?? null,
|
|
62
|
+
] as unknown as string[]);
|
|
63
|
+
return name !== undefined ? { id, email, name } : { id, email };
|
|
81
64
|
}
|
|
82
65
|
|
|
83
|
-
export
|
|
84
|
-
|
|
85
|
-
}
|
|
66
|
+
export const auth = defineAuth({
|
|
67
|
+
...(process.env.AUTH_SECRET ? { secret: process.env.AUTH_SECRET } : {}),
|
|
68
|
+
store: createSQLiteSessionStore({ db: getDb() }),
|
|
69
|
+
successRedirect: "/dashboard",
|
|
70
|
+
providers: [
|
|
71
|
+
{
|
|
72
|
+
id: "local",
|
|
73
|
+
name: "Local",
|
|
74
|
+
type: "credentials",
|
|
75
|
+
async authorize({ email, password }) {
|
|
76
|
+
if (!email || !password) return null;
|
|
77
|
+
const database = getDb();
|
|
78
|
+
const row = database
|
|
79
|
+
.prepare("SELECT id, email, password_hash, name FROM users WHERE email = ?1")
|
|
80
|
+
.get(email) as UserRow | null;
|
|
81
|
+
if (!row) return null;
|
|
82
|
+
const valid = await verifyPassword(password, row.password_hash);
|
|
83
|
+
if (!valid) return null;
|
|
84
|
+
return row.name !== null
|
|
85
|
+
? { id: row.id, email: row.email, name: row.name }
|
|
86
|
+
: { id: row.id, email: row.email };
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
});
|
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getSession, parseSessionCookie } from "../../lib/auth";
|
|
1
|
+
import { auth } from "../../lib/auth";
|
|
3
2
|
|
|
4
|
-
export const middleware
|
|
5
|
-
const token = parseSessionCookie(ctx.request.headers.get("Cookie"));
|
|
6
|
-
const session = getSession(token);
|
|
7
|
-
|
|
8
|
-
if (!session) {
|
|
9
|
-
return new Response(null, {
|
|
10
|
-
status: 302,
|
|
11
|
-
headers: { Location: "/login" },
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return next();
|
|
16
|
-
};
|
|
3
|
+
export const middleware = auth.requireAuth({ redirectTo: "/login" });
|
|
@@ -11,10 +11,10 @@ export default function Dashboard() {
|
|
|
11
11
|
</p>
|
|
12
12
|
<form
|
|
13
13
|
method="POST"
|
|
14
|
-
action="/api/auth/
|
|
14
|
+
action="/api/auth/signout"
|
|
15
15
|
onSubmit={(e) => {
|
|
16
16
|
e.preventDefault();
|
|
17
|
-
fetch("/api/auth/
|
|
17
|
+
fetch("/api/auth/signout", { method: "POST" }).then(() => {
|
|
18
18
|
window.location.href = "/login";
|
|
19
19
|
});
|
|
20
20
|
}}
|
|
@@ -9,18 +9,18 @@ export default function Login() {
|
|
|
9
9
|
setError("");
|
|
10
10
|
setLoading(true);
|
|
11
11
|
const form = e.currentTarget;
|
|
12
|
-
const data =
|
|
12
|
+
const data = new FormData();
|
|
13
|
+
data.set("email", form.email.value);
|
|
14
|
+
data.set("password", form.password.value);
|
|
13
15
|
try {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
16
|
+
// @thexjs/auth's credentials sign-in expects form data and redirects
|
|
17
|
+
// (302 -> successRedirect) on success; fetch follows that redirect
|
|
18
|
+
// automatically and returns the final response.
|
|
19
|
+
const res = await fetch("/api/auth/signin/local", { method: "POST", body: data });
|
|
19
20
|
if (res.ok) {
|
|
20
21
|
window.location.href = "/dashboard";
|
|
21
22
|
} else {
|
|
22
|
-
|
|
23
|
-
setError(err.error ?? "Login failed");
|
|
23
|
+
setError(res.status === 429 ? "Too many attempts, try again later" : "Invalid credentials");
|
|
24
24
|
}
|
|
25
25
|
} catch {
|
|
26
26
|
setError("Network error");
|
|
@@ -34,12 +34,12 @@ export default function Login() {
|
|
|
34
34
|
<h1 style={{ fontSize: "1.5rem", marginBottom: "1rem" }}>Sign in</h1>
|
|
35
35
|
<form onSubmit={handleSubmit} style={{ display: "grid", gap: "0.75rem" }}>
|
|
36
36
|
<label style={{ display: "grid", gap: "0.25rem" }}>
|
|
37
|
-
|
|
38
|
-
<input name="
|
|
37
|
+
Email
|
|
38
|
+
<input name="email" type="email" required style={field} />
|
|
39
39
|
</label>
|
|
40
40
|
<label style={{ display: "grid", gap: "0.25rem" }}>
|
|
41
41
|
Password
|
|
42
|
-
<input name="password" type="password"
|
|
42
|
+
<input name="password" type="password" required style={field} />
|
|
43
43
|
</label>
|
|
44
44
|
{error && <p style={{ color: "#dc2626", fontSize: "0.85rem" }}>{error}</p>}
|
|
45
45
|
<button type="submit" disabled={loading} style={submit}>
|
|
@@ -47,7 +47,7 @@ export default function Login() {
|
|
|
47
47
|
</button>
|
|
48
48
|
</form>
|
|
49
49
|
<p style={{ fontSize: "0.8rem", color: "#64748b", marginTop: "1rem" }}>
|
|
50
|
-
|
|
50
|
+
No account yet? <a href="/signup">Sign up</a>
|
|
51
51
|
</p>
|
|
52
52
|
</main>
|
|
53
53
|
);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
3
|
+
export default function Signup() {
|
|
4
|
+
const [error, setError] = useState("");
|
|
5
|
+
const [loading, setLoading] = useState(false);
|
|
6
|
+
|
|
7
|
+
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
|
8
|
+
e.preventDefault();
|
|
9
|
+
setError("");
|
|
10
|
+
setLoading(true);
|
|
11
|
+
const form = e.currentTarget;
|
|
12
|
+
// HTMLFormElement already has its own `name` string property (the
|
|
13
|
+
// form's own name attribute), which shadows a named-input lookup for a
|
|
14
|
+
// field also called "name" -- go through `elements` explicitly for it.
|
|
15
|
+
const nameField = form.elements.namedItem("name") as HTMLInputElement;
|
|
16
|
+
const data = {
|
|
17
|
+
email: form.email.value,
|
|
18
|
+
password: form.password.value,
|
|
19
|
+
name: nameField.value || undefined,
|
|
20
|
+
};
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch("/api/auth/signup", {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: { "Content-Type": "application/json" },
|
|
25
|
+
body: JSON.stringify(data),
|
|
26
|
+
});
|
|
27
|
+
if (res.ok) {
|
|
28
|
+
window.location.href = "/dashboard";
|
|
29
|
+
} else {
|
|
30
|
+
const err = (await res.json()) as { error?: string };
|
|
31
|
+
setError(err.error ?? "Signup failed");
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
setError("Network error");
|
|
35
|
+
} finally {
|
|
36
|
+
setLoading(false);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<main style={{ maxWidth: 360, margin: "0 auto", padding: "4rem 1.5rem" }}>
|
|
42
|
+
<h1 style={{ fontSize: "1.5rem", marginBottom: "1rem" }}>Create an account</h1>
|
|
43
|
+
<form onSubmit={handleSubmit} style={{ display: "grid", gap: "0.75rem" }}>
|
|
44
|
+
<label style={{ display: "grid", gap: "0.25rem" }}>
|
|
45
|
+
Name
|
|
46
|
+
<input name="name" style={field} />
|
|
47
|
+
</label>
|
|
48
|
+
<label style={{ display: "grid", gap: "0.25rem" }}>
|
|
49
|
+
Email
|
|
50
|
+
<input name="email" type="email" required style={field} />
|
|
51
|
+
</label>
|
|
52
|
+
<label style={{ display: "grid", gap: "0.25rem" }}>
|
|
53
|
+
Password
|
|
54
|
+
<input name="password" type="password" required minLength={8} style={field} />
|
|
55
|
+
</label>
|
|
56
|
+
{error && <p style={{ color: "#dc2626", fontSize: "0.85rem" }}>{error}</p>}
|
|
57
|
+
<button type="submit" disabled={loading} style={submit}>
|
|
58
|
+
{loading ? "Creating account..." : "Sign up"}
|
|
59
|
+
</button>
|
|
60
|
+
</form>
|
|
61
|
+
<p style={{ fontSize: "0.8rem", color: "#64748b", marginTop: "1rem" }}>
|
|
62
|
+
Already have an account? <a href="/login">Sign in</a>
|
|
63
|
+
</p>
|
|
64
|
+
</main>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const field: React.CSSProperties = {
|
|
69
|
+
padding: "0.6rem 0.75rem",
|
|
70
|
+
border: "1px solid #cbd5e1",
|
|
71
|
+
borderRadius: 8,
|
|
72
|
+
fontSize: "0.95rem",
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const submit: React.CSSProperties = {
|
|
76
|
+
padding: "0.65rem 1rem",
|
|
77
|
+
background: "#0f172a",
|
|
78
|
+
color: "#fff",
|
|
79
|
+
border: "none",
|
|
80
|
+
borderRadius: 8,
|
|
81
|
+
fontSize: "0.95rem",
|
|
82
|
+
fontWeight: 600,
|
|
83
|
+
cursor: "pointer",
|
|
84
|
+
};
|
package/templates/base/AGENTS.md
CHANGED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { createSession, setSessionCookie } from "../../lib/auth";
|
|
2
|
-
|
|
3
|
-
export async function POST(req: Request): Promise<Response> {
|
|
4
|
-
try {
|
|
5
|
-
const body = (await req.json()) as { username?: string; password?: string };
|
|
6
|
-
const { username, password } = body;
|
|
7
|
-
|
|
8
|
-
if (!username || !password) {
|
|
9
|
-
return Response.json({ error: "username and password required" }, { status: 400 });
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (username !== "admin" || password !== "admin") {
|
|
13
|
-
return Response.json({ error: "invalid credentials" }, { status: 401 });
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const session = createSession(username);
|
|
17
|
-
return new Response(JSON.stringify({ user: { username: session.username } }), {
|
|
18
|
-
status: 200,
|
|
19
|
-
headers: {
|
|
20
|
-
"Content-Type": "application/json",
|
|
21
|
-
"Set-Cookie": setSessionCookie(session.token),
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
} catch (err) {
|
|
25
|
-
console.error("[auth] login error:", err);
|
|
26
|
-
return Response.json({ error: "internal error" }, { status: 500 });
|
|
27
|
-
}
|
|
28
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { clearSessionCookie, deleteSession, parseSessionCookie } from "../../lib/auth";
|
|
2
|
-
|
|
3
|
-
export async function POST(req: Request): Promise<Response> {
|
|
4
|
-
const token = parseSessionCookie(req.headers.get("Cookie"));
|
|
5
|
-
if (token) {
|
|
6
|
-
deleteSession(token);
|
|
7
|
-
}
|
|
8
|
-
return new Response("OK", {
|
|
9
|
-
status: 200,
|
|
10
|
-
headers: { "Set-Cookie": clearSessionCookie() },
|
|
11
|
-
});
|
|
12
|
-
}
|