useSession cannot reach get-session endpoint

Hey there I'm using React in the frontend side and elysia in the backend with postgres as the driver (with bun sql). The login ad registration through email works correctly but when the user is redirected to the dashboard where I call the useSession the endpoint gives not_found. While working on my localhost everything works fine, this happens only in prod.
24 Replies
_Zaizen_
_Zaizen_OP4w ago
The code in the backend is something like this:
export const api = new Elysia({
prefix: "/api",
})
.all("/auth/*", betterAuthView).listen({port: 80, hostname: '0.0.0.0'}}
export const api = new Elysia({
prefix: "/api",
})
.all("/auth/*", betterAuthView).listen({port: 80, hostname: '0.0.0.0'}}
import type { Context } from "elysia";
import { auth } from "./auth";

const betterAuthView = (context: Context) => {
const BETTER_AUTH_ACCEPT_METHODS = ["POST", "GET"];
if (BETTER_AUTH_ACCEPT_METHODS.includes(context.request.method)) {
return auth.handler(context.request);
}
context.error(405);

};

export default betterAuthView;
import type { Context } from "elysia";
import { auth } from "./auth";

const betterAuthView = (context: Context) => {
const BETTER_AUTH_ACCEPT_METHODS = ["POST", "GET"];
if (BETTER_AUTH_ACCEPT_METHODS.includes(context.request.method)) {
return auth.handler(context.request);
}
context.error(405);

};

export default betterAuthView;
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../../db";
import { account, session, user, verification } from "../../db/auth-schema";
import { env } from "../../env";


const trustedOrigins = ["http://localhost:3000", `https://${env.HOSTNAME}`, ...env.TRUSTED_ORIGINS.split(",").map(origin => `https://${origin}`)];


