@airdraft/cloud-auth 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@airdraft/cloud-auth` will be documented here.
4
+
5
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
@@ -0,0 +1,74 @@
1
+ import type { CloudAuthAdapter } from './types.js';
2
+ interface BetterAuthSession {
3
+ user: {
4
+ id: string;
5
+ email: string;
6
+ name?: string | null;
7
+ image?: string | null;
8
+ };
9
+ session: unknown;
10
+ }
11
+ /**
12
+ * Minimal structural interface for a `betterAuth(...)` instance.
13
+ * Accepts the real Better Auth instance without requiring an exact type import.
14
+ */
15
+ export interface BetterAuthLike {
16
+ /** Fetch-compatible catch-all route handler. */
17
+ handler: (req: Request) => Promise<Response>;
18
+ api: {
19
+ /** Resolves the session from request headers. Returns null if unauthenticated. */
20
+ getSession(opts: {
21
+ headers: Headers;
22
+ }): Promise<BetterAuthSession | null>;
23
+ /**
24
+ * Create a user account. Requires the Better Auth `admin` plugin.
25
+ * @see https://better-auth.com/docs/plugins/admin
26
+ */
27
+ adminCreateUser?(opts: {
28
+ body: {
29
+ email: string;
30
+ name?: string;
31
+ password?: string;
32
+ role?: string;
33
+ data?: Record<string, unknown>;
34
+ };
35
+ }): Promise<{
36
+ user: {
37
+ id: string;
38
+ };
39
+ }>;
40
+ /**
41
+ * Delete a user account and all associated sessions.
42
+ * Requires the Better Auth `admin` plugin.
43
+ */
44
+ adminRemoveUser?(opts: {
45
+ body: {
46
+ userId: string;
47
+ };
48
+ }): Promise<void>;
49
+ };
50
+ }
51
+ /**
52
+ * Wraps a `betterAuth(...)` instance to satisfy the `CloudAuthAdapter` interface.
53
+ *
54
+ * The `admin` plugin must be enabled on the Better Auth instance for
55
+ * `createUser` and `deleteUser` to work.
56
+ *
57
+ * ```ts
58
+ * import { betterAuth } from 'better-auth'
59
+ * import { admin } from 'better-auth/plugins'
60
+ * import { mongodbAdapter } from 'better-auth/adapters/mongodb'
61
+ * import { createBetterAuthAdapter } from '@airdraft/cloud-auth/better-auth'
62
+ *
63
+ * export const auth = betterAuth({
64
+ * database: mongodbAdapter(db),
65
+ * emailAndPassword: { enabled: true },
66
+ * plugins: [admin()],
67
+ * })
68
+ *
69
+ * export const authAdapter = createBetterAuthAdapter(auth)
70
+ * ```
71
+ */
72
+ export declare function createBetterAuthAdapter(auth: BetterAuthLike): CloudAuthAdapter;
73
+ export {};
74
+ //# sourceMappingURL=BetterAuthAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BetterAuthAdapter.d.ts","sourceRoot":"","sources":["../src/BetterAuthAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAkC,MAAM,YAAY,CAAA;AAUlF,UAAU,iBAAiB;IACzB,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM,CAAA;QACV,KAAK,EAAE,MAAM,CAAA;QACb,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;QACpB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KACtB,CAAA;IACD,OAAO,EAAE,OAAO,CAAA;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,gDAAgD;IAChD,OAAO,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC5C,GAAG,EAAE;QACH,kFAAkF;QAClF,UAAU,CAAC,IAAI,EAAE;YAAE,OAAO,EAAE,OAAO,CAAA;SAAE,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAA;QACzE;;;WAGG;QACH,eAAe,CAAC,CAAC,IAAI,EAAE;YACrB,IAAI,EAAE;gBACJ,KAAK,EAAE,MAAM,CAAA;gBACb,IAAI,CAAC,EAAE,MAAM,CAAA;gBACb,QAAQ,CAAC,EAAE,MAAM,CAAA;gBACjB,IAAI,CAAC,EAAE,MAAM,CAAA;gBACb,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;aAC/B,CAAA;SACF,GAAG,OAAO,CAAC;YAAE,IAAI,EAAE;gBAAE,EAAE,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAC,CAAA;QACrC;;;WAGG;QACH,eAAe,CAAC,CAAC,IAAI,EAAE;YAAE,IAAI,EAAE;gBAAE,MAAM,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;KACpE,CAAA;CACF;AAMD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,cAAc,GAAG,gBAAgB,CA6C9E"}
@@ -0,0 +1,65 @@
1
+ // ---------------------------------------------------------------------------
2
+ // BetterAuthAdapter factory
3
+ // ---------------------------------------------------------------------------
4
+ /**
5
+ * Wraps a `betterAuth(...)` instance to satisfy the `CloudAuthAdapter` interface.
6
+ *
7
+ * The `admin` plugin must be enabled on the Better Auth instance for
8
+ * `createUser` and `deleteUser` to work.
9
+ *
10
+ * ```ts
11
+ * import { betterAuth } from 'better-auth'
12
+ * import { admin } from 'better-auth/plugins'
13
+ * import { mongodbAdapter } from 'better-auth/adapters/mongodb'
14
+ * import { createBetterAuthAdapter } from '@airdraft/cloud-auth/better-auth'
15
+ *
16
+ * export const auth = betterAuth({
17
+ * database: mongodbAdapter(db),
18
+ * emailAndPassword: { enabled: true },
19
+ * plugins: [admin()],
20
+ * })
21
+ *
22
+ * export const authAdapter = createBetterAuthAdapter(auth)
23
+ * ```
24
+ */
25
+ export function createBetterAuthAdapter(auth) {
26
+ return {
27
+ async verifySession(req) {
28
+ const session = await auth.api.getSession({ headers: req.headers });
29
+ if (!session)
30
+ return null;
31
+ return {
32
+ userId: session.user.id,
33
+ email: session.user.email,
34
+ name: session.user.name ?? undefined,
35
+ avatarUrl: session.user.image ?? undefined,
36
+ };
37
+ },
38
+ handler(req) {
39
+ return auth.handler(req);
40
+ },
41
+ async createUser(input) {
42
+ if (!auth.api.adminCreateUser) {
43
+ throw new Error('@airdraft/cloud-auth: createUser requires the Better Auth `admin` plugin. ' +
44
+ 'Add `admin()` to your betterAuth plugins array.');
45
+ }
46
+ const result = await auth.api.adminCreateUser({
47
+ body: {
48
+ email: input.email,
49
+ name: input.name,
50
+ password: input.password,
51
+ data: input.emailVerified ? { emailVerified: true } : undefined,
52
+ },
53
+ });
54
+ return result.user.id;
55
+ },
56
+ async deleteUser(userId) {
57
+ if (!auth.api.adminRemoveUser) {
58
+ throw new Error('@airdraft/cloud-auth: deleteUser requires the Better Auth `admin` plugin. ' +
59
+ 'Add `admin()` to your betterAuth plugins array.');
60
+ }
61
+ await auth.api.adminRemoveUser({ body: { userId } });
62
+ },
63
+ };
64
+ }
65
+ //# sourceMappingURL=BetterAuthAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BetterAuthAdapter.js","sourceRoot":"","sources":["../src/BetterAuthAdapter.ts"],"names":[],"mappings":"AAmDA,8EAA8E;AAC9E,4BAA4B;AAC5B,8EAA8E;AAE9E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAoB;IAC1D,OAAO;QACL,KAAK,CAAC,aAAa,CAAC,GAAY;YAC9B,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAA;YACnE,IAAI,CAAC,OAAO;gBAAE,OAAO,IAAI,CAAA;YACzB,OAAO;gBACL,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,EAAE;gBACvB,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK;gBACzB,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,SAAS;gBACpC,SAAS,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,SAAS;aAC3C,CAAA;QACH,CAAC;QAED,OAAO,CAAC,GAAY;YAClB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC1B,CAAC;QAED,KAAK,CAAC,UAAU,CAAC,KAAsB;YACrC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,CAAC;gBAC9B,MAAM,IAAI,KAAK,CACb,4EAA4E;oBAC1E,iDAAiD,CACpD,CAAA;YACH,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC;gBAC5C,IAAI,EAAE;oBACJ,KAAK,EAAE,KAAK,CAAC,KAAK;oBAClB,IAAI,EAAE,KAAK,CAAC,IAAI;oBAChB,QAAQ,EAAE,KAAK,CAAC,QAAQ;oBACxB,IAAI,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,SAAS;iBAChE;aACF,CAAC,CAAA;YACF,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAA;QACvB,CAAC;QAED,KAAK,CAAC,UAAU,CAAC,MAAc;YAC7B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,CAAC;gBAC9B,MAAM,IAAI,KAAK,CACb,4EAA4E;oBAC1E,iDAAiD,CACpD,CAAA;YACH,CAAC;YACD,MAAM,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,CAAA;QACtD,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export type { CloudAuthAdapter, CloudIdentity, CreateUserInput } from './types.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,gBAAgB,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Minimal identity returned for every verified session.
3
+ * Downstream code looks up team membership separately.
4
+ */
5
+ export interface CloudIdentity {
6
+ userId: string;
7
+ email: string;
8
+ name?: string;
9
+ avatarUrl?: string;
10
+ }
11
+ /**
12
+ * Input for programmatic user creation (e.g. during invite acceptance).
13
+ */
14
+ export interface CreateUserInput {
15
+ email: string;
16
+ name?: string;
17
+ /** Only required for email/password auth. Adapters that don't use passwords may ignore it. */
18
+ password?: string;
19
+ /** Whether the email is pre-verified (e.g. for invited users). Default: false. */
20
+ emailVerified?: boolean;
21
+ }
22
+ /**
23
+ * Pluggable authentication adapter for the Airdraft Cloud dashboard.
24
+ *
25
+ * The default implementation wraps Better Auth (`createBetterAuthAdapter`),
26
+ * but any object implementing this interface can be used.
27
+ *
28
+ * ```ts
29
+ * // Default (Better Auth):
30
+ * import { createBetterAuthAdapter } from '@airdraft/cloud-auth/better-auth'
31
+ * const authAdapter = createBetterAuthAdapter(betterAuthInstance)
32
+ *
33
+ * // Custom:
34
+ * const authAdapter: CloudAuthAdapter = {
35
+ * async verifySession(req) { ... },
36
+ * async handler(req) { ... },
37
+ * async createUser(input) { ... },
38
+ * async deleteUser(userId) { ... },
39
+ * }
40
+ * ```
41
+ */
42
+ export interface CloudAuthAdapter {
43
+ /**
44
+ * Verify an inbound request and return the caller's identity.
45
+ * Returns `null` for unauthenticated requests — callers should return 401.
46
+ */
47
+ verifySession(req: Request): Promise<CloudIdentity | null>;
48
+ /**
49
+ * Catch-all HTTP handler for auth endpoints (login, logout, OAuth callback,
50
+ * magic link, OTP, etc.). Mount at `/api/auth/[...all]` in the cloud app.
51
+ */
52
+ handler(req: Request): Promise<Response>;
53
+ /**
54
+ * Create a new user account. Returns the new user's ID.
55
+ * Called during invite acceptance to provision the account.
56
+ */
57
+ createUser(input: CreateUserInput): Promise<string>;
58
+ /**
59
+ * Delete a user account and invalidate all their sessions.
60
+ * Called when a user closes their account.
61
+ */
62
+ deleteUser(userId: string): Promise<void>;
63
+ }
64
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAiBA;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,8FAA8F;IAC9F,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,kFAAkF;IAClF,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IAE1D;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;IAExC;;;OAGG;IACH,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAEnD;;;OAGG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1C"}
package/dist/types.js ADDED
@@ -0,0 +1,18 @@
1
+ // ---------------------------------------------------------------------------
2
+ // CloudAuthAdapter — pluggable session layer for the Airdraft Cloud dashboard
3
+ //
4
+ // Implement this interface to swap the auth backend (Better Auth, Clerk,
5
+ // custom JWT, etc.) without touching any other part of the cloud app.
6
+ //
7
+ // The adapter is responsible for:
8
+ // - Verifying inbound requests and returning the caller's identity
9
+ // - Owning the auth HTTP handler (/api/auth/[...all])
10
+ // - Creating and deleting user accounts
11
+ //
12
+ // The adapter is NOT responsible for:
13
+ // - Team / org membership (Airdraft-owned MongoDB collections)
14
+ // - Project access control (Airdraft service layer)
15
+ // - `ntk_` API key verification (@airdraft/auth, unchanged)
16
+ // ---------------------------------------------------------------------------
17
+ export {};
18
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8EAA8E;AAC9E,EAAE;AACF,yEAAyE;AACzE,sEAAsE;AACtE,EAAE;AACF,kCAAkC;AAClC,qEAAqE;AACrE,wDAAwD;AACxD,0CAA0C;AAC1C,EAAE;AACF,sCAAsC;AACtC,iEAAiE;AACjE,sDAAsD;AACtD,8DAA8D;AAC9D,8EAA8E"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@airdraft/cloud-auth",
3
+ "version": "0.1.0",
4
+ "description": "Airdraft Cloud auth adapter interface — pluggable session verification for the cloud dashboard",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./better-auth": {
14
+ "import": "./dist/BetterAuthAdapter.js",
15
+ "types": "./dist/BetterAuthAdapter.d.ts"
16
+ }
17
+ },
18
+ "files": ["dist", "README.md", "CHANGELOG.md"],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "dev": "tsc --watch",
22
+ "clean": "rm -rf dist",
23
+ "test": "vitest run",
24
+ "test:watch": "vitest",
25
+ "typecheck": "tsc --noEmit",
26
+ "prepublishOnly": "npm run build",
27
+ "release": "standard-version",
28
+ "release:patch": "standard-version --release-as patch",
29
+ "release:minor": "standard-version --release-as minor",
30
+ "release:major": "standard-version --release-as major"
31
+ },
32
+ "publishConfig": { "access": "public" },
33
+ "license": "MIT",
34
+ "peerDependencies": {
35
+ "better-auth": ">=1.0.0"
36
+ },
37
+ "peerDependenciesMeta": {
38
+ "better-auth": { "optional": true }
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^20.0.0",
42
+ "better-auth": "^1.0.0",
43
+ "standard-version": "^9.5.0",
44
+ "typescript": "^5.4.0",
45
+ "vitest": "^1.5.0"
46
+ },
47
+ "engines": { "node": ">=18.0.0" }
48
+ }