@batijs/cli 0.0.444 → 0.0.446

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.
Files changed (148) hide show
  1. package/dist/boilerplates/@batijs/auth0/files/$.env.js +1 -1
  2. package/dist/boilerplates/@batijs/auth0/files/$README.md.js +1 -1
  3. package/dist/boilerplates/@batijs/authjs/files/$package.json.js +1 -1
  4. package/dist/boilerplates/@batijs/aws/files/$README.md.js +1 -1
  5. package/dist/boilerplates/@batijs/aws/files/$package.json.js +1 -1
  6. package/dist/boilerplates/@batijs/aws/files/$tsconfig.json.js +1 -1
  7. package/dist/boilerplates/@batijs/biome/files/$package.json.js +1 -1
  8. package/dist/boilerplates/@batijs/cloudflare/files/$package.json.js +1 -1
  9. package/dist/boilerplates/@batijs/cloudflare/files/$tsconfig.json.js +1 -1
  10. package/dist/boilerplates/@batijs/cloudflare/files/$vite.config.ts.js +1 -1
  11. package/dist/boilerplates/@batijs/compiled/files/$package.json.js +1 -1
  12. package/dist/boilerplates/@batijs/compiled/files/$vite.config.ts.js +1 -1
  13. package/dist/boilerplates/@batijs/d1/files/$README.md.js +1 -1
  14. package/dist/boilerplates/@batijs/d1/files/$package.json.js +1 -1
  15. package/dist/boilerplates/@batijs/d1/files/$tsconfig.json.js +1 -1
  16. package/dist/boilerplates/@batijs/d1/files/$wrangler.toml.js +1 -1
  17. package/dist/boilerplates/@batijs/d1-sqlite/files/$package.json.js +1 -7
  18. package/dist/boilerplates/@batijs/drizzle/files/$.env.js +1 -1
  19. package/dist/boilerplates/@batijs/drizzle/files/$README.md.js +1 -1
  20. package/dist/boilerplates/@batijs/drizzle/files/$package.json.js +1 -13
  21. package/dist/boilerplates/@batijs/eslint/files/$package.json.js +1 -1
  22. package/dist/boilerplates/@batijs/express/files/$package.json.js +1 -1
  23. package/dist/boilerplates/@batijs/express/files/express-entry.ts +0 -35
  24. package/dist/boilerplates/@batijs/fastify/files/$package.json.js +1 -1
  25. package/dist/boilerplates/@batijs/fastify/files/fastify-entry.ts +0 -35
  26. package/dist/boilerplates/@batijs/google-analytics/files/$.env.js +1 -1
  27. package/dist/boilerplates/@batijs/h3/files/$package.json.js +1 -1
  28. package/dist/boilerplates/@batijs/h3/files/h3-entry.ts +0 -35
  29. package/dist/boilerplates/@batijs/hattip/files/$package.json.js +1 -1
  30. package/dist/boilerplates/@batijs/hattip/files/hattip-entry.ts +0 -35
  31. package/dist/boilerplates/@batijs/hono/files/$package.json.js +1 -1
  32. package/dist/boilerplates/@batijs/hono/files/$vite.config.ts.js +1 -1
  33. package/dist/boilerplates/@batijs/hono/files/hono-entry.ts +0 -35
  34. package/dist/boilerplates/@batijs/mantine/files/$README.md.js +1 -1
  35. package/dist/boilerplates/@batijs/mantine/files/$package.json.js +1 -1
  36. package/dist/boilerplates/@batijs/panda-css/files/$package.json.js +1 -1
  37. package/dist/boilerplates/@batijs/pnpm/files/$pnpm-workspace.yaml.js +1 -1
  38. package/dist/boilerplates/@batijs/prettier/files/$package.json.js +1 -1
  39. package/dist/boilerplates/@batijs/prettier/files/.prettierignore +1 -4
  40. package/dist/boilerplates/@batijs/prisma/files/$.env.js +1 -1
  41. package/dist/boilerplates/@batijs/prisma/files/$README.md.js +1 -1
  42. package/dist/boilerplates/@batijs/prisma/files/$package.json.js +1 -1
  43. package/dist/boilerplates/@batijs/react/files/$README.md.js +1 -1
  44. package/dist/boilerplates/@batijs/react/files/$package.json.js +1 -1
  45. package/dist/boilerplates/@batijs/react/files/$tsconfig.json.js +1 -1
  46. package/dist/boilerplates/@batijs/react/files/$vite.config.ts.js +1 -1
  47. package/dist/boilerplates/@batijs/react/files/layouts/LayoutDefault.tsx +0 -1
  48. package/dist/boilerplates/@batijs/react/files/pages/+config.ts +1 -1
  49. package/dist/boilerplates/@batijs/react-sentry/files/$package.json.js +1 -1
  50. package/dist/boilerplates/@batijs/sentry/files/$.env.js +1 -1
  51. package/dist/boilerplates/@batijs/sentry/files/$README.md.js +1 -1
  52. package/dist/boilerplates/@batijs/sentry/files/$package.json.js +1 -1
  53. package/dist/boilerplates/@batijs/sentry/files/$vite.config.ts.js +1 -1
  54. package/dist/boilerplates/@batijs/sentry/files/pages/$+client.ts.js +1 -1
  55. package/dist/boilerplates/@batijs/shadcn-ui/files/$README.md.js +1 -1
  56. package/dist/boilerplates/@batijs/shadcn-ui/files/$package.json.js +1 -1
  57. package/dist/boilerplates/@batijs/shadcn-ui/files/$tsconfig.json.js +1 -1
  58. package/dist/boilerplates/@batijs/shadcn-ui/files/$vite.config.ts.js +1 -1
  59. package/dist/boilerplates/@batijs/shared/files/$README.md.js +1 -1
  60. package/dist/boilerplates/@batijs/shared/files/gitignore +1 -4
  61. package/dist/boilerplates/@batijs/shared-server/files/$package.json.js +1 -1
  62. package/dist/boilerplates/@batijs/solid/files/$README.md.js +1 -1
  63. package/dist/boilerplates/@batijs/solid/files/$package.json.js +1 -1
  64. package/dist/boilerplates/@batijs/solid/files/$tsconfig.json.js +1 -1
  65. package/dist/boilerplates/@batijs/solid/files/$vite.config.ts.js +1 -1
  66. package/dist/boilerplates/@batijs/solid/files/layouts/LayoutDefault.tsx +0 -1
  67. package/dist/boilerplates/@batijs/solid/files/pages/+config.ts +1 -1
  68. package/dist/boilerplates/@batijs/solid-sentry/files/$package.json.js +1 -1
  69. package/dist/boilerplates/@batijs/sqlite/files/$.env.js +1 -1
  70. package/dist/boilerplates/@batijs/sqlite/files/$README.md.js +1 -1
  71. package/dist/boilerplates/@batijs/sqlite/files/$package.json.js +1 -13
  72. package/dist/boilerplates/@batijs/sqlite/files/database/sqlite/schema/all.ts +0 -2
  73. package/dist/boilerplates/@batijs/sqlite/types/database/sqlite/schema/all.d.ts +0 -1
  74. package/dist/boilerplates/@batijs/tailwindcss/files/$package.json.js +1 -1
  75. package/dist/boilerplates/@batijs/tailwindcss/files/$vite.config.ts.js +1 -1
  76. package/dist/boilerplates/@batijs/telefunc/files/$package.json.js +1 -1
  77. package/dist/boilerplates/@batijs/telefunc/files/$vite.config.ts.js +1 -1
  78. package/dist/boilerplates/@batijs/trpc/files/$package.json.js +1 -1
  79. package/dist/boilerplates/@batijs/trpc/types/trpc/client.d.ts +3 -3
  80. package/dist/boilerplates/@batijs/ts-rest/files/$package.json.js +1 -1
  81. package/dist/boilerplates/@batijs/vercel/files/$package.json.js +1 -1
  82. package/dist/boilerplates/@batijs/vercel/files/$tsconfig.json.js +1 -1
  83. package/dist/boilerplates/@batijs/vercel/files/$vite.config.ts.js +1 -1
  84. package/dist/boilerplates/@batijs/vercel/files/pages/$+config.ts.js +1 -1
  85. package/dist/boilerplates/@batijs/vue/files/$README.md.js +1 -1
  86. package/dist/boilerplates/@batijs/vue/files/$package.json.js +1 -1
  87. package/dist/boilerplates/@batijs/vue/files/$tsconfig.json.js +1 -1
  88. package/dist/boilerplates/@batijs/vue/files/$vite.config.ts.js +1 -1
  89. package/dist/boilerplates/@batijs/vue/files/layouts/LayoutDefault.vue +0 -2
  90. package/dist/boilerplates/@batijs/vue/files/pages/+config.ts +1 -1
  91. package/dist/boilerplates/@batijs/vue-sentry/files/$package.json.js +1 -1
  92. package/dist/boilerplates/boilerplates.json +0 -118
  93. package/dist/{chunk-HYBBK3CW.js → chunk-ZDDQD2BO.js} +9848 -9848
  94. package/dist/index.js +17 -64
  95. package/package.json +5 -5
  96. package/dist/boilerplates/@batijs/d1-sqlite/files/database/d1/queries/lucia-auth.ts +0 -29
  97. package/dist/boilerplates/@batijs/d1-sqlite/files/database/migrations/lucia-auth.sql +0 -20
  98. package/dist/boilerplates/@batijs/d1-sqlite/types/database/d1/queries/lucia-auth.d.ts +0 -5
  99. package/dist/boilerplates/@batijs/drizzle/files/database/drizzle/queries/lucia-auth.ts +0 -56
  100. package/dist/boilerplates/@batijs/drizzle/files/database/drizzle/schema/lucia-auth.ts +0 -41
  101. package/dist/boilerplates/@batijs/drizzle/types/database/drizzle/queries/lucia-auth.d.ts +0 -25
  102. package/dist/boilerplates/@batijs/drizzle/types/database/drizzle/schema/lucia-auth.d.ts +0 -190
  103. package/dist/boilerplates/@batijs/express/files/global.d.ts +0 -10
  104. package/dist/boilerplates/@batijs/fastify/files/global.d.ts +0 -10
  105. package/dist/boilerplates/@batijs/firebase-auth/files/$.env.js +0 -24
  106. package/dist/boilerplates/@batijs/firebase-auth/files/$README.md.js +0 -39
  107. package/dist/boilerplates/@batijs/firebase-auth/files/$package.json.js +0 -133
  108. package/dist/boilerplates/@batijs/firebase-auth/files/firebase/$service-account.json.js +0 -26
  109. package/dist/boilerplates/@batijs/firebase-auth/files/global.d.ts +0 -11
  110. package/dist/boilerplates/@batijs/firebase-auth/files/libs/firebaseAdmin.ts +0 -15
  111. package/dist/boilerplates/@batijs/firebase-auth/files/libs/firebaseUI.ts +0 -31
  112. package/dist/boilerplates/@batijs/firebase-auth/files/pages/+client.ts +0 -30
  113. package/dist/boilerplates/@batijs/firebase-auth/files/pages/login/+config.ts +0 -6
  114. package/dist/boilerplates/@batijs/firebase-auth/files/pages/login/+guard.ts +0 -11
  115. package/dist/boilerplates/@batijs/firebase-auth/files/server/firebase-auth-middleware.ts +0 -71
  116. package/dist/boilerplates/@batijs/firebase-auth/types/libs/firebaseAdmin.d.ts +0 -5
  117. package/dist/boilerplates/@batijs/firebase-auth/types/libs/firebaseUI.d.ts +0 -5
  118. package/dist/boilerplates/@batijs/firebase-auth/types/pages/+client.d.ts +0 -1
  119. package/dist/boilerplates/@batijs/firebase-auth/types/pages/login/+config.d.ts +0 -4
  120. package/dist/boilerplates/@batijs/firebase-auth/types/pages/login/+guard.d.ts +0 -3
  121. package/dist/boilerplates/@batijs/firebase-auth/types/server/firebase-auth-middleware.d.ts +0 -4
  122. package/dist/boilerplates/@batijs/h3/files/global.d.ts +0 -10
  123. package/dist/boilerplates/@batijs/hono/files/global.d.ts +0 -10
  124. package/dist/boilerplates/@batijs/lucia-auth/files/$.env.js +0 -32
  125. package/dist/boilerplates/@batijs/lucia-auth/files/$README.md.js +0 -38
  126. package/dist/boilerplates/@batijs/lucia-auth/files/$package.json.js +0 -113
  127. package/dist/boilerplates/@batijs/lucia-auth/files/global.d.ts +0 -11
  128. package/dist/boilerplates/@batijs/lucia-auth/files/lib/lucia-auth.ts +0 -136
  129. package/dist/boilerplates/@batijs/lucia-auth/files/pages/login/+guard.ts +0 -11
  130. package/dist/boilerplates/@batijs/lucia-auth/files/pages/login/style.css +0 -94
  131. package/dist/boilerplates/@batijs/lucia-auth/files/server/lucia-auth-handlers.ts +0 -401
  132. package/dist/boilerplates/@batijs/lucia-auth/types/lib/lucia-auth.d.ts +0 -63
  133. package/dist/boilerplates/@batijs/lucia-auth/types/pages/login/+guard.d.ts +0 -3
  134. package/dist/boilerplates/@batijs/lucia-auth/types/server/lucia-auth-handlers.d.ts +0 -65
  135. package/dist/boilerplates/@batijs/react-firebase-auth/files/pages/login/+Page.tsx +0 -52
  136. package/dist/boilerplates/@batijs/react-firebase-auth/types/pages/login/+Page.d.ts +0 -3
  137. package/dist/boilerplates/@batijs/react-lucia-auth/files/pages/login/+Page.tsx +0 -97
  138. package/dist/boilerplates/@batijs/react-lucia-auth/types/pages/login/+Page.d.ts +0 -2
  139. package/dist/boilerplates/@batijs/solid-firebase-auth/files/pages/login/+Page.tsx +0 -52
  140. package/dist/boilerplates/@batijs/solid-firebase-auth/types/pages/login/+Page.d.ts +0 -3
  141. package/dist/boilerplates/@batijs/solid-lucia-auth/files/pages/login/+Page.tsx +0 -97
  142. package/dist/boilerplates/@batijs/solid-lucia-auth/types/pages/login/+Page.d.ts +0 -2
  143. package/dist/boilerplates/@batijs/sqlite/files/database/sqlite/queries/lucia-auth.ts +0 -27
  144. package/dist/boilerplates/@batijs/sqlite/files/database/sqlite/schema/lucia-auth.ts +0 -32
  145. package/dist/boilerplates/@batijs/sqlite/types/database/sqlite/queries/lucia-auth.d.ts +0 -5
  146. package/dist/boilerplates/@batijs/sqlite/types/database/sqlite/schema/lucia-auth.d.ts +0 -1
  147. package/dist/boilerplates/@batijs/vue-firebase-auth/files/pages/login/+Page.vue +0 -51
  148. package/dist/boilerplates/@batijs/vue-lucia-auth/files/pages/login/+Page.vue +0 -88
