@batijs/cli 0.0.259 → 0.0.261
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/dist/boilerplates/@batijs/authjs/files/$package.json.js +1 -1
- package/dist/boilerplates/@batijs/authjs/files/server/authjs-handler.ts +2 -2
- package/dist/boilerplates/@batijs/authjs/types/server/authjs-handler.d.ts +2 -3
- package/dist/boilerplates/@batijs/aws/files/$README.md.js +109 -0
- package/dist/boilerplates/@batijs/aws/files/$package.json.js +138 -0
- package/dist/boilerplates/@batijs/aws/files/$tsconfig.json.js +15 -0
- package/dist/boilerplates/@batijs/aws/files/cdk/$stack-name-suffix.json.js +19 -0
- package/dist/boilerplates/@batijs/aws/files/cdk/bin/infrastructure.ts +85 -0
- package/dist/boilerplates/@batijs/aws/files/cdk/lib/vike-stack.ts +186 -0
- package/dist/boilerplates/@batijs/aws/files/cdk.json +72 -0
- package/dist/boilerplates/@batijs/aws/files/tests/aws_handler.spec.ts +116 -0
- package/dist/boilerplates/@batijs/aws/files/vitest.config.ts +8 -0
- package/dist/boilerplates/@batijs/aws/types/cdk/bin/infrastructure.d.ts +11 -0
- package/dist/boilerplates/@batijs/aws/types/cdk/lib/vike-stack.d.ts +13 -0
- package/dist/boilerplates/@batijs/aws/types/vitest.config.d.ts +2 -0
- package/dist/boilerplates/@batijs/cloudflare/files/$package.json.js +1 -1
- package/dist/boilerplates/@batijs/cloudflare/files/$tsconfig.json.js +10 -0
- package/dist/boilerplates/@batijs/d1/files/$README.md.js +0 -4
- package/dist/boilerplates/@batijs/d1/files/$package.json.js +96 -0
- package/dist/boilerplates/@batijs/d1/files/$wrangler.toml.js +1 -0
- package/dist/boilerplates/@batijs/d1/files/database/d1/helpers.ts +17 -0
- package/dist/boilerplates/@batijs/d1/files/vike.d.ts +8 -0
- package/dist/boilerplates/@batijs/d1/types/database/d1/helpers.d.ts +6 -0
- package/dist/boilerplates/@batijs/d1-sqlite/files/$package.json.js +10 -0
- package/dist/boilerplates/@batijs/d1-sqlite/files/database/d1/queries/lucia-auth.ts +28 -0
- package/dist/boilerplates/@batijs/d1-sqlite/files/database/d1/queries/todos.ts +10 -0
- package/dist/boilerplates/@batijs/d1-sqlite/files/database/migrations/lucia-auth.sql +20 -0
- package/dist/boilerplates/@batijs/d1-sqlite/files/database/migrations/todos.sql +4 -0
- package/dist/boilerplates/@batijs/d1-sqlite/types/database/d1/queries/lucia-auth.d.ts +5 -0
- package/dist/boilerplates/@batijs/d1-sqlite/types/database/d1/queries/todos.d.ts +6 -0
- package/dist/boilerplates/@batijs/drizzle/files/$.env.js +1 -0
- package/dist/boilerplates/@batijs/drizzle/files/$README.md.js +0 -3
- package/dist/boilerplates/@batijs/drizzle/files/$package.json.js +7 -14
- package/dist/boilerplates/@batijs/drizzle/files/database/drizzle/db.ts +10 -12
- package/dist/boilerplates/@batijs/drizzle/files/database/drizzle/queries/lucia-auth.ts +38 -9
- package/dist/boilerplates/@batijs/drizzle/files/database/drizzle/queries/todos.ts +16 -5
- package/dist/boilerplates/@batijs/drizzle/files/drizzle.config.ts +7 -4
- package/dist/boilerplates/@batijs/drizzle/types/database/drizzle/db.d.ts +2 -2
- package/dist/boilerplates/@batijs/drizzle/types/database/drizzle/queries/lucia-auth.d.ts +19 -6
- package/dist/boilerplates/@batijs/drizzle/types/database/drizzle/queries/todos.d.ts +9 -2
- package/dist/boilerplates/@batijs/eslint/files/$package.json.js +1 -1
- package/dist/boilerplates/@batijs/eslint/files/eslint.config.js +2 -1
- package/dist/boilerplates/@batijs/express/files/$package.json.js +2 -1
- package/dist/boilerplates/@batijs/express/files/express-entry.ts +13 -12
- package/dist/boilerplates/@batijs/fastify/files/$package.json.js +2 -1
- package/dist/boilerplates/@batijs/fastify/files/fastify-entry.ts +17 -24
- package/dist/boilerplates/@batijs/firebase-auth/files/$package.json.js +2 -2
- package/dist/boilerplates/@batijs/firebase-auth/files/server/firebase-auth-middleware.ts +2 -2
- package/dist/boilerplates/@batijs/firebase-auth/types/server/firebase-auth-middleware.d.ts +2 -6
- package/dist/boilerplates/@batijs/h3/files/$package.json.js +1 -0
- package/dist/boilerplates/@batijs/h3/files/h3-entry.ts +12 -2
- package/dist/boilerplates/@batijs/hattip/files/$package.json.js +15 -2
- package/dist/boilerplates/@batijs/hattip/files/entry_aws_lambda.ts +37 -0
- package/dist/boilerplates/@batijs/hattip/files/hattip-entry.ts +12 -12
- package/dist/boilerplates/@batijs/hattip/types/entry_aws_lambda.d.ts +2 -0
- package/dist/boilerplates/@batijs/hono/files/$package.json.js +22 -4
- package/dist/boilerplates/@batijs/hono/files/entry_aws_lambda.ts +37 -0
- package/dist/boilerplates/@batijs/hono/files/hono-entry.ts +12 -12
- package/dist/boilerplates/@batijs/hono/types/entry_aws_lambda.d.ts +3 -0
- package/dist/boilerplates/@batijs/lucia-auth/files/$package.json.js +4 -1
- package/dist/boilerplates/@batijs/lucia-auth/files/lib/lucia-auth.ts +75 -39
- package/dist/boilerplates/@batijs/lucia-auth/files/server/lucia-auth-handlers.ts +77 -33
- package/dist/boilerplates/@batijs/lucia-auth/types/lib/lucia-auth.d.ts +30 -10
- package/dist/boilerplates/@batijs/lucia-auth/types/server/lucia-auth-handlers.d.ts +13 -13
- package/dist/boilerplates/@batijs/react-lucia-auth/files/pages/login/+Page.tsx +2 -1
- package/dist/boilerplates/@batijs/shared/files/vite.config.ts +6 -0
- package/dist/boilerplates/@batijs/shared-db/files/server/db-middleware.ts +37 -0
- package/dist/boilerplates/@batijs/shared-db/types/server/db-middleware.d.ts +17 -0
- package/dist/boilerplates/@batijs/shared-no-db/files/$package.json.js +2 -1
- package/dist/boilerplates/@batijs/shared-server/files/$package.json.js +5 -2
- package/dist/boilerplates/@batijs/shared-server/files/server/create-todo-handler.ts +22 -4
- package/dist/boilerplates/@batijs/shared-server/files/server/vike-handler.ts +2 -2
- package/dist/boilerplates/@batijs/shared-server/types/server/create-todo-handler.d.ts +19 -1
- package/dist/boilerplates/@batijs/shared-todo/files/pages/todo/+data.ts +10 -4
- package/dist/boilerplates/@batijs/shared-todo/files/vike.d.ts +19 -0
- package/dist/boilerplates/@batijs/shared-todo/types/pages/todo/+data.d.ts +2 -1
- package/dist/boilerplates/@batijs/solid-lucia-auth/files/pages/login/+Page.tsx +2 -1
- package/dist/boilerplates/@batijs/sqlite/files/$.env.js +1 -0
- package/dist/boilerplates/@batijs/sqlite/files/$package.json.js +10 -1
- package/dist/boilerplates/@batijs/sqlite/files/database/sqlite/queries/lucia-auth.ts +16 -15
- package/dist/boilerplates/@batijs/sqlite/files/database/sqlite/queries/todos.ts +5 -5
- package/dist/boilerplates/@batijs/sqlite/types/database/sqlite/queries/lucia-auth.d.ts +5 -5
- package/dist/boilerplates/@batijs/sqlite/types/database/sqlite/queries/todos.d.ts +3 -2
- package/dist/boilerplates/@batijs/telefunc/files/$package.json.js +5 -3
- package/dist/boilerplates/@batijs/telefunc/files/pages/todo/TodoList.telefunc.ts +10 -3
- package/dist/boilerplates/@batijs/telefunc/files/server/telefunc-handler.ts +15 -2
- package/dist/boilerplates/@batijs/telefunc/files/vike.d.ts +20 -0
- package/dist/boilerplates/@batijs/trpc/files/$package.json.js +10 -7
- package/dist/boilerplates/@batijs/trpc/files/server/trpc-handler.ts +22 -0
- package/dist/boilerplates/@batijs/trpc/files/trpc/server.ts +20 -4
- package/dist/boilerplates/@batijs/trpc/types/server/trpc-handler.d.ts +1 -0
- package/dist/boilerplates/@batijs/trpc/types/trpc/client.d.ts +36 -4
- package/dist/boilerplates/@batijs/trpc/types/trpc/server.d.ts +75 -8
- package/dist/boilerplates/@batijs/ts-rest/files/$package.json.js +4 -3
- package/dist/boilerplates/@batijs/ts-rest/files/server/ts-rest-handler.ts +48 -27
- package/dist/boilerplates/@batijs/vue-lucia-auth/files/pages/login/+Page.vue +2 -1
- package/dist/boilerplates/boilerplates.json +61 -3
- package/dist/index.js +6 -1
- package/package.json +5 -5
- package/dist/boilerplates/@batijs/drizzle/files/database/drizzle/seed.ts +0 -13
- package/dist/boilerplates/@batijs/trpc/files/$vite.config.ts.js +0 -13
- package/dist/boilerplates/@batijs/trpc/files/trpc/vite-plugin.ts +0 -49
- package/dist/boilerplates/@batijs/trpc/types/trpc/vite-plugin.d.ts +0 -12
- /package/dist/boilerplates/@batijs/{drizzle/types/database/drizzle/seed.d.ts → aws/types/tests/aws_handler.spec.d.ts} +0 -0
|
@@ -13,21 +13,29 @@ import {
|
|
|
13
13
|
luciaAuthLogoutHandler,
|
|
14
14
|
luciaAuthSignupHandler,
|
|
15
15
|
luciaCsrfMiddleware,
|
|
16
|
+
luciaDbMiddleware,
|
|
16
17
|
luciaGithubCallbackHandler,
|
|
17
18
|
luciaGithubLoginHandler,
|
|
18
19
|
} from "@batijs/lucia-auth/server/lucia-auth-handlers";
|
|
19
20
|
import { createTodoHandler } from "@batijs/shared-server/server/create-todo-handler";
|
|
20
21
|
import { vikeHandler } from "@batijs/shared-server/server/vike-handler";
|
|
21
22
|
import { telefuncHandler } from "@batijs/telefunc/server/telefunc-handler";
|
|
22
|
-
import { appRouter } from "@batijs/trpc/trpc/server";
|
|
23
23
|
import { tsRestHandler } from "@batijs/ts-rest/server/ts-rest-handler";
|
|
24
|
-
import { type FetchCreateContextFnOptions, fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
|
25
24
|
import { Hono } from "hono";
|
|
26
25
|
import { handle } from "hono/vercel";
|
|
27
26
|
import { createHandler, createMiddleware } from "@universal-middleware/hono";
|
|
27
|
+
import { dbMiddleware } from "@batijs/shared-db/server/db-middleware";
|
|
28
|
+
import { trpcHandler } from "@batijs/trpc/server/trpc-handler";
|
|
28
29
|
|
|
29
30
|
const app = new Hono();
|
|
30
31
|
|
|
32
|
+
if (BATI.hasDatabase) {
|
|
33
|
+
/**
|
|
34
|
+
* Make database available in Context as `context.db`
|
|
35
|
+
*/
|
|
36
|
+
app.use(createMiddleware(dbMiddleware)());
|
|
37
|
+
}
|
|
38
|
+
|
|
31
39
|
if (BATI.has("authjs") || BATI.has("auth0")) {
|
|
32
40
|
/**
|
|
33
41
|
* Append Auth.js session to context
|
|
@@ -48,6 +56,7 @@ if (BATI.has("firebase-auth")) {
|
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
if (BATI.has("lucia-auth")) {
|
|
59
|
+
app.use(createMiddleware(luciaDbMiddleware)());
|
|
51
60
|
app.use(createMiddleware(luciaCsrfMiddleware)());
|
|
52
61
|
app.use(createMiddleware(luciaAuthContextMiddleware)());
|
|
53
62
|
app.use(createMiddleware(luciaAuthCookieMiddleware)());
|
|
@@ -65,16 +74,7 @@ if (BATI.has("trpc")) {
|
|
|
65
74
|
*
|
|
66
75
|
* @link {@see https://trpc.io/docs/server/adapters}
|
|
67
76
|
**/
|
|
68
|
-
app.use("/api/trpc/*", (
|
|
69
|
-
return fetchRequestHandler({
|
|
70
|
-
endpoint: "/api/trpc",
|
|
71
|
-
req: c.req.raw,
|
|
72
|
-
router: appRouter,
|
|
73
|
-
createContext({ req, resHeaders }): FetchCreateContextFnOptions {
|
|
74
|
-
return { req, resHeaders };
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
});
|
|
77
|
+
app.use("/api/trpc/*", createHandler(trpcHandler)("/api/trpc"));
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
if (BATI.has("telefunc")) {
|
|
@@ -43,13 +43,16 @@ var require_package = __commonJS({
|
|
|
43
43
|
devDependencies: {
|
|
44
44
|
"@batijs/compile": "workspace:*",
|
|
45
45
|
"@batijs/drizzle": "workspace:*",
|
|
46
|
+
"@batijs/d1": "workspace:*",
|
|
47
|
+
"@batijs/d1-sqlite": "workspace:*",
|
|
46
48
|
"@batijs/sqlite": "workspace:*",
|
|
49
|
+
"@cloudflare/workers-types": "^4.20240821.1",
|
|
47
50
|
"@lucia-auth/adapter-drizzle": "^1.1.0",
|
|
48
51
|
"@lucia-auth/adapter-sqlite": "^3.0.2",
|
|
49
52
|
"@types/better-sqlite3": "^7.6.11",
|
|
50
53
|
"@types/cookie": "^0.6.0",
|
|
51
54
|
"@types/node": "^18.19.14",
|
|
52
|
-
"@universal-middleware/core": "^0.2.
|
|
55
|
+
"@universal-middleware/core": "^0.2.6",
|
|
53
56
|
arctic: "^1.9.2",
|
|
54
57
|
"better-sqlite3": "^11.2.1",
|
|
55
58
|
cookie: "^0.6.0",
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import "dotenv/config";
|
|
2
|
-
import { Lucia } from "lucia";
|
|
3
|
-
import { BetterSqlite3Adapter } from "@lucia-auth/adapter-sqlite";
|
|
2
|
+
import { Lucia, type Register } from "lucia";
|
|
3
|
+
import { BetterSqlite3Adapter, D1Adapter } from "@lucia-auth/adapter-sqlite";
|
|
4
4
|
import { GitHub } from "arctic";
|
|
5
5
|
import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
|
|
6
|
-
import {
|
|
7
|
-
import { sessionTable, userTable } from "@batijs/drizzle/database/drizzle/schema/lucia-auth";
|
|
6
|
+
import { dbD1, dbSqlite } from "@batijs/drizzle/database/drizzle/db";
|
|
8
7
|
import { db as sqliteDb } from "@batijs/sqlite/database/sqlite/db";
|
|
8
|
+
import { sessionTable, userTable } from "@batijs/drizzle/database/drizzle/schema/lucia-auth";
|
|
9
|
+
import { D1Database } from "@cloudflare/workers-types";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Polyfill needed if you're using Node.js 18 or below
|
|
@@ -20,40 +21,57 @@ if (!globalThis.crypto) {
|
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
export function initializeLucia(
|
|
25
|
+
db: BATI.If<
|
|
26
|
+
{
|
|
27
|
+
'BATI.has("sqlite") && !BATI.hasD1': ReturnType<typeof sqliteDb>;
|
|
28
|
+
'BATI.has("drizzle") && !BATI.hasD1': ReturnType<typeof dbSqlite>;
|
|
29
|
+
'BATI.has("drizzle")': ReturnType<typeof dbD1>;
|
|
30
|
+
"BATI.hasD1": D1Database;
|
|
31
|
+
},
|
|
32
|
+
"union"
|
|
33
|
+
>,
|
|
34
|
+
) {
|
|
35
|
+
/**
|
|
36
|
+
* Database setup
|
|
37
|
+
*
|
|
38
|
+
* @link {@see https://lucia-auth.com/database/#database-setup}
|
|
39
|
+
**/
|
|
40
|
+
const adapter = BATI.has("drizzle")
|
|
41
|
+
? new DrizzleSQLiteAdapter(db as BATI.Any, sessionTable, userTable)
|
|
42
|
+
: BATI.hasD1
|
|
43
|
+
? new D1Adapter(db as BATI.Any, {
|
|
44
|
+
user: "users",
|
|
45
|
+
session: "sessions",
|
|
46
|
+
})
|
|
47
|
+
: new BetterSqlite3Adapter(db as BATI.Any, {
|
|
48
|
+
user: "users",
|
|
49
|
+
session: "sessions",
|
|
50
|
+
});
|
|
34
51
|
|
|
35
|
-
/**
|
|
36
|
-
* Initialize Lucia
|
|
37
|
-
*
|
|
38
|
-
* @link {@see https://lucia-auth.com/getting-started/#initialize-lucia}
|
|
39
|
-
*/
|
|
40
|
-
export const lucia = new Lucia(adapter, {
|
|
41
52
|
/**
|
|
42
|
-
* Lucia
|
|
53
|
+
* Initialize Lucia
|
|
43
54
|
*
|
|
44
|
-
* @link {@see https://lucia-auth.com/
|
|
55
|
+
* @link {@see https://lucia-auth.com/getting-started/#initialize-lucia}
|
|
45
56
|
*/
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
return new Lucia(adapter, {
|
|
58
|
+
/**
|
|
59
|
+
* Lucia Configuration
|
|
60
|
+
*
|
|
61
|
+
* @link {@see https://lucia-auth.com/basics/configuration}
|
|
62
|
+
*/
|
|
63
|
+
sessionCookie: {
|
|
64
|
+
attributes: {
|
|
65
|
+
secure: process.env.NODE_ENV === "production",
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
getUserAttributes: (attributes) => {
|
|
69
|
+
return {
|
|
70
|
+
username: attributes.username,
|
|
71
|
+
};
|
|
49
72
|
},
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
username: attributes.username,
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
57
75
|
|
|
58
76
|
/**
|
|
59
77
|
* Initialize OAuth provider
|
|
@@ -69,25 +87,43 @@ export const github = new GitHub(process.env.GITHUB_CLIENT_ID as string, process
|
|
|
69
87
|
*/
|
|
70
88
|
declare module "lucia" {
|
|
71
89
|
interface Register {
|
|
72
|
-
Lucia: typeof
|
|
90
|
+
Lucia: ReturnType<typeof initializeLucia>;
|
|
73
91
|
DatabaseUserAttributes: Omit<DatabaseUser, "id">;
|
|
74
92
|
}
|
|
75
93
|
}
|
|
76
94
|
|
|
95
|
+
declare global {
|
|
96
|
+
namespace Universal {
|
|
97
|
+
interface Context {
|
|
98
|
+
lucia: Register["Lucia"];
|
|
99
|
+
db: BATI.If<{
|
|
100
|
+
'BATI.has("sqlite") && !BATI.hasD1': ReturnType<typeof sqliteDb>;
|
|
101
|
+
'BATI.has("drizzle") && !BATI.hasD1': ReturnType<typeof dbSqlite>;
|
|
102
|
+
'BATI.has("drizzle")': ReturnType<typeof dbD1>;
|
|
103
|
+
"BATI.hasD1": D1Database;
|
|
104
|
+
}>;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
77
109
|
export interface DatabaseUser {
|
|
78
110
|
id: string;
|
|
79
111
|
username: string;
|
|
80
|
-
password?: string;
|
|
112
|
+
password?: string | null;
|
|
81
113
|
}
|
|
82
114
|
|
|
115
|
+
//# !BATI.has("drizzle")
|
|
83
116
|
export interface DatabaseOAuthAccount {
|
|
84
117
|
provider_id: string;
|
|
85
118
|
provider_user_id: string;
|
|
86
|
-
/*{ @if (it.BATI.has("drizzle")) }*/
|
|
87
|
-
userId: string;
|
|
88
|
-
/*{ #else }*/
|
|
89
119
|
user_id: string;
|
|
90
|
-
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
//# BATI.has("drizzle")
|
|
123
|
+
export interface DatabaseOAuthAccount {
|
|
124
|
+
providerId: string;
|
|
125
|
+
providerUserId: string;
|
|
126
|
+
userId: string;
|
|
91
127
|
}
|
|
92
128
|
|
|
93
129
|
export interface GitHubUser {
|
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
import type { Session, User } from "lucia";
|
|
2
2
|
import { generateId, Scrypt, verifyRequestOrigin } from "lucia";
|
|
3
|
-
import
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
type DatabaseOAuthAccount,
|
|
5
|
+
type DatabaseUser,
|
|
6
|
+
github,
|
|
7
|
+
type GitHubUser,
|
|
8
|
+
initializeLucia,
|
|
9
|
+
} from "../lib/lucia-auth";
|
|
5
10
|
import { SqliteError } from "better-sqlite3";
|
|
6
11
|
import { generateState, OAuth2RequestError } from "arctic";
|
|
7
12
|
import { parse, serialize } from "cookie";
|
|
8
13
|
import * as drizzleQueries from "@batijs/drizzle/database/drizzle/queries/lucia-auth";
|
|
9
14
|
import * as sqliteQueries from "@batijs/sqlite/database/sqlite/queries/lucia-auth";
|
|
10
|
-
import
|
|
15
|
+
import * as d1Queries from "@batijs/d1-sqlite/database/d1/queries/lucia-auth";
|
|
16
|
+
import { type Get, type UniversalHandler, type UniversalMiddleware } from "@universal-middleware/core";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Add lucia database to the context
|
|
20
|
+
*
|
|
21
|
+
* @link {@see https://universal-middleware.dev/examples/context-middleware}
|
|
22
|
+
*/
|
|
23
|
+
export const luciaDbMiddleware: Get<[], UniversalMiddleware> = () => async (_request, context, _runtime) => {
|
|
24
|
+
const lucia = initializeLucia(context.db);
|
|
25
|
+
return {
|
|
26
|
+
...context,
|
|
27
|
+
lucia,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
11
30
|
|
|
12
31
|
/**
|
|
13
32
|
* CSRF protection middleware
|
|
@@ -35,8 +54,8 @@ export const luciaCsrfMiddleware = (() => async (request) => {
|
|
|
35
54
|
*
|
|
36
55
|
* @link {@see https://lucia-auth.com/guides/validate-session-cookies/}
|
|
37
56
|
*/
|
|
38
|
-
export const luciaAuthContextMiddleware = (
|
|
39
|
-
const sessionId = lucia.readSessionCookie(request.headers.get("cookie") ?? "");
|
|
57
|
+
export const luciaAuthContextMiddleware: Get<[], UniversalMiddleware> = () => async (request, context) => {
|
|
58
|
+
const sessionId = context.lucia.readSessionCookie(request.headers.get("cookie") ?? "");
|
|
40
59
|
|
|
41
60
|
if (!sessionId) {
|
|
42
61
|
return {
|
|
@@ -45,7 +64,7 @@ export const luciaAuthContextMiddleware = (() => async (request, context) => {
|
|
|
45
64
|
user: null,
|
|
46
65
|
};
|
|
47
66
|
} else {
|
|
48
|
-
const { session, user } = await lucia.validateSession(sessionId);
|
|
67
|
+
const { session, user } = await context.lucia.validateSession(sessionId);
|
|
49
68
|
|
|
50
69
|
return {
|
|
51
70
|
...context,
|
|
@@ -54,7 +73,7 @@ export const luciaAuthContextMiddleware = (() => async (request, context) => {
|
|
|
54
73
|
user,
|
|
55
74
|
};
|
|
56
75
|
}
|
|
57
|
-
}
|
|
76
|
+
};
|
|
58
77
|
|
|
59
78
|
/**
|
|
60
79
|
* Set Set-Cookie headers if in context
|
|
@@ -62,22 +81,25 @@ export const luciaAuthContextMiddleware = (() => async (request, context) => {
|
|
|
62
81
|
export const luciaAuthCookieMiddleware = (() => (_request, context) => {
|
|
63
82
|
return (response: Response) => {
|
|
64
83
|
if (context.session?.fresh) {
|
|
65
|
-
response.headers.append("Set-Cookie", lucia.createSessionCookie(context.session.id).serialize());
|
|
84
|
+
response.headers.append("Set-Cookie", context.lucia.createSessionCookie(context.session.id).serialize());
|
|
66
85
|
}
|
|
67
86
|
if (context.sessionId && !context.session) {
|
|
68
|
-
response.headers.append("Set-Cookie", lucia.createBlankSessionCookie().serialize());
|
|
87
|
+
response.headers.append("Set-Cookie", context.lucia.createBlankSessionCookie().serialize());
|
|
69
88
|
}
|
|
70
89
|
|
|
71
90
|
return response;
|
|
72
91
|
};
|
|
73
|
-
}) satisfies Get<
|
|
92
|
+
}) satisfies Get<
|
|
93
|
+
[],
|
|
94
|
+
UniversalMiddleware<Universal.Context & { session?: Session | null; user?: User | null; sessionId?: string | null }>
|
|
95
|
+
>;
|
|
74
96
|
|
|
75
97
|
/**
|
|
76
98
|
* Register user handler
|
|
77
99
|
*
|
|
78
100
|
* @link {@see https://lucia-auth.com/guides/email-and-password/basics#register-user}
|
|
79
101
|
*/
|
|
80
|
-
export const luciaAuthSignupHandler = (() => async (request) => {
|
|
102
|
+
export const luciaAuthSignupHandler = (() => async (request, context, _runtime) => {
|
|
81
103
|
const body = (await request.json()) as { username: string; password: string };
|
|
82
104
|
const username = body.username ?? "";
|
|
83
105
|
const password = body.password ?? "";
|
|
@@ -106,15 +128,21 @@ export const luciaAuthSignupHandler = (() => async (request) => {
|
|
|
106
128
|
const userId = generateId(15);
|
|
107
129
|
|
|
108
130
|
try {
|
|
109
|
-
(BATI.has("drizzle")
|
|
131
|
+
if (BATI.has("drizzle")) {
|
|
132
|
+
await drizzleQueries.signupWithCredentials(context.db, userId, username, passwordHash);
|
|
133
|
+
} else if (BATI.has("sqlite") && !BATI.hasD1) {
|
|
134
|
+
sqliteQueries.signupWithCredentials(context.db, userId, username, passwordHash);
|
|
135
|
+
} else if (BATI.hasD1) {
|
|
136
|
+
await d1Queries.signupWithCredentials(context.db, userId, username, passwordHash);
|
|
137
|
+
}
|
|
110
138
|
|
|
111
|
-
const session = await lucia.createSession(userId, {});
|
|
139
|
+
const session = await context.lucia.createSession(userId, {});
|
|
112
140
|
|
|
113
141
|
return new Response(JSON.stringify({ status: "success" }), {
|
|
114
142
|
status: 200,
|
|
115
143
|
headers: {
|
|
116
144
|
"content-type": "application/json",
|
|
117
|
-
"set-cookie": lucia.createSessionCookie(session.id).serialize(),
|
|
145
|
+
"set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
|
|
118
146
|
},
|
|
119
147
|
});
|
|
120
148
|
} catch (error) {
|
|
@@ -141,7 +169,7 @@ export const luciaAuthSignupHandler = (() => async (request) => {
|
|
|
141
169
|
*
|
|
142
170
|
* @link {@see https://lucia-auth.com/guides/email-and-password/basics#sign-in-user}
|
|
143
171
|
*/
|
|
144
|
-
export const luciaAuthLoginHandler = (() => async (request) => {
|
|
172
|
+
export const luciaAuthLoginHandler = (() => async (request, context, _runtime) => {
|
|
145
173
|
const body = (await request.json()) as { username: string; password: string };
|
|
146
174
|
const username = body.username ?? "";
|
|
147
175
|
const password = body.password ?? "";
|
|
@@ -157,9 +185,14 @@ export const luciaAuthLoginHandler = (() => async (request) => {
|
|
|
157
185
|
});
|
|
158
186
|
}
|
|
159
187
|
|
|
160
|
-
const existingUser =
|
|
161
|
-
|
|
162
|
-
|
|
188
|
+
const existingUser: DatabaseUser | undefined | null = BATI.has("drizzle")
|
|
189
|
+
? await drizzleQueries.getExistingUser(context.db, username)
|
|
190
|
+
: BATI.has("sqlite") && !BATI.hasD1
|
|
191
|
+
? sqliteQueries.getExistingUser<DatabaseUser>(context.db, username)
|
|
192
|
+
: BATI.hasD1
|
|
193
|
+
? await d1Queries.getExistingUser<DatabaseUser>(context.db, username)
|
|
194
|
+
: undefined;
|
|
195
|
+
|
|
163
196
|
if (!existingUser) {
|
|
164
197
|
return new Response(JSON.stringify({ error: { invalid: "Incorrect username or password" } }), {
|
|
165
198
|
status: 422,
|
|
@@ -181,13 +214,13 @@ export const luciaAuthLoginHandler = (() => async (request) => {
|
|
|
181
214
|
});
|
|
182
215
|
}
|
|
183
216
|
|
|
184
|
-
const session = await lucia.createSession(existingUser.id, {});
|
|
217
|
+
const session = await context.lucia.createSession(existingUser.id, {});
|
|
185
218
|
|
|
186
219
|
return new Response(JSON.stringify({ status: "success" }), {
|
|
187
220
|
status: 200,
|
|
188
221
|
headers: {
|
|
189
222
|
"content-type": "application/json",
|
|
190
|
-
"set-cookie": lucia.createSessionCookie(session.id).serialize(),
|
|
223
|
+
"set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
|
|
191
224
|
},
|
|
192
225
|
});
|
|
193
226
|
}) satisfies Get<[], UniversalMiddleware>;
|
|
@@ -208,7 +241,7 @@ export const luciaAuthLogoutHandler = (() => async (_request, context) => {
|
|
|
208
241
|
*
|
|
209
242
|
* @link {@see https://lucia-auth.com/basics/sessions#invalidate-sessions}
|
|
210
243
|
*/
|
|
211
|
-
await lucia.invalidateSession(session.id);
|
|
244
|
+
await context.lucia.invalidateSession(session.id);
|
|
212
245
|
|
|
213
246
|
/**
|
|
214
247
|
* Delete session cookie
|
|
@@ -218,10 +251,10 @@ export const luciaAuthLogoutHandler = (() => async (_request, context) => {
|
|
|
218
251
|
return new Response(JSON.stringify({ status: "success" }), {
|
|
219
252
|
status: 200,
|
|
220
253
|
headers: {
|
|
221
|
-
"set-cookie": lucia.createBlankSessionCookie().serialize(),
|
|
254
|
+
"set-cookie": context.lucia.createBlankSessionCookie().serialize(),
|
|
222
255
|
},
|
|
223
256
|
});
|
|
224
|
-
}) satisfies Get<[], UniversalMiddleware<{ session?: Session | null }>>;
|
|
257
|
+
}) satisfies Get<[], UniversalMiddleware<Universal.Context & { session?: Session | null }>>;
|
|
225
258
|
|
|
226
259
|
/**
|
|
227
260
|
* Github OAuth authorization handler
|
|
@@ -252,7 +285,7 @@ export const luciaGithubLoginHandler = (() => async () => {
|
|
|
252
285
|
*
|
|
253
286
|
* @link {@see https://lucia-auth.com/guides/oauth/basics#validate-callback}
|
|
254
287
|
*/
|
|
255
|
-
export const luciaGithubCallbackHandler = (() => async (request) => {
|
|
288
|
+
export const luciaGithubCallbackHandler = (() => async (request, context, _runtime) => {
|
|
256
289
|
const cookies = parse(request.headers.get("cookie") ?? "");
|
|
257
290
|
const params = new URL(request.url).searchParams;
|
|
258
291
|
const code = params.get("code");
|
|
@@ -274,13 +307,18 @@ export const luciaGithubCallbackHandler = (() => async (request) => {
|
|
|
274
307
|
});
|
|
275
308
|
const githubUser = (await githubUserResponse.json()) as GitHubUser;
|
|
276
309
|
|
|
277
|
-
const existingAccount =
|
|
278
|
-
"github",
|
|
279
|
-
|
|
280
|
-
|
|
310
|
+
const existingAccount: DatabaseOAuthAccount | undefined | null = BATI.has("drizzle")
|
|
311
|
+
? ((await drizzleQueries.getExistingAccount(context.db, "github", githubUser.id)) as
|
|
312
|
+
| DatabaseOAuthAccount
|
|
313
|
+
| undefined)
|
|
314
|
+
: BATI.has("sqlite") && !BATI.hasD1
|
|
315
|
+
? sqliteQueries.getExistingAccount<DatabaseOAuthAccount>(context.db, "github", githubUser.id)
|
|
316
|
+
: BATI.hasD1
|
|
317
|
+
? await d1Queries.getExistingAccount<DatabaseOAuthAccount>(context.db, "github", githubUser.id)
|
|
318
|
+
: undefined;
|
|
281
319
|
|
|
282
320
|
if (existingAccount) {
|
|
283
|
-
const session = await lucia.createSession(
|
|
321
|
+
const session = await context.lucia.createSession(
|
|
284
322
|
BATI.has("drizzle") ? existingAccount.userId : existingAccount.user_id,
|
|
285
323
|
{},
|
|
286
324
|
);
|
|
@@ -288,22 +326,28 @@ export const luciaGithubCallbackHandler = (() => async (request) => {
|
|
|
288
326
|
status: 302,
|
|
289
327
|
headers: {
|
|
290
328
|
Location: "/",
|
|
291
|
-
"set-cookie": lucia.createSessionCookie(session.id).serialize(),
|
|
329
|
+
"set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
|
|
292
330
|
},
|
|
293
331
|
});
|
|
294
332
|
}
|
|
295
333
|
|
|
296
334
|
const userId = generateId(15);
|
|
297
335
|
|
|
298
|
-
(BATI.has("drizzle")
|
|
336
|
+
if (BATI.has("drizzle")) {
|
|
337
|
+
await drizzleQueries.signupWithGithub(context.db, userId, githubUser.login, githubUser.id);
|
|
338
|
+
} else if (BATI.has("sqlite") && !BATI.hasD1) {
|
|
339
|
+
sqliteQueries.signupWithGithub(context.db, userId, githubUser.login, githubUser.id);
|
|
340
|
+
} else if (BATI.hasD1) {
|
|
341
|
+
await d1Queries.signupWithGithub(context.db, userId, githubUser.login, githubUser.id);
|
|
342
|
+
}
|
|
299
343
|
|
|
300
|
-
const session = await lucia.createSession(userId, {});
|
|
344
|
+
const session = await context.lucia.createSession(userId, {});
|
|
301
345
|
|
|
302
346
|
return new Response(null, {
|
|
303
347
|
status: 302,
|
|
304
348
|
headers: {
|
|
305
349
|
Location: "/",
|
|
306
|
-
"set-cookie": lucia.createSessionCookie(session.id).serialize(),
|
|
350
|
+
"set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
|
|
307
351
|
},
|
|
308
352
|
});
|
|
309
353
|
} catch (error) {
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import "dotenv/config";
|
|
2
|
-
import { Lucia } from "lucia";
|
|
2
|
+
import { Lucia, type Register } from "lucia";
|
|
3
3
|
import { GitHub } from "arctic";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import { dbD1, dbSqlite } from "@batijs/drizzle/database/drizzle/db";
|
|
5
|
+
import { db as sqliteDb } from "@batijs/sqlite/database/sqlite/db";
|
|
6
|
+
import { D1Database } from "@cloudflare/workers-types";
|
|
7
|
+
export declare function initializeLucia(db: BATI.If<{
|
|
8
|
+
'BATI.has("sqlite") && !BATI.hasD1': ReturnType<typeof sqliteDb>;
|
|
9
|
+
'BATI.has("drizzle") && !BATI.hasD1': ReturnType<typeof dbSqlite>;
|
|
10
|
+
'BATI.has("drizzle")': ReturnType<typeof dbD1>;
|
|
11
|
+
"BATI.hasD1": D1Database;
|
|
12
|
+
}, "union">): Lucia<Record<never, never>, {
|
|
10
13
|
username: string;
|
|
11
14
|
}>;
|
|
12
15
|
/**
|
|
@@ -22,21 +25,38 @@ export declare const github: GitHub;
|
|
|
22
25
|
*/
|
|
23
26
|
declare module "lucia" {
|
|
24
27
|
interface Register {
|
|
25
|
-
Lucia: typeof
|
|
28
|
+
Lucia: ReturnType<typeof initializeLucia>;
|
|
26
29
|
DatabaseUserAttributes: Omit<DatabaseUser, "id">;
|
|
27
30
|
}
|
|
28
31
|
}
|
|
32
|
+
declare global {
|
|
33
|
+
namespace Universal {
|
|
34
|
+
interface Context {
|
|
35
|
+
lucia: Register["Lucia"];
|
|
36
|
+
db: BATI.If<{
|
|
37
|
+
'BATI.has("sqlite") && !BATI.hasD1': ReturnType<typeof sqliteDb>;
|
|
38
|
+
'BATI.has("drizzle") && !BATI.hasD1': ReturnType<typeof dbSqlite>;
|
|
39
|
+
'BATI.has("drizzle")': ReturnType<typeof dbD1>;
|
|
40
|
+
"BATI.hasD1": D1Database;
|
|
41
|
+
}>;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
29
45
|
export interface DatabaseUser {
|
|
30
46
|
id: string;
|
|
31
47
|
username: string;
|
|
32
|
-
password?: string;
|
|
48
|
+
password?: string | null;
|
|
33
49
|
}
|
|
34
50
|
export interface DatabaseOAuthAccount {
|
|
35
51
|
provider_id: string;
|
|
36
52
|
provider_user_id: string;
|
|
37
|
-
userId: string;
|
|
38
53
|
user_id: string;
|
|
39
54
|
}
|
|
55
|
+
export interface DatabaseOAuthAccount {
|
|
56
|
+
providerId: string;
|
|
57
|
+
providerUserId: string;
|
|
58
|
+
userId: string;
|
|
59
|
+
}
|
|
40
60
|
export interface GitHubUser {
|
|
41
61
|
id: number;
|
|
42
62
|
login: string;
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { Session, User } from "lucia";
|
|
2
|
+
import { type Get, type UniversalMiddleware } from "@universal-middleware/core";
|
|
3
|
+
/**
|
|
4
|
+
* Add lucia database to the context
|
|
5
|
+
*
|
|
6
|
+
* @link {@see https://universal-middleware.dev/examples/context-middleware}
|
|
7
|
+
*/
|
|
8
|
+
export declare const luciaDbMiddleware: Get<[], UniversalMiddleware>;
|
|
2
9
|
/**
|
|
3
10
|
* CSRF protection middleware
|
|
4
11
|
*
|
|
@@ -10,18 +17,11 @@ export declare const luciaCsrfMiddleware: () => (request: Request) => Promise<Re
|
|
|
10
17
|
*
|
|
11
18
|
* @link {@see https://lucia-auth.com/guides/validate-session-cookies/}
|
|
12
19
|
*/
|
|
13
|
-
export declare const luciaAuthContextMiddleware:
|
|
14
|
-
session: null;
|
|
15
|
-
user: null;
|
|
16
|
-
} | {
|
|
17
|
-
sessionId: string;
|
|
18
|
-
session: Session | null;
|
|
19
|
-
user: User | null;
|
|
20
|
-
}>;
|
|
20
|
+
export declare const luciaAuthContextMiddleware: Get<[], UniversalMiddleware>;
|
|
21
21
|
/**
|
|
22
22
|
* Set Set-Cookie headers if in context
|
|
23
23
|
*/
|
|
24
|
-
export declare const luciaAuthCookieMiddleware: () => (_request: Request, context: {
|
|
24
|
+
export declare const luciaAuthCookieMiddleware: () => (_request: Request, context: Universal.Context & {
|
|
25
25
|
session?: Session | null;
|
|
26
26
|
user?: User | null;
|
|
27
27
|
sessionId?: string | null;
|
|
@@ -31,17 +31,17 @@ export declare const luciaAuthCookieMiddleware: () => (_request: Request, contex
|
|
|
31
31
|
*
|
|
32
32
|
* @link {@see https://lucia-auth.com/guides/email-and-password/basics#register-user}
|
|
33
33
|
*/
|
|
34
|
-
export declare const luciaAuthSignupHandler: () => (request: Request) => Promise<Response>;
|
|
34
|
+
export declare const luciaAuthSignupHandler: () => (request: Request, context: Universal.Context, _runtime: import("@universal-middleware/core").RuntimeAdapter) => Promise<Response>;
|
|
35
35
|
/**
|
|
36
36
|
* Sign in user handler
|
|
37
37
|
*
|
|
38
38
|
* @link {@see https://lucia-auth.com/guides/email-and-password/basics#sign-in-user}
|
|
39
39
|
*/
|
|
40
|
-
export declare const luciaAuthLoginHandler: () => (request: Request) => Promise<Response>;
|
|
40
|
+
export declare const luciaAuthLoginHandler: () => (request: Request, context: Universal.Context, _runtime: import("@universal-middleware/core").RuntimeAdapter) => Promise<Response>;
|
|
41
41
|
/**
|
|
42
42
|
* Log out user handler
|
|
43
43
|
*/
|
|
44
|
-
export declare const luciaAuthLogoutHandler: () => (_request: Request, context: {
|
|
44
|
+
export declare const luciaAuthLogoutHandler: () => (_request: Request, context: Universal.Context & {
|
|
45
45
|
session?: Session | null;
|
|
46
46
|
}) => Promise<Response>;
|
|
47
47
|
/**
|
|
@@ -55,7 +55,7 @@ export declare const luciaGithubLoginHandler: () => () => Promise<Response>;
|
|
|
55
55
|
*
|
|
56
56
|
* @link {@see https://lucia-auth.com/guides/oauth/basics#validate-callback}
|
|
57
57
|
*/
|
|
58
|
-
export declare const luciaGithubCallbackHandler: () => (request: Request) => Promise<Response>;
|
|
58
|
+
export declare const luciaGithubCallbackHandler: () => (request: Request, context: Universal.Context, _runtime: import("@universal-middleware/core").RuntimeAdapter) => Promise<Response>;
|
|
59
59
|
export declare function validateInput(username: string | null, password: string | null): {
|
|
60
60
|
error: {
|
|
61
61
|
username: string | null;
|
|
@@ -27,7 +27,8 @@ export default function Page() {
|
|
|
27
27
|
body: JSON.stringify(formData),
|
|
28
28
|
headers: { "Content-Type": "application/json" },
|
|
29
29
|
});
|
|
30
|
-
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
const result: Record<string, any> = await response.json();
|
|
31
32
|
if ("error" in result) {
|
|
32
33
|
console.error("A validation error has occurred :", result.error);
|
|
33
34
|
setError(result.error);
|
|
@@ -16,6 +16,12 @@ export default defineConfig({
|
|
|
16
16
|
//# BATI.has("hattip") && !BATI.has("vercel")
|
|
17
17
|
hattip(),
|
|
18
18
|
],
|
|
19
|
+
//# BATI.hasD1
|
|
20
|
+
build: {
|
|
21
|
+
rollupOptions: {
|
|
22
|
+
external: ["wrangler"],
|
|
23
|
+
},
|
|
24
|
+
},
|
|
19
25
|
//# BATI.has("vercel") && BATI.hasServer
|
|
20
26
|
vercel: {
|
|
21
27
|
additionalEndpoints: [
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*# BATI include-if-imported #*/
|
|
2
|
+
import type { Get, UniversalMiddleware } from "@universal-middleware/core";
|
|
3
|
+
import { dbD1, dbSqlite } from "@batijs/drizzle/database/drizzle/db";
|
|
4
|
+
import { db as sqliteDb } from "@batijs/sqlite/database/sqlite/db";
|
|
5
|
+
import { getDbFromRuntime } from "@batijs/d1/database/d1/helpers";
|
|
6
|
+
import { D1Database } from "@cloudflare/workers-types";
|
|
7
|
+
|
|
8
|
+
//# BATI.hasDatabase
|
|
9
|
+
declare global {
|
|
10
|
+
namespace Universal {
|
|
11
|
+
interface Context {
|
|
12
|
+
db: BATI.If<{
|
|
13
|
+
'BATI.has("sqlite") && !BATI.hasD1': ReturnType<typeof sqliteDb>;
|
|
14
|
+
'BATI.has("drizzle") && !BATI.hasD1': ReturnType<typeof dbSqlite>;
|
|
15
|
+
'BATI.has("drizzle")': ReturnType<typeof dbD1>;
|
|
16
|
+
"BATI.hasD1": D1Database;
|
|
17
|
+
}>;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Add `db` to the Context
|
|
23
|
+
export const dbMiddleware: Get<[], UniversalMiddleware> = () => async (_request, context, _runtime) => {
|
|
24
|
+
const db =
|
|
25
|
+
BATI.has("sqlite") && !BATI.hasD1
|
|
26
|
+
? sqliteDb()
|
|
27
|
+
: BATI.has("drizzle") && !BATI.hasD1
|
|
28
|
+
? dbSqlite()
|
|
29
|
+
: BATI.has("drizzle")
|
|
30
|
+
? dbD1(await getDbFromRuntime(_runtime))
|
|
31
|
+
: await getDbFromRuntime(_runtime);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
...context,
|
|
35
|
+
db: db as BATI.Any,
|
|
36
|
+
};
|
|
37
|
+
};
|