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
The code in the backend is something like this:
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•4w ago
Message Not Public
Sign In & Join Server To View
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?
pass the api url in the
baseURL
paramWait 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•4w ago
Message Not Public
Sign In & Join Server To View
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
https://github.com/Anulo2/BetterAuthIssue @Aziz @bekacru
GitHub
GitHub - Anulo2/BetterAuthIssue
Contribute to Anulo2/BetterAuthIssue development by creating an account on GitHub.
Should I open an issue too about this?
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?
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
this is weird. try it on a http client instead. Also this is localhost, you said it works locally.
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
https://github.com/Anulo2/BetterAuthIssue/tree/sqlite sqlite edition
GitHub
GitHub - Anulo2/BetterAuthIssue at sqlite
Contribute to Anulo2/BetterAuthIssue development by creating an account on GitHub.
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 thisTHanks, 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)
what endpoints are you hitting for register and login?
http://127.0.0.1:3000/api/auth/sign-in/email
but everything is handled by authClient.signIn.email
mount the openAPI plugin and see if getSession works
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
great! glad you found it!
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•4w ago
Message Not Public
Sign In & Join Server To View
Already tried putting the static plugin after the api route but that doesn't solve the issue