export const auth = betterAuth({
trustedOrigins,
database: drizzleAdapter(db, {
// We're using Drizzle as our database
provider: "pg",
schema: {
user,
session,
verification,
account,
},
}),
user: {
additionalFields: {
surname: { type: "string", required: true },
phone: { type: "string", required: true },
schoolCode: { type: "string", required: true },
isTeacher: { type: "boolean", required: true },
},
},
emailAndPassword: {
enabled: true, // If you want to use email and password auth
},
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../../db";
import { account, session, user, verification } from "../../db/auth-schema";
import { env } from "../../env";


const trustedOrigins = ["http://localhost:3000", `https://${env.HOSTNAME}`, ...env.TRUSTED_ORIGINS.split(",").map(origin => `https://${origin}`)];


export const auth = betterAuth({
trustedOrigins,
database: drizzleAdapter(db, {
// We're using Drizzle as our database
provider: "pg",
schema: {
user,
session,
verification,
account,
},
}),
user: {
additionalFields: {
surname: { type: "string", required: true },
phone: { type: "string", required: true },
schoolCode: { type: "string", required: true },
isTeacher: { type: "boolean", required: true },
},
},
emailAndPassword: {
enabled: true, // If you want to use email and password auth
},
});
Logs: NOT_FOUND 72 | export class NotFoundError extends Error { 73 | code = 'NOT_FOUND' 74 | status = 404 75 | 76 | constructor(message?: string) { 77 | super(message ?? 'NOT_FOUND') ^ error: NOT_FOUND status: 404, code: "NOT_FOUND" at new NotFoundError (/usr/src/app/server/node_modules/elysia/src/error.ts:77:3) at <anonymous> (/usr/src/app/server/node_modules/@elysiajs/static/dist/index.mjs:258:19)
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
_Zaizen_
_Zaizen_OP4w ago
import { createAuthClient } from "better-auth/react"; import { inferAdditionalFields } from "better-auth/client/plugins"; import { auth } from "@server"; export const authClient = createAuthClient({ plugins: [inferAdditionalFields<typeof auth>()], baseURL: window.location.origin, // the base url of your auth server }); Any idea?
bekacru
bekacru4w ago
pass the api url in the baseURL param
_Zaizen_
_Zaizen_OP4w ago
Wait but if the login and signup work correctly doesn't this mean this is already correct? Will try it in a bit btw
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
_Zaizen_
_Zaizen_OP4w ago
The frontend is on mydomain.com and the backend/api on mydomain.com/api (the backend is actually serving the files for the frontend too) In front I have a reverse proxy but I don't think that's actually important in this context, it doesn't do anything strange on the locations I'm gonna try and recrete the issue in a smaller project that I can share publicly so that you can look at the complete thing btw
_Zaizen_
_Zaizen_OP4w ago
GitHub
GitHub - Anulo2/BetterAuthIssue
Contribute to Anulo2/BetterAuthIssue development by creating an account on GitHub.
_Zaizen_
_Zaizen_OP4w ago
Should I open an issue too about this?
bekacru
bekacru4w ago
if your frontend and backend url are in the same domain you can remove the passed baseURL. Also can you send where exactly it's trying to send the request from the browser?
_Zaizen_
_Zaizen_OP4w ago
http://127.0.0.1:5173/api/auth/get-session This is the route it tries to request generating the error NOT_FOUND error: NOT_FOUND status: 404, code: "NOT_FOUND" at new NotFoundError (node_modules\elysia\src\error.ts:77:3) om the backend
bekacru
bekacru4w ago
this is weird. try it on a http client instead. Also this is localhost, you said it works locally.
_Zaizen_
_Zaizen_OP4w ago
After more testing it actually doesn't work in localhost, should have specified I discovered this while building the example that you see in the repo Already did, In the exmaple it returns "i'm a teapot" (my desired content for when an error is caught) and logs the error said in the message btw: I've also tried this with sqlite as an adapter Gonna push to the repo the sqlite version in a few minutes
_Zaizen_
_Zaizen_OP4w ago
GitHub
GitHub - Anulo2/BetterAuthIssue at sqlite
Contribute to Anulo2/BetterAuthIssue development by creating an account on GitHub.
bekacru
bekacru4w ago
IHmm, this in general isn't directly related to Better Auth. It’s specific to your setup. A 404 is only returned if Better Auth's router couldn't match the route to any endpoint. Since /get-session exists as an endpoint, the first thing to check is whether the /api/auth prefix is matching as expected by Better Auth. From the repo, I couldn't see how that would be the case, but that's one possibility. Another possibility is that you're not using the GET method, though I don’t think that’s the issue. I'll try to pull and check this
_Zaizen_
_Zaizen_OP4w ago
THanks, I really can't figure out why the register/login works but the useSession doesn't... I think I'm going insane 🤣 (btw, thanks for the really fast responses, this has been great, will definetly recommend better-auth to other devs in the future, you really care about the DX)
bekacru
bekacru4w ago
what endpoints are you hitting for register and login?
_Zaizen_
_Zaizen_OP4w ago
http://127.0.0.1:3000/api/auth/sign-in/email but everything is handled by authClient.signIn.email
bekacru
bekacru4w ago
mount the openAPI plugin and see if getSession works
_Zaizen_
_Zaizen_OP4w ago
What if in the swagger dashboard there's none of the auth routes listed? Btw in the example the swagger plugin for elysia is already loaded in I FOUND THE F*ING ISSUE the static plugin .use(staticPlugin({ prefix: "/" })) for some strange reason eats away that path by commenting that line everything start working fine wow, this has been a pain to find. @bekacru @Aziz
bekacru
bekacru4w ago
great! glad you found it!
_Zaizen_
_Zaizen_OP4w ago
I will write about this on the elhsia discord too so we can understand fully what's happening. Will report back if I get any info https://discordapp.com/channels/1044804142461362206/1336771163598356560
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
_Zaizen_
_Zaizen_OP4w ago
Already tried putting the static plugin after the api route but that doesn't solve the issue

Did you find this page helpful?