integrations/better-auth #555
Replies: 9 comments 6 replies
-
|
Remember, don’t forget to add the OpenAPI plugin before following these instructions: import { betterAuth } from "better-auth"; export const auth = betterAuth({ |
Beta Was this translation helpful? Give feedback.
-
|
I'm using Elysia.js and have the following setup: export const authRoutes = new Elysia({ prefix: "/auth" }).all(
"/*",
({ request }) => auth.handler(request),
);And let's say we have a structure like this. const app = new Elysia()
.use(elysiaLogger)
.use(cors())
.group("/api/v1", (app) => {
return app.use(authRoutes);
})
.listen(3000);How do I access the routes defined in authRoutes? My instinct was to access better auth endpoints at |
Beta Was this translation helpful? Give feedback.
-
|
Using the given macro that checks for user authentication, it returns a |
Beta Was this translation helpful? Give feedback.
-
|
I am receiving an error in a route file when trying to use the auth macro. I configured Better Auth. I used the plugin in my index.ts but on my route I can't receive the typing when I put the auth macro What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
-
|
How to get type safety using Eden when mounting Better Auth ? |
Beta Was this translation helpful? Give feedback.
-
|
The doc should mention that you need to configure the fetch when using treaty and better-auth. This took me two hours to figure out. |
Beta Was this translation helpful? Give feedback.
-
|
auth.ts index.ts well. the thing is im getting unauthorized whenever im trying to fetch the user using the token as bearer token |
Beta Was this translation helpful? Give feedback.
-
|
Hi, I have a question. ================================================ import { Elysia } from 'elysia' import { openApiPlugin } from './plugins/openapi' import { postRoutes } from './routes/post.route' export const app = new Elysia() ================================================ import { Elysia } from 'elysia' export const postRoutes = new Elysia({ name: 'posts', prefix: '/v1/api' }) ================================================ import { Elysia } from 'elysia' export const betterAuthPlugin = new Elysia({ name: 'better-auth' }) |
Beta Was this translation helpful? Give feedback.
-
|
Guys, I’ve run into this issue in a few projects and it’s actually pretty simple to work around, although you should still add a few extra security validations afterward just to be safe. What ends up fixing the problem is a combination of two things. First, By default, derived context in Elysia does not propagate correctly to child routes. When your auth plugin is mounted at a higher level, nested routes may not receive export const authPlugin = new Elysia({ name: "auth" })
.derive({ as: "scoped" }, async ({ request }) => {
const { user, session } = await resolveAuthUser(request.headers);
return {
user,
session,
isAuthenticated: Boolean(user),
};
});The second point is resolving the session directly from the request headers. Better Auth relies on cookies to read the session, so headers must be passed exactly as expected. Elysia sometimes provides headers as a plain object instead of a Normalizing the headers before calling Better Auth fixes this. async function getSessionFromHeaders(
headers: Headers | Record<string, string>
) {
const headersObj =
headers instanceof Headers
? headers
: new Headers(headers as Record<string, string>);
return auth.api.getSession({
headers: headersObj,
});
}Once these two pieces are in place, authentication becomes consistent across routes and child routes stop randomly failing with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
integrations/better-auth
We may use @better-auth/cli to generate auth schema and migrate our database as well.
https://elysiajs.com/integrations/better-auth
Beta Was this translation helpful? Give feedback.
All reactions