Elysia 통합
이 통합 가이드는 bun 서버와 함께 Elysia를 사용한다고 가정합니다.
시작하기 전에, Better Auth 인스턴스가 설정되어 있는지 확인하세요. 아직 설정하지 않았다면 설치를 확인하세요.
핸들러 마운트
Elysia 엔드포인트에 핸들러를 마운트해야 합니다.
import { Elysia } from "elysia";
import { auth } from "./auth";
const app = new Elysia().mount(auth.handler).listen(3000);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);CORS
CORS를 구성하려면, @elysiajs/cors의 cors 플러그인을 사용할 수 있습니다.
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}`,
);매크로
resolve와 함께 매크로를 사용하여 뷰에 전달하기 전에 세션 및 사용자 정보를 제공할 수 있습니다.
import { Elysia } from "elysia";
import { auth } from "./auth";
// 사용자 미들웨어 (사용자 및 세션을 계산하여 라우트에 전달)
const betterAuth = new Elysia({ name: "better-auth" })
.mount(auth.handler)
.macro({
auth: {
async resolve({ status, request: { headers } }) {
const session = await auth.api.getSession({
headers,
});
if (!session) return status(401);
return {
user: session.user,
session: session.session,
};
},
},
});
const app = new Elysia()
.use(betterAuth)
.get("/user", ({ user }) => user, {
auth: true,
})
.listen(3000);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);이렇게 하면 모든 라우트에서 user 및 session 객체에 접근할 수 있습니다.