Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(web): auth, session, dual-auth libraries

Generae8e3cb10af2
diff --git a/src/web/src/lib/api-auth.ts b/src/web/src/lib/api-auth.ts
new file mode 100644
index 000000000..164da7c7d
--- /dev/null
+++ b/src/web/src/lib/api-auth.ts
@@ -0,0 +1,27 @@
+import { getCloudflareContext } from "@opennextjs/cloudflare"
+import { createDb, queries } from "@alook/shared"
+import { createHash } from "crypto"
+
+function hashToken(raw: string): string {
+ return createHash("sha256").update(raw).digest("hex")
+}
+
+export async function withToken(request: Request) {
+ const authHeader = request.headers.get("Authorization")
+ if (!authHeader?.startsWith("Bearer ")) {
+ return { error: new Response("Unauthorized", { status: 401 }), machineToken: null }
+ }
+ const token = authHeader.slice(7)
+ if (!token.startsWith("al_")) {
+ return { error: new Response("Invalid token format", { status: 401 }), machineToken: null }
+ }
+ const { env } = await getCloudflareContext({ async: true })
+ const db = createDb((env as Env).DB)
+ const hashedToken = hashToken(token)
+ const mt = await queries.machineToken.getMachineTokenByHash(db, hashedToken)
+ if (!mt) {
+ return { error: new Response("Token not found", { status: 401 }), machineToken: null }
+ }
+ queries.machineToken.updateMachineTokenLastUsed(db, mt.id).catch(() => {})
+ return { error: null, machineToken: mt }
+}
diff --git a/src/web/src/lib/auth-client.ts b/src/web/src/lib/auth-client.ts
new file mode 100644
index 000000000..e6e04c440
--- /dev/null
+++ b/src/web/src/lib/auth-client.ts
@@ -0,0 +1,8 @@
+"use client"
+import { createAuthClient } from "better-auth/react"
+
+export const authClient = createAuthClient({
+ baseURL: process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
+})
+
+export const { signIn, signUp, signOut, useSession } = authClient
diff --git a/src/web/src/lib/auth.ts b/src/web/src/lib/auth.ts
new file mode 100644
index 000000000..6f6fd2787
--- /dev/null
+++ b/src/web/src/lib/auth.ts
@@ -0,0 +1,23 @@
+import { betterAuth } from "better-auth"
+
+export function createAuth(env: Env) {
+ return betterAuth({
+ baseURL: env.BETTER_AUTH_URL,
+ database: env.DB,
+ secret: env.BETTER_AUTH_SECRET,
+ emailAndPassword: {
+ enabled: true,
+ requireEmailVerification: false,
+ },
+ socialProviders: {
+ github: {
+ clientId: env.GITHUB_CLIENT_ID,
+ clientSecret: env.GITHUB_CLIENT_SECRET,
+ },
+ google: {
+ clientId: env.GOOGLE_CLIENT_ID,
+ clientSecret: env.GOOGLE_CLIENT_SECRET,
+ },
+ },
+ })
+}
diff --git a/src/web/src/lib/dual-auth.ts b/src/web/src/lib/dual-auth.ts
new file mode 100644
index 000000000..35e456724
--- /dev/null
+++ b/src/web/src/lib/dual-auth.ts
@@ -0,0 +1,35 @@
+import { getCloudflareContext } from "@opennextjs/cloudflare"
+import { createDb, queries } from "@alook/shared"
+import { createAuth } from "@/lib/auth"
+import { createHash } from "crypto"
+
+function hashToken(raw: string): string {
+ return createHash("sha256").update(raw).digest("hex")
+}
+
+export async function requireAuth(request: Request) {
+ const { env } = await getCloudflareContext({ async: true })
+ const cloudflareEnv = env as Env
+
+ const authHeader = request.headers.get("Authorization")
+ if (authHeader?.startsWith("Bearer ")) {
+ const token = authHeader.slice(7)
+ if (token.startsWith("al_")) {
+ const db = createDb(cloudflareEnv.DB)
+ const hashedToken = hashToken(token)
+ const mt = await queries.machineToken.getMachineTokenByHash(db, hashedToken)
+ if (mt) {
+ queries.machineToken.updateMachineTokenLastUsed(db, mt.id).catch(() => {})
+ return { userId: mt.userId, email: mt.userEmail, workspaceId: mt.workspaceId ?? undefined, error: null }
+ }
+ }
+ }
+
+ const auth = createAuth(cloudflareEnv)
+ const session = await auth.api.getSession({ headers: request.headers })
+ if (session) {
+ return { userId: session.user.id, email: session.user.email, workspaceId: undefined, error: null }
+ }
+
+ return { userId: null, email: null, workspaceId: undefined, error: new Response("Unauthorized", { status: 401 }) }
+}
diff --git a/src/web/src/lib/session.ts b/src/web/src/lib/session.ts
new file mode 100644
index 000000000..bb3248494
--- /dev/null
+++ b/src/web/src/lib/session.ts
@@ -0,0 +1,15 @@
+import { headers } from "next/headers"
+import { getCloudflareContext } from "@opennextjs/cloudflare"
+import { createAuth } from "@/lib/auth"
+
+export async function getSession() {
+ const { env } = await getCloudflareContext({ async: true })
+ const auth = createAuth(env as Env)
+ return auth.api.getSession({ headers: await headers() })
+}
+
+export async function requireSession() {
+ const session = await getSession()
+ if (!session) throw new Error("Unauthorized")
+ return session
+}