BA
Better Auth•3mo ago
Imam

Cors Error on Elysia with Node Adapter

i always got cors error only for better-auth api on Elysia, here is my backend code (Elysia):
import { cors } from '@elysiajs/cors';
import { node } from '@elysiajs/node';
import { swagger } from '@elysiajs/swagger';
import { db } from '@repo/db';
import { Elysia } from 'elysia';

import betterAuthView from './libs/auth';

// import { userMiddleware } from './middlewares/auth-middleware';

const app = new Elysia({ adapter: node() })
.use(
cors({
origin: 'http://localhost:3000',
credentials: true,
})
)
.use(swagger())
.onRequest(({ set }) => {
set.headers['access-control-allow-credentials'] = 'true';
})
// .derive(userMiddleware)
.all('/api/auth/*', betterAuthView)
.get('/', async () => {
return {
message: 'Hello',
};
})
.get('/api', async () => {
return { message: 'Hello from API' };
})
.post('/api/users', async () => {
const users = await db.query.userTable.findMany();

return users;
})

.listen(process.env.PORT);

console.log(`🦊 Elysia is running at http://localhost:${process.env.PORT}`);

export type App = typeof app;
import { cors } from '@elysiajs/cors';
import { node } from '@elysiajs/node';
import { swagger } from '@elysiajs/swagger';
import { db } from '@repo/db';
import { Elysia } from 'elysia';

import betterAuthView from './libs/auth';

// import { userMiddleware } from './middlewares/auth-middleware';

const app = new Elysia({ adapter: node() })
.use(
cors({
origin: 'http://localhost:3000',
credentials: true,
})
)
.use(swagger())
.onRequest(({ set }) => {
set.headers['access-control-allow-credentials'] = 'true';
})
// .derive(userMiddleware)
.all('/api/auth/*', betterAuthView)
.get('/', async () => {
return {
message: 'Hello',
};
})
.get('/api', async () => {
return { message: 'Hello from API' };
})
.post('/api/users', async () => {
const users = await db.query.userTable.findMany();

return users;
})

.listen(process.env.PORT);

console.log(`🦊 Elysia is running at http://localhost:${process.env.PORT}`);

export type App = typeof app;
my github repo link: https://github.com/mamlzy/cms-hono.git (on elysia branch)
GitHub
GitHub - mamlzy/cms-hono
Contribute to mamlzy/cms-hono development by creating an account on GitHub.
No description
13 Replies
Imam
ImamOP•3mo ago
server auth.ts
import { db } from '@repo/db'; // your drizzle instance
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { username } from 'better-auth/plugins';

export const auth = betterAuth({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL,
database: drizzleAdapter(db, {
provider: 'pg',
}),
emailAndPassword: {
enabled: true,
},
plugins: [username()],
trustedOrigins: [process.env.NEXT_PUBLIC_WEB_BASE_URL!],
});
import { db } from '@repo/db'; // your drizzle instance
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { username } from 'better-auth/plugins';

export const auth = betterAuth({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL,
database: drizzleAdapter(db, {
provider: 'pg',
}),
emailAndPassword: {
enabled: true,
},
plugins: [username()],
trustedOrigins: [process.env.NEXT_PUBLIC_WEB_BASE_URL!],
});
client auth.ts
import { usernameClient } from 'better-auth/client/plugins';
import { createAuthClient } from 'better-auth/react';

export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL, // the base url of your auth server
emailAndPassword: {
enabled: true,
},
plugins: [usernameClient()],
fetchOptions: {
credentials: 'include',
},
});
import { usernameClient } from 'better-auth/client/plugins';
import { createAuthClient } from 'better-auth/react';

export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL, // the base url of your auth server
emailAndPassword: {
enabled: true,
},
plugins: [usernameClient()],
fetchOptions: {
credentials: 'include',
},
});
Imam
ImamOP•3mo ago
No description
Laktos
Laktos•3mo ago
Check that your NEXT PUBLIC WEB BASE URL is in fact http://localhost:3000
Imam
ImamOP•3mo ago
even i hardcoded it, the cors keep yelling, i don't know why i'll try to double check it again, thanks for the suggestion @Laktos
Esscraye
Esscraye•6d ago
hey @Imam have you achieved to resolve your problem ?
Imam
ImamOP•6d ago
are you still facing this issue on the latest version of elysia + node adpater & better-auth? since facing this issue, i don't use elysia again
Esscraye
Esscraye•6d ago
i'm not using that stack, i'm using a front in next.js and a back in express.js, i have the better-auth client in my front and the better-auth on my api
Imam
ImamOP•6d ago
i think using next.js with express would work perfectly fine i was using hono and just works but you should concern about the url stuff, the cors would not giving any errors next.js + hono (seperate repo)
Esscraye
Esscraye•6d ago
mmh, i have no idea what i do wrong.. and have the exact same error as your last screen
Imam
ImamOP•6d ago
how about the trustedOrigins? u could check the docs about it better-auth docs
Esscraye
Esscraye•6d ago
i have it in my back with my front url
Imam
ImamOP•6d ago
can you just hard code the url? i mean do not use env variables just for testing
Esscraye
Esscraye•6d ago
it is actually hardcoed oh i just found my problem, it was in my order between cors config and endoint, i'm sorry 😉 thank you ^^

Did you find this page helpful?