Authenticate Next.js app against Express backend.

Hi! I'm trying to setup Better-Auth API in my express app and have it linked to my next.js app, so I can make authenticated requests from client and server components.
I got it somehow working but I have my Next.js app with a different url using nginx (local.dev.com), so that way I can test locally Riot Games OAuth.
When I try to log in from localhost it works fine (I can get the session from both client and server components), but when I use the local.dev.com url it doesn't work on server components.

Any help is appreciated. I honestly don't know if it is a good idea to setup authentication like this (a fullstack app with another backend).

This is my config:

export const auth = betterAuth({
    baseURL: `http://localhost:${env.APP_PORT}`,
    database: drizzleAdapter(db, {
        provider: "pg",
        schema,
        usePlural: true,
    }),
    advanced: {
        database: { generateId: false },
        crossSubDomainCookies: {
            enabled: true,
        },
        cookie: {
            sameSite: "none",
            secure: true,
            path: "/",
        },
        defaultCookieAttributes: {
            secure: true,
            sameSite: "none",
        },
    },
    socialProviders: {
        discord: {
            clientId: env.DISCORD_CLIENT_ID,
            clientSecret: env.DISCORD_CLIENT_SECRET,
            redirectURI: "http://localhost:3000/api/auth/callback/discord",
        },
    },
    trustedOrigins: ["https://local.dev.com", "http://localhost:5000"],
});
Was this page helpful?