diff --git a/examples/better-auth/src/backend/auth.ts b/examples/better-auth/src/backend/auth.ts index 141cff093..c204f1e62 100644 --- a/examples/better-auth/src/backend/auth.ts +++ b/examples/better-auth/src/backend/auth.ts @@ -1,20 +1,21 @@ -// import { betterAuth } from "better-auth"; -// import { sqliteAdapter } from "@better-auth/sqlite"; -// import Database from "better-sqlite3"; -// -// const db = new Database("./auth.db"); -// -// export const auth = betterAuth({ -// database: sqliteAdapter(db), -// emailAndPassword: { -// enabled: true, -// }, -// session: { -// expiresIn: 60 * 60 * 24 * 7, // 7 days -// updateAge: 60 * 60 * 24, // 1 day (every day the session expiry is updated) -// }, -// plugins: [], -// }); -// -// export type Session = typeof auth.$Infer.Session; -// export type User = typeof auth.$Infer.User; +import { betterAuth } from "better-auth"; +import { sqliteAdapter } from "@better-auth/sqlite"; +import Database from "better-sqlite3"; + +const db = new Database("./auth.db"); + +export const auth = betterAuth({ + // IMPORTANT: Connect your own database here + database: sqliteAdapter(db), + emailAndPassword: { + enabled: true, + }, + session: { + expiresIn: 60 * 60 * 24 * 7, // 7 days + updateAge: 60 * 60 * 24, // 1 day (every day the session expiry is updated) + }, + plugins: [], +}); + +export type Session = typeof auth.$Infer.Session; +export type User = typeof auth.$Infer.User; diff --git a/examples/better-auth/src/backend/registry.ts b/examples/better-auth/src/backend/registry.ts index 9bfac3607..8d2a17003 100644 --- a/examples/better-auth/src/backend/registry.ts +++ b/examples/better-auth/src/backend/registry.ts @@ -1,47 +1,47 @@ -// import { actor, setup } from "@rivetkit/actor"; -// import { auth, type Session, type User } from "./auth"; -// -// export const chatRoom = actor({ -// onAuth: async (c) => { -// const authResult = await auth.api.getSession({ -// headers: c.req.headers, -// }); -// -// if (!authResult?.session || !authResult?.user) { -// throw new Error("Unauthorized"); -// } -// -// return { -// userId: authResult.user.id, -// user: authResult.user, -// session: authResult.session, -// }; -// }, -// state: { -// messages: [] as Array<{ id: string; userId: string; username: string; message: string; timestamp: number }> -// }, -// actions: { -// sendMessage: (c, message: string) => { -// const newMessage = { -// id: crypto.randomUUID(), -// userId: c.auth.userId, -// username: c.auth.user.email, -// message, -// timestamp: Date.now(), -// }; -// -// c.state.messages.push(newMessage); -// c.broadcast("newMessage", newMessage); -// -// return newMessage; -// }, -// getMessages: (c) => { -// return c.state.messages; -// }, -// }, -// }); -// -// export const registry = setup({ -// use: { chatRoom }, -// }); -// +import { actor, setup } from "@rivetkit/actor"; +import { auth, type Session, type User } from "./auth"; + +export const chatRoom = actor({ + onAuth: async (c) => { + const authResult = await auth.api.getSession({ + headers: c.req.headers, + }); + + if (!authResult?.session || !authResult?.user) { + throw new Error("Unauthorized"); + } + + return { + userId: authResult.user.id, + user: authResult.user, + session: authResult.session, + }; + }, + state: { + messages: [] as Array<{ id: string; userId: string; username: string; message: string; timestamp: number }> + }, + actions: { + sendMessage: (c, message: string) => { + const newMessage = { + id: crypto.randomUUID(), + userId: c.auth.userId, + username: c.auth.user.email, + message, + timestamp: Date.now(), + }; + + c.state.messages.push(newMessage); + c.broadcast("newMessage", newMessage); + + return newMessage; + }, + getMessages: (c) => { + return c.state.messages; + }, + }, +}); + +export const registry = setup({ + use: { chatRoom }, +}); + diff --git a/examples/better-auth/src/backend/server.ts b/examples/better-auth/src/backend/server.ts index 62437f6b7..dc9b9420a 100644 --- a/examples/better-auth/src/backend/server.ts +++ b/examples/better-auth/src/backend/server.ts @@ -1,54 +1,54 @@ -// import { registry } from "./registry"; -// import { auth } from "./auth"; -// import { Hono } from "hono"; -// import { serve } from "@hono/node-server"; -// -// // Setup router -// const app = new Hono(); -// -// // Start RivetKit -// const { client, hono } = registry.run({ -// driver: createMemoryDriver(), -// cors: { -// // IMPORTANT: Configure origins in production -// origin: "*", -// }, -// }); -// -// // Mount Better Auth routes -// app.on(["GET", "POST"], "/api/auth/**", (c) => auth.handler(c.req.raw)); -// -// // Expose RivetKit to the frontend -// app.route("/registry", hono); -// -// // Example HTTP endpoint to join chat room -// app.post("/api/join-room/:roomId", async (c) => { -// const roomId = c.req.param("roomId"); -// -// // Verify authentication -// const authResult = await auth.api.getSession({ -// headers: c.req.header(), -// }); -// -// if (!authResult?.session || !authResult?.user) { -// return c.json({ error: "Unauthorized" }, 401); -// } -// -// try { -// const room = client.chatRoom.getOrCreate(roomId); -// const messages = await room.getMessages(); -// -// return c.json({ -// success: true, -// roomId, -// messages, -// user: authResult.user -// }); -// } catch (error) { -// return c.json({ error: "Failed to join room" }, 500); -// } -// }); -// -// serve({ fetch: app.fetch, port: 8080 }, () => -// console.log("Listening at http://localhost:8080"), -// ); +import { registry } from "./registry"; +import { auth } from "./auth"; +import { Hono } from "hono"; +import { serve } from "@hono/node-server"; + +// Setup router +const app = new Hono(); + +// Start RivetKit +const { client, hono } = registry.run({ + driver: createMemoryDriver(), + cors: { + // IMPORTANT: Configure origins in production + origin: "*", + }, +}); + +// Mount Better Auth routes +app.on(["GET", "POST"], "/api/auth/**", (c) => auth.handler(c.req.raw)); + +// Expose RivetKit to the frontend +app.route("/registry", hono); + +// Example HTTP endpoint to join chat room +app.post("/api/join-room/:roomId", async (c) => { + const roomId = c.req.param("roomId"); + + // Verify authentication + const authResult = await auth.api.getSession({ + headers: c.req.header(), + }); + + if (!authResult?.session || !authResult?.user) { + return c.json({ error: "Unauthorized" }, 401); + } + + try { + const room = client.chatRoom.getOrCreate(roomId); + const messages = await room.getMessages(); + + return c.json({ + success: true, + roomId, + messages, + user: authResult.user + }); + } catch (error) { + return c.json({ error: "Failed to join room" }, 500); + } +}); + +serve({ fetch: app.fetch, port: 8080 }, () => + console.log("Listening at http://localhost:8080"), +); diff --git a/examples/better-auth/src/frontend/App.tsx b/examples/better-auth/src/frontend/App.tsx index 059b6e758..f76d8b7b5 100644 --- a/examples/better-auth/src/frontend/App.tsx +++ b/examples/better-auth/src/frontend/App.tsx @@ -1,73 +1,73 @@ -// import { useState, useEffect } from "react"; -// import { authClient } from "./auth-client"; -// import { AuthForm } from "./components/AuthForm"; -// import { ChatRoom } from "./components/ChatRoom"; -// -// function App() { -// const [user, setUser] = useState<{ id: string; email: string } | null>(null); -// const [loading, setLoading] = useState(true); -// -// useEffect(() => { -// // Check if user is already authenticated -// const checkAuth = async () => { -// try { -// const session = await authClient.getSession(); -// if (session.data?.user) { -// setUser(session.data.user); -// } -// } catch (error) { -// console.error("Auth check failed:", error); -// } finally { -// setLoading(false); -// } -// }; -// -// checkAuth(); -// }, []); -// -// const handleAuthSuccess = async () => { -// try { -// const session = await authClient.getSession(); -// if (session.data?.user) { -// setUser(session.data.user); -// } -// } catch (error) { -// console.error("Failed to get user after auth:", error); -// } -// }; -// -// const handleSignOut = () => { -// setUser(null); -// }; -// -// if (loading) { -// return ( -//
-// Loading... -//
-// ); -// } -// -// return ( -//
-//
-//

-// RivetKit with Better Auth -//

-// -// {user ? ( -// -// ) : ( -// -// )} -//
-//
-// ); -// } -// -// export default App; +import { useState, useEffect } from "react"; +import { authClient } from "./auth-client"; +import { AuthForm } from "./components/AuthForm"; +import { ChatRoom } from "./components/ChatRoom"; + +function App() { + const [user, setUser] = useState<{ id: string; email: string } | null>(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + // Check if user is already authenticated + const checkAuth = async () => { + try { + const session = await authClient.getSession(); + if (session.data?.user) { + setUser(session.data.user); + } + } catch (error) { + console.error("Auth check failed:", error); + } finally { + setLoading(false); + } + }; + + checkAuth(); + }, []); + + const handleAuthSuccess = async () => { + try { + const session = await authClient.getSession(); + if (session.data?.user) { + setUser(session.data.user); + } + } catch (error) { + console.error("Failed to get user after auth:", error); + } + }; + + const handleSignOut = () => { + setUser(null); + }; + + if (loading) { + return ( +
+ Loading... +
+ ); + } + + return ( +
+
+

+ RivetKit with Better Auth +

+ + {user ? ( + + ) : ( + + )} +
+
+ ); +} + +export default App; diff --git a/examples/better-auth/src/frontend/auth-client.ts b/examples/better-auth/src/frontend/auth-client.ts index 8756bb2da..64c8a7b5a 100644 --- a/examples/better-auth/src/frontend/auth-client.ts +++ b/examples/better-auth/src/frontend/auth-client.ts @@ -1,5 +1,5 @@ -// import { createAuthClient } from "better-auth/react"; -// -// export const authClient = createAuthClient({ -// baseURL: "http://localhost:8080", -// }); +import { createAuthClient } from "better-auth/react"; + +export const authClient = createAuthClient({ + baseURL: "http://localhost:8080", +}); diff --git a/examples/better-auth/src/frontend/main.tsx b/examples/better-auth/src/frontend/main.tsx index cd443c7ac..6d0ba7949 100644 --- a/examples/better-auth/src/frontend/main.tsx +++ b/examples/better-auth/src/frontend/main.tsx @@ -1,9 +1,9 @@ -// import React from "react"; -// import ReactDOM from "react-dom/client"; -// import App from "./App"; -// -// ReactDOM.createRoot(document.getElementById("root")!).render( -// -// -// , -// ); +import React from "react"; +import ReactDOM from "react-dom/client"; +import App from "./App"; + +ReactDOM.createRoot(document.getElementById("root")!).render( + + + , +); diff --git a/packages/core/src/actor/connection.ts b/packages/core/src/actor/connection.ts index d4dde5695..07e482eae 100644 --- a/packages/core/src/actor/connection.ts +++ b/packages/core/src/actor/connection.ts @@ -52,8 +52,8 @@ export class Conn { return this.__persist.p; } - public get auth(): unknown { - return this.__persist.a; + public get auth(): AD { + return this.__persist.a as AD; } public get _stateEnabled() {