@@ -1,136 +0,0 @@
1
- import "dotenv/config";
2
- import { Lucia, type Register } from "lucia";
3
- import { BetterSqlite3Adapter, D1Adapter } from "@lucia-auth/adapter-sqlite";
4
- import { GitHub } from "arctic";
5
- import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
6
- import { dbD1, dbSqlite } from "@batijs/drizzle/database/drizzle/db";
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";
10
-
11
- /**
12
- * Polyfill needed if you're using Node.js 18 or below
13
- *
14
- * @link {@see https://lucia-auth.com/getting-started/#polyfill}
15
- */
16
- if (!globalThis.crypto) {
17
- Object.defineProperty(globalThis, "crypto", {
18
- value: await import("node:crypto").then((crypto) => crypto.webcrypto as Crypto),
19
- writable: false,
20
- configurable: true,
21
- });
22
- }
23
-
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 as BATI.Any, userTable as BATI.Any)
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
- });
51
-
52
- /**
53
- * Initialize Lucia
54
- *
55
- * @link {@see https://lucia-auth.com/getting-started/#initialize-lucia}
56
- */
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
- };
72
- },
73
- });
74
- }
75
-
76
- /**
77
- * Initialize OAuth provider
78
- *
79
- * @link {@see https://lucia-auth.com/guides/oauth/basics#initialize-oauth-provider}
80
- */
81
- export const github = new GitHub(
82
- process.env.GITHUB_CLIENT_ID as string,
83
- process.env.GITHUB_CLIENT_SECRET as string,
84
- null,
85
- );
86
-
87
- /**
88
- * Define user attributes
89
- *
90
- * @link {@see https://lucia-auth.com/basics/users#define-user-attributes}
91
- */
92
- declare module "lucia" {
93
- interface Register {
94
- Lucia: ReturnType<typeof initializeLucia>;
95
- DatabaseUserAttributes: Omit<DatabaseUser, "id">;
96
- }
97
- }
98
-
99
- declare global {
100
- namespace Universal {
101
- interface Context {
102
- lucia: Register["Lucia"];
103
- db: BATI.If<{
104
- 'BATI.has("sqlite") && !BATI.hasD1': ReturnType<typeof sqliteDb>;
105
- 'BATI.has("drizzle") && !BATI.hasD1': ReturnType<typeof dbSqlite>;
106
- 'BATI.has("drizzle")': ReturnType<typeof dbD1>;
107
- "BATI.hasD1": D1Database;
108
- }>;
109
- }
110
- }
111
- }
112
-
113
- export interface DatabaseUser {
114
- id: string;
115
- username: string;
116
- password?: string | null;
117
- }
118
-
119
- //# !BATI.has("drizzle")
120
- export interface DatabaseOAuthAccount {
121
- provider_id: string;
122
- provider_user_id: string;
123
- user_id: string;
124
- }
125
-
126
- //# BATI.has("drizzle")
127
- export interface DatabaseOAuthAccount {
128
- providerId: string;
129
- providerUserId: string;
130
- userId: string;
131
- }
132
-
133
- export interface GitHubUser {
134
- id: number;
135
- login: string; // username
136
- }
@@ -1,11 +0,0 @@
1
- // https://vike.dev/guard
2
- import { redirect } from "vike/abort";
3
- import type { GuardAsync } from "vike/types";
4
-
5
- const guard: GuardAsync = async (pageContext): ReturnType<GuardAsync> => {
6
- if (pageContext.user) {
7
- throw redirect("/");
8
- }
9
- };
10
-
11
- export { guard };
@@ -1,94 +0,0 @@
1
- header {
2
- font-size: 28px;
3
- font-weight: 600;
4
- text-align: center;
5
- }
6
- .form {
7
- position: absolute;
8
- max-width: 430px;
9
- width: 100%;
10
- }
11
- .form .field {
12
- position: relative;
13
- height: 50px;
14
- width: 100%;
15
- margin-top: 20px;
16
- border-radius: 6px;
17
- }
18
- .field input,
19
- .button-field {
20
- height: 100%;
21
- width: 100%;
22
- border: none;
23
- font-size: 16px;
24
- font-weight: 400;
25
- border-radius: 6px;
26
- }
27
- .button-group {
28
- display: flex;
29
- }
30
- .login-button {
31
- background-color: #0171d3e8;
32
- }
33
- .login-button:hover {
34
- background-color: #016dcb;
35
- }
36
- .signup-button {
37
- background-color: #d30101f6;
38
- }
39
- .signup-button:hover {
40
- background-color: #cb010b;
41
- }
42
- .field input {
43
- outline: none;
44
- padding: 0 15px;
45
- border: 1px solid#CACACA;
46
- }
47
- .field input:focus {
48
- border-bottom-width: 2px;
49
- }
50
- .field button {
51
- color: #fff;
52
- margin: 0 2px;
53
- transition: all 0.3s ease;
54
- cursor: pointer;
55
- }
56
- .field-error {
57
- display: block;
58
- width: 100%;
59
- margin-top: 0.25rem;
60
- font-size: 85%;
61
- color: #dc3545;
62
- }
63
- .media-options a {
64
- display: flex;
65
- align-items: center;
66
- justify-content: center;
67
- }
68
- a.github {
69
- color: #fff;
70
- background-color: #000;
71
- }
72
- a.github .github-icon {
73
- height: 28px;
74
- width: 28px;
75
- color: #000;
76
- font-size: 20px;
77
- border-radius: 50%;
78
- display: flex;
79
- align-items: center;
80
- justify-content: center;
81
- background-color: #fff;
82
- }
83
- .github-icon {
84
- position: absolute;
85
- top: 50%;
86
- left: 15px;
87
- transform: translateY(-50%);
88
- }
89
-
90
- @media screen and (max-width: 400px) {
91
- .form {
92
- padding: 20px 10px;
93
- }
94
- }
@@ -1,401 +0,0 @@
1
- import type { Session, User } from "lucia";
2
- import { generateId, Scrypt, verifyRequestOrigin } from "lucia";
3
- import {
4
- type DatabaseOAuthAccount,
5
- type DatabaseUser,
6
- github,
7
- type GitHubUser,
8
- initializeLucia,
9
- } from "../lib/lucia-auth";
10
- import { SqliteError } from "better-sqlite3";
11
- import { generateState, OAuth2RequestError } from "arctic";
12
- import { parse, serialize } from "cookie";
13
- import * as drizzleQueries from "@batijs/drizzle/database/drizzle/queries/lucia-auth";
14
- import * as sqliteQueries from "@batijs/sqlite/database/sqlite/queries/lucia-auth";
15
- import * as d1Queries from "@batijs/d1-sqlite/database/d1/queries/lucia-auth";
16
- // TODO: stop using universal-middleware and directly integrate server middlewares instead and/or use vike-server https://vike.dev/server. (Bati generates boilerplates that use universal-middleware https://github.com/magne4000/universal-middleware to make Bati's internal logic easier. This is temporary and will be removed soon.)
17
- import { type Get, type UniversalHandler, type UniversalMiddleware } from "@universal-middleware/core";
18
-
19
- /**
20
- * Add lucia database to the context
21
- *
22
- * @link {@see https://universal-middleware.dev/examples/context-middleware}
23
- */
24
- export const luciaDbMiddleware: Get<[], UniversalMiddleware> = () => async (_request, context, _runtime) => {
25
- const lucia = initializeLucia(context.db);
26
- return {
27
- ...context,
28
- lucia,
29
- };
30
- };
31
-
32
- /**
33
- * CSRF protection middleware
34
- *
35
- * @link {@see https://lucia-auth.com/guides/validate-session-cookies/}
36
- */
37
- export const luciaCsrfMiddleware = (() => async (request) => {
38
- if (request.method === "GET") {
39
- return;
40
- }
41
- if (!BATI_TEST) {
42
- const originHeader = request.headers.get("Origin") ?? null;
43
- const hostHeader = request.headers.get("Host") ?? null;
44
-
45
- if (!originHeader || !hostHeader || !verifyRequestOrigin(originHeader, [hostHeader])) {
46
- return new Response("Forbidden Request", {
47
- status: 403,
48
- });
49
- }
50
- }
51
- }) satisfies Get<[], UniversalMiddleware>;
52
-
53
- /**
54
- * Validate session cookies middleware and set context
55
- *
56
- * @link {@see https://lucia-auth.com/guides/validate-session-cookies/}
57
- */
58
- export const luciaAuthContextMiddleware: Get<[], UniversalMiddleware> = () => async (request, context) => {
59
- const sessionId = context.lucia.readSessionCookie(request.headers.get("cookie") ?? "");
60
-
61
- if (!sessionId) {
62
- return {
63
- ...context,
64
- session: null,
65
- user: null,
66
- };
67
- } else {
68
- const { session, user } = await context.lucia.validateSession(sessionId);
69
-
70
- return {
71
- ...context,
72
- sessionId,
73
- session,
74
- user,
75
- };
76
- }
77
- };
78
-
79
- /**
80
- * Set Set-Cookie headers if in context
81
- */
82
- export const luciaAuthCookieMiddleware = (() => (_request, context) => {
83
- return (response: Response) => {
84
- if (context.session?.fresh) {
85
- response.headers.append("Set-Cookie", context.lucia.createSessionCookie(context.session.id).serialize());
86
- }
87
- if (context.sessionId && !context.session) {
88
- response.headers.append("Set-Cookie", context.lucia.createBlankSessionCookie().serialize());
89
- }
90
-
91
- return response;
92
- };
93
- }) satisfies Get<
94
- [],
95
- UniversalMiddleware<Universal.Context & { session?: Session | null; user?: User | null; sessionId?: string | null }>
96
- >;
97
-
98
- /**
99
- * Register user handler
100
- *
101
- * @link {@see https://lucia-auth.com/guides/email-and-password/basics#register-user}
102
- */
103
- export const luciaAuthSignupHandler = (() => async (request, context, _runtime) => {
104
- const body = (await request.json()) as { username: string; password: string };
105
- const username = body.username ?? "";
106
- const password = body.password ?? "";
107
-
108
- const validated = validateInput(username, password);
109
-
110
- if (!validated.success) {
111
- return new Response(JSON.stringify({ error: validated.error }), {
112
- status: 422,
113
- headers: {
114
- "content-type": "application/json",
115
- },
116
- });
117
- }
118
-
119
- /**
120
- * A pure JS implementation of Scrypt.
121
- * It's portable but slower than implementations based on native code.
122
- *
123
- * @link {@see https://lucia-auth.com/reference/main/Scrypt}
124
- * @link {@see https://lucia-auth.com/guides/email-and-password/basics#hashing-passwords}
125
- */
126
- const scrypt = new Scrypt();
127
- const passwordHash = await scrypt.hash(password);
128
-
129
- const userId = generateId(15);
130
-
131
- try {
132
- if (BATI.has("drizzle")) {
133
- await drizzleQueries.signupWithCredentials(context.db, userId, username, passwordHash);
134
- } else if (BATI.has("sqlite") && !BATI.hasD1) {
135
- sqliteQueries.signupWithCredentials(context.db, userId, username, passwordHash);
136
- } else if (BATI.hasD1) {
137
- await d1Queries.signupWithCredentials(context.db, userId, username, passwordHash);
138
- }
139
-
140
- const session = await context.lucia.createSession(userId, {});
141
-
142
- return new Response(JSON.stringify({ status: "success" }), {
143
- status: 200,
144
- headers: {
145
- "content-type": "application/json",
146
- "set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
147
- },
148
- });
149
- } catch (error) {
150
- console.error(error);
151
- if (BATI.has("sqlite") && !BATI.hasD1) {
152
- if (error instanceof SqliteError && error.code === "SQLITE_CONSTRAINT_UNIQUE") {
153
- return new Response(JSON.stringify({ error: { username: "Username already in use" } }), {
154
- status: 422,
155
- headers: {
156
- "content-type": "application/json",
157
- },
158
- });
159
- }
160
- }
161
-
162
- return new Response(JSON.stringify({ error: { invalid: "An unknown error has occurred" } }), {
163
- status: 500,
164
- headers: {
165
- "content-type": "application/json",
166
- },
167
- });
168
- }
169
- }) satisfies Get<[], UniversalMiddleware>;
170
-
171
- /**
172
- * Sign in user handler
173
- *
174
- * @link {@see https://lucia-auth.com/guides/email-and-password/basics#sign-in-user}
175
- */
176
- export const luciaAuthLoginHandler = (() => async (request, context, _runtime) => {
177
- const body = (await request.json()) as { username: string; password: string };
178
- const username = body.username ?? "";
179
- const password = body.password ?? "";
180
-
181
- const validated = validateInput(username, password);
182
-
183
- if (!validated.success) {
184
- return new Response(JSON.stringify({ error: validated.error }), {
185
- status: 422,
186
- headers: {
187
- "content-type": "application/json",
188
- },
189
- });
190
- }
191
-
192
- const existingUser: DatabaseUser | undefined | null = BATI.has("drizzle")
193
- ? await drizzleQueries.getExistingUser(context.db, username)
194
- : BATI.has("sqlite") && !BATI.hasD1
195
- ? sqliteQueries.getExistingUser<DatabaseUser>(context.db, username)
196
- : BATI.hasD1
197
- ? await d1Queries.getExistingUser<DatabaseUser>(context.db, username)
198
- : undefined;
199
-
200
- if (!existingUser) {
201
- return new Response(JSON.stringify({ error: { invalid: "Incorrect username or password" } }), {
202
- status: 422,
203
- headers: {
204
- "content-type": "application/json",
205
- },
206
- });
207
- }
208
-
209
- const scrypt = new Scrypt();
210
- const validPassword = existingUser.password && (await scrypt.verify(existingUser.password, password));
211
-
212
- if (!validPassword) {
213
- return new Response(JSON.stringify({ error: { invalid: "Incorrect username or password" } }), {
214
- status: 422,
215
- headers: {
216
- "content-type": "application/json",
217
- },
218
- });
219
- }
220
-
221
- const session = await context.lucia.createSession(existingUser.id, {});
222
-
223
- return new Response(JSON.stringify({ status: "success" }), {
224
- status: 200,
225
- headers: {
226
- "content-type": "application/json",
227
- "set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
228
- },
229
- });
230
- }) satisfies Get<[], UniversalMiddleware>;
231
-
232
- /**
233
- * Log out user handler
234
- */
235
- export const luciaAuthLogoutHandler = (() => async (_request, context) => {
236
- const session = context.session ?? null;
237
-
238
- if (!session) {
239
- return new Response("Unauthorized Request", {
240
- status: 401,
241
- });
242
- }
243
- /**
244
- * Invalidate sessions
245
- *
246
- * @link {@see https://lucia-auth.com/basics/sessions#invalidate-sessions}
247
- */
248
- await context.lucia.invalidateSession(session.id);
249
-
250
- /**
251
- * Delete session cookie
252
- *
253
- * @link {@see https://lucia-auth.com/basics/sessions#delete-session-cookie}
254
- */
255
- return new Response(JSON.stringify({ status: "success" }), {
256
- status: 200,
257
- headers: {
258
- "set-cookie": context.lucia.createBlankSessionCookie().serialize(),
259
- },
260
- });
261
- }) satisfies Get<[], UniversalMiddleware<Universal.Context & { session?: Session | null }>>;
262
-
263
- /**
264
- * Github OAuth authorization handler
265
- *
266
- * @link {@see https://lucia-auth.com/guides/oauth/basics#creating-authorization-url}
267
- */
268
- export const luciaGithubLoginHandler = (() => async () => {
269
- const state = generateState();
270
- const url = github.createAuthorizationURL(state, ["user:email"]);
271
-
272
- return new Response(null, {
273
- status: 302,
274
- headers: {
275
- Location: url.toString(),
276
- "set-cookie": serialize("github_oauth_state", state, {
277
- path: "/",
278
- secure: process.env.NODE_ENV === "production",
279
- httpOnly: true,
280
- maxAge: 60 * 10,
281
- sameSite: "lax",
282
- }),
283
- },
284
- });
285
- }) satisfies Get<[], UniversalHandler>;
286
-
287
- /**
288
- * Github OAuth validate callback handler
289
- *
290
- * @link {@see https://lucia-auth.com/guides/oauth/basics#validate-callback}
291
- */
292
- export const luciaGithubCallbackHandler = (() => async (request, context, _runtime) => {
293
- const cookies = parse(request.headers.get("cookie") ?? "");
294
- const params = new URL(request.url).searchParams;
295
- const code = params.get("code");
296
- const state = params.get("state");
297
- const storedState = cookies.github_oauth_state ?? null;
298
-
299
- if (!code || !state || !storedState || state !== storedState) {
300
- return new Response("Unauthorized Request", {
301
- status: 401,
302
- });
303
- }
304
-
305
- try {
306
- const tokens = await github.validateAuthorizationCode(code);
307
- const githubUserResponse = await fetch("https://api.github.com/user", {
308
- headers: {
309
- Authorization: `Bearer ${tokens.accessToken}`,
310
- },
311
- });
312
- const githubUser = (await githubUserResponse.json()) as GitHubUser;
313
-
314
- const existingAccount: DatabaseOAuthAccount | undefined | null = BATI.has("drizzle")
315
- ? ((await drizzleQueries.getExistingAccount(context.db, "github", githubUser.id)) as
316
- | DatabaseOAuthAccount
317
- | undefined)
318
- : BATI.has("sqlite") && !BATI.hasD1
319
- ? sqliteQueries.getExistingAccount<DatabaseOAuthAccount>(context.db, "github", githubUser.id)
320
- : BATI.hasD1
321
- ? await d1Queries.getExistingAccount<DatabaseOAuthAccount>(context.db, "github", githubUser.id)
322
- : undefined;
323
-
324
- if (existingAccount) {
325
- const session = await context.lucia.createSession(
326
- BATI.has("drizzle") ? existingAccount.userId : existingAccount.user_id,
327
- {},
328
- );
329
- return new Response(null, {
330
- status: 302,
331
- headers: {
332
- Location: "/",
333
- "set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
334
- },
335
- });
336
- }
337
-
338
- const userId = generateId(15);
339
-
340
- if (BATI.has("drizzle")) {
341
- await drizzleQueries.signupWithGithub(context.db, userId, githubUser.login, githubUser.id);
342
- } else if (BATI.has("sqlite") && !BATI.hasD1) {
343
- sqliteQueries.signupWithGithub(context.db, userId, githubUser.login, githubUser.id);
344
- } else if (BATI.hasD1) {
345
- await d1Queries.signupWithGithub(context.db, userId, githubUser.login, githubUser.id);
346
- }
347
-
348
- const session = await context.lucia.createSession(userId, {});
349
-
350
- return new Response(null, {
351
- status: 302,
352
- headers: {
353
- Location: "/",
354
- "set-cookie": context.lucia.createSessionCookie(session.id).serialize(),
355
- },
356
- });
357
- } catch (error) {
358
- if (error instanceof OAuth2RequestError && error.message === "bad_verification_code") {
359
- return new Response(JSON.stringify({ error: error.message }), {
360
- status: 400,
361
- headers: {
362
- "content-type": "application/json",
363
- },
364
- });
365
- }
366
- return new Response(JSON.stringify({ error: error }), {
367
- status: 500,
368
- headers: {
369
- "content-type": "application/json",
370
- },
371
- });
372
- }
373
- }) satisfies Get<[], UniversalHandler>;
374
-
375
- export function validateInput(username: string | null, password: string | null) {
376
- const error: {
377
- username: string | null;
378
- password: string | null;
379
- } = {
380
- username: null,
381
- password: null,
382
- };
383
-
384
- if (!username || username.length < 3 || username.length > 31 || !/^[a-z0-9_-]+$/.test(username)) {
385
- error.username = "Invalid username";
386
- }
387
- if (!password || password.length < 6 || password.length > 255) {
388
- error.password = "Invalid password";
389
- }
390
-
391
- if (error.username || error.password) {
392
- return {
393
- error,
394
- success: false,
395
- };
396
- }
397
- return {
398
- error,
399
- success: true,
400
- };
401
- }
@@ -1,63 +0,0 @@
1
- import "dotenv/config";
2
- import { Lucia, type Register } from "lucia";
3
- import { GitHub } from "arctic";
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>, {
13
- username: string;
14
- }>;
15
- /**
16
- * Initialize OAuth provider
17
- *
18
- * @link {@see https://lucia-auth.com/guides/oauth/basics#initialize-oauth-provider}
19
- */
20
- export declare const github: GitHub;
21
- /**
22
- * Define user attributes
23
- *
24
- * @link {@see https://lucia-auth.com/basics/users#define-user-attributes}
25
- */
26
- declare module "lucia" {
27
- interface Register {
28
- Lucia: ReturnType<typeof initializeLucia>;
29
- DatabaseUserAttributes: Omit<DatabaseUser, "id">;
30
- }
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
- }
45
- export interface DatabaseUser {
46
- id: string;
47
- username: string;
48
- password?: string | null;
49
- }
50
- export interface DatabaseOAuthAccount {
51
- provider_id: string;
52
- provider_user_id: string;
53
- user_id: string;
54
- }
55
- export interface DatabaseOAuthAccount {
56
- providerId: string;
57
- providerUserId: string;
58
- userId: string;
59
- }
60
- export interface GitHubUser {
61
- id: number;
62
- login: string;
63
- }
@@ -1,3 +0,0 @@
1
- import type { GuardAsync } from "vike/types";
2
- declare const guard: GuardAsync;
3
- export { guard };