From 722cb22769b4ac28bf3889ff5a1237df6c2f04bd Mon Sep 17 00:00:00 2001 From: SaltyAom Date: Sat, 22 Feb 2025 01:18:11 +0700 Subject: [PATCH] doc(elysia): using mount and macro to simplify code (#1531) --- docs/content/docs/integrations/elysia.mdx | 97 ++++++++++++----------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/docs/content/docs/integrations/elysia.mdx b/docs/content/docs/integrations/elysia.mdx index e68a96b56d..82e79b799a 100644 --- a/docs/content/docs/integrations/elysia.mdx +++ b/docs/content/docs/integrations/elysia.mdx @@ -15,71 +15,78 @@ We need to mount the handler to Elysia endpoint. import { Elysia } from "elysia"; import { auth } from "./auth"; -const betterAuthView = (context: Context) => { - const BETTER_AUTH_ACCEPT_METHODS = ["POST", "GET"] - // validate request method - if(BETTER_AUTH_ACCEPT_METHODS.includes(context.request.method)) { - return auth.handler(context.request); - } - else { - context.error(405) - } -} - -const app = new Elysia().all("/api/auth/*", betterAuthView).listen(3000); +const app = new Elysia().mount(auth.handler).listen(3000); console.log( - `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}` + `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`, ); ``` -### Middleware +### CORS -You can use [derive](https://elysiajs.com/essential/handler.html#derive) to provide session and user information before pass to view. +To configure cors, you can use the `cors` plugin from `@elysiajs/cors`. + +```ts +import { Elysia } from "elysia"; +import { cors } from "@elysiajs/cors"; + +import { auth } from "./auth"; + +const app = new Elysia() + .use( + cors({ + origin: "http://localhost:3001", + methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], + credentials: true, + allowedHeaders: ["Content-Type", "Authorization"], + }), + ) + .mount(auth.handler) + .listen(3000); + +console.log( + `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`, +); +``` + +### Macro + +You can use [macro](https://elysiajs.com/patterns/macro.html#macro) with [resolve](https://elysiajs.com/essential/handler.html#resolve) to provide session and user information before pass to view. ```ts import { Elysia } from "elysia"; import { auth } from "./auth"; // user middleware (compute user and session and pass to routes) -const userMiddleware = async (request: Request) => { - const session = await auth.api.getSession({ headers: request.headers }); +const betterAuth = new Elysia({ name: "better-auth" }) + .mount(auth.handler) + .macro({ + auth: { + async resolve({ error, request: { headers } }) { + const session = await auth.api.getSession({ + headers, + }); - if (!session) { - return { - user: null, - session: null - } - } + if (!session) return error(401); - return { - user: session.user, - session: session.session - } -} - -// user info view -// type User can be export from `typeof auth.$Infer.Session.user` -// type Session can be export from `typeof auth.$Infer.Session.session` -const userInfo = (user: User | null, session: Session | null) => { - return { - user: user, - session: session - } -} + return { + user: session.user, + session: session.session, + }; + }, + }, + }); const app = new Elysia() - .derive(({ request }) => userMiddleware(request)) - // about betterAuthView code, please check `Mount the handler` topic - .all("/api/auth/*", betterAuthView) - // Example custom endpoint - .get("/user", ({ user, session }) => userInfo(user, session)) + .use(betterAuth) + .get("/user", ({ user }) => user, { + auth: true, + }) .listen(3000); console.log( - `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}` + `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`, ); - ``` This will allow you to access the `user` and `session` object in all of your routes.