Getting 404 for all API endpoints - SvelteKit #279

Closed
opened 2026-03-13 07:40:34 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @anatoliy-t7 on GitHub (Nov 25, 2024).

Getting 404 Error for all API endpoints on a server - SvelteKit

POST https://mydomain.com/api/auth/sign-in/email?currentURL=https%3A%2F%mydomain.com%2Fauth%2Flogin 404 (Not Found)
POST https://mydomain.com/api/auth/sign-up/email?currentURL=https%3A%2F%mydomain.com%2Fauth%2Fregister 404 (Not Found)

It is working localhost, but not on a server.
I set up baseURL: PUBLIC_DOMAIN, for a server and a client instance.

Desktop

  • OS: Mac OS latest

  • Browser chrome

  • Version latest

  • better-auth 1.0.3

  • SvelteKit (Svelte 5)

  • I do not use SSR

Originally created by @anatoliy-t7 on GitHub (Nov 25, 2024). Getting 404 Error for all API endpoints on a server - SvelteKit ``` js POST https://mydomain.com/api/auth/sign-in/email?currentURL=https%3A%2F%mydomain.com%2Fauth%2Flogin 404 (Not Found) POST https://mydomain.com/api/auth/sign-up/email?currentURL=https%3A%2F%mydomain.com%2Fauth%2Fregister 404 (Not Found) ``` It is working `localhost`, but not on a server. I set up `baseURL: PUBLIC_DOMAIN,` for a server and a client instance. **Desktop** - OS: Mac OS latest - Browser chrome - Version latest - better-auth 1.0.3 - SvelteKit (Svelte 5) - I do not use SSR
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

make sure to mount the handler properly on your server. And you can check /api/auth/ok to check if the server is properly running.

@Bekacru commented on GitHub (Nov 26, 2024): make sure to mount the handler properly on your server. And you can check `/api/auth/ok` to check if the server is properly running.
Author
Owner

@anatoliy-t7 commented on GitHub (Nov 26, 2024):

Currently:

const response = await svelteKitHandler({ event, resolve, auth });
response.headers.append('Access-Control-Allow-Origin', PUBLIC_DOMAIN);
return response;

and I tried

return svelteKitHandler({ event, resolve, auth });
export const auth = betterAuth({
	appName: PUBLIC_APP_NAME,
	baseURL: PUBLIC_DOMAIN,
	secret: BETTER_AUTH_SECRET,
	database: prismaAdapter(db, {
		provider: 'postgresql'
	}),
	emailAndPassword: {
		enabled: true,
		requireEmailVerification: false,
		minPasswordLength: 8,
		autoSignIn: true,
	},
})
@anatoliy-t7 commented on GitHub (Nov 26, 2024): Currently: ```js const response = await svelteKitHandler({ event, resolve, auth }); response.headers.append('Access-Control-Allow-Origin', PUBLIC_DOMAIN); return response; ``` and I tried ```js return svelteKitHandler({ event, resolve, auth }); ``` ```js export const auth = betterAuth({ appName: PUBLIC_APP_NAME, baseURL: PUBLIC_DOMAIN, secret: BETTER_AUTH_SECRET, database: prismaAdapter(db, { provider: 'postgresql' }), emailAndPassword: { enabled: true, requireEmailVerification: false, minPasswordLength: 8, autoSignIn: true, }, }) ```
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

can't tell much from here, but if 404 is returned that means the handler isn't properly mounted in hooks.server.ts. Make sure to follow the guide in the docs or the sveltekit example in this repo.

@Bekacru commented on GitHub (Nov 26, 2024): can't tell much from here, but if 404 is returned that means the handler isn't properly mounted in `hooks.server.ts`. Make sure to follow the guide in the docs or the sveltekit example in this repo.
Author
Owner

@anatoliy-t7 commented on GitHub (Nov 26, 2024):

I figured it out.

env was not set via:

import { PUBLIC_DOMAIN } from '$env/static/private';

baseURL:  PUBLIC_DOMAIN
baseURL:  process.env.PUBLIC_DOMAIN
baseURL:  import.meta.env.PUBLIC_DOMAIN

Works only:

import { env } from '$env/dynamic/private';
baseURL:  env.PUBLIC_DOMAIN
@anatoliy-t7 commented on GitHub (Nov 26, 2024): I figured it out. env was not set via: ``` import { PUBLIC_DOMAIN } from '$env/static/private'; baseURL: PUBLIC_DOMAIN baseURL: process.env.PUBLIC_DOMAIN baseURL: import.meta.env.PUBLIC_DOMAIN ``` Works only: ``` import { env } from '$env/dynamic/private'; baseURL: env.PUBLIC_DOMAIN ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#279