diff --git a/deno.jsonc b/deno.jsonc index 90a1d1cf..8f958fc1 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -47,15 +47,13 @@ "@/": "./", "netzo/": "./lib/", "fresh/": "https://deno.land/x/fresh@1.6.8/", - "preact": "https://esm.sh/preact@10.19.6", - "preact/": "https://esm.sh/preact@10.19.6/", - "@preact/signals": "https://esm.sh/*@preact/signals@1.2.3", - "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.6.0", - "react": "https://esm.sh/preact@10.19.6/compat", - "react-dom": "https://esm.sh/preact@10.19.6/compat", + "preact": "npm:preact@10.22.0", + "react": "npm:preact@10.22.0/compat", + "react-dom": "npm:preact@10.22.0/compat", + "@preact/signals": "npm:@preact/signals@1.2.3", + "@preact/signals-core": "npm:@preact/signals-core@1.6.1", "std/": "https://deno.land/std@0.208.0/", - "react/jsx-runtime": "https://esm.sh/preact@10.19.6/compat", - "zod": "https://deno.land/x/zod@v3.22.4/mod.ts", + "drizzle-orm": "npm:drizzle-orm@0.30.10", "deno_kv_oauth/": "https://deno.land/x/deno_kv_oauth@v0.9.1/" } } diff --git a/lib/plugins/auth/plugin.ts b/lib/plugins/auth/plugin.ts index f363a442..cf16e1a8 100644 --- a/lib/plugins/auth/plugin.ts +++ b/lib/plugins/auth/plugin.ts @@ -2,11 +2,11 @@ import type { FreshContext, Plugin, PluginRoute } from "fresh/server.ts"; import { HTMLAttributes } from "preact/compat"; import type { NetzoState } from "../../mod.ts"; import { + NetzoStateWithAuth, assertUserIsMemberOfWorkspaceOfApiKeyIfProviderIsNetzo, createAssertUserIsAuthorized, createAuthState, ensureSignedIn, - NetzoStateWithAuth, setRequestState, setSessionState, } from "./middlewares/mod.ts"; @@ -18,8 +18,8 @@ import type { Auth, AuthProvider, AuthUser } from "./utils/types.ts"; export * from "../../deps/deno_kv_oauth/mod.ts"; -export * from "./utils/adapters/database.ts"; -export * from "./utils/adapters/datastore.ts"; +export * from "./utils/adapter.ts"; +export * from "./utils/schema.ts"; type AuthConfigProvider = { /** Whether to allow signups for new users (defaults to true). */ diff --git a/lib/plugins/auth/utils/adapters/database.ts b/lib/plugins/auth/utils/adapter.ts similarity index 93% rename from lib/plugins/auth/utils/adapters/database.ts rename to lib/plugins/auth/utils/adapter.ts index 0d5c5e5d..910e8b92 100644 --- a/lib/plugins/auth/utils/adapters/database.ts +++ b/lib/plugins/auth/utils/adapter.ts @@ -1,8 +1,6 @@ import { eq } from "drizzle-orm"; -import { database, id } from "../../../../database/mod.ts"; -import type { Auth, AuthUser } from "../types.ts"; - -export * from "./database.schema.ts"; +import { database, id } from "../../../database/mod.ts"; +import type { Auth, AuthUser } from "./types.ts"; export const createDatabaseAuth = (db: ReturnType): Auth => { const { $users, $sessions } = db?._?.fullSchema! ?? {}; // use fullSchema, not schema diff --git a/lib/plugins/auth/utils/adapters/datastore.ts b/lib/plugins/auth/utils/adapters/datastore.ts deleted file mode 100644 index f5f32989..00000000 --- a/lib/plugins/auth/utils/adapters/datastore.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { ulid } from "../../../../datastore/mod.ts"; -import type { Auth, AuthUser } from "../types.ts"; - -const KV = await Deno.openKv(Deno.env.get("DENO_KV_PATH")); - -export const createDatastoreAuth = (kv = KV): Auth => { - return { - createUser: async (user: AuthUser) => { - user.id = ulid(); - user.createdAt = new Date().toISOString(); - user.updatedAt = user.createdAt; - user.deletedAt = ""; - const usersKey = ["users", user.authId]; - - const atomicOp = kv.atomic() - .check({ key: usersKey, versionstamp: null }) - .set(usersKey, user); - - const res = await atomicOp.commit(); - if (!res.ok) throw new Error("Failed to create user"); - }, - createUserSession: async (user: AuthUser, sessionId: string) => { - user.updatedAt = new Date().toISOString(); - const usersBySessionKey = ["usersBySession", sessionId]; - - const atomicOp = kv.atomic() - .check({ key: usersBySessionKey, versionstamp: null }) - .set(usersBySessionKey, user); - - const res = await atomicOp.commit(); - if (!res.ok) throw new Error("Failed to create user session"); - }, - updateUser: async (user: AuthUser) => { - user.updatedAt ||= new Date().toISOString(); - const usersKey = ["users", user.authId]; - const usersBySessionKey = ["usersBySession", user.sessionId]; - - const atomicOp = kv.atomic() - .set(usersKey, user) - .set(usersBySessionKey, user); - - const res = await atomicOp.commit(); - if (!res.ok) throw new Error("Failed to update user"); - }, - updateUserSession: async (user: AuthUser, sessionId: string) => { - user.updatedAt = new Date().toISOString(); - const userKey = ["users", user.authId]; - const oldUserBySessionKey = ["usersBySession", user.sessionId]; - const newUserBySessionKey = ["usersBySession", sessionId]; - const newUser: AuthUser = { ...user, sessionId }; - - const atomicOp = kv.atomic() - .set(userKey, newUser) - .delete(oldUserBySessionKey) - .check({ key: newUserBySessionKey, versionstamp: null }) - .set(newUserBySessionKey, newUser); - - const res = await atomicOp.commit(); - if (!res.ok) throw new Error("Failed to update user session"); - }, - getUser: async (authId: string) => { - const res = await kv.get(["users", authId]); - return res.value; - }, - getUserBySession: async (sessionId: string) => { - const key = ["usersBySession", sessionId]; - const eventualRes = await kv.get(key, { - consistency: "eventual", - }); - if (eventualRes.value !== null) return eventualRes.value; - const res = await kv.get(key); - return res.value; - }, - }; -}; diff --git a/lib/plugins/auth/utils/adapters/database.schema.ts b/lib/plugins/auth/utils/schema.ts similarity index 100% rename from lib/plugins/auth/utils/adapters/database.schema.ts rename to lib/plugins/auth/utils/schema.ts