Repository navigation
Better auth routes end up with 404 while trying to test it via ngrok #15831
Replies: 2 comments 6 replies
|
The 404 on Better Auth routes (like email verification links) through ngrok is almost always a Better Auth validates the origin of incoming requests. When you use ngrok, the domain changes from Fix: update your Better Auth config to include the ngrok URL: // src/lib/auth.ts (or wherever your auth config is)
export const auth = betterAuth({
baseURL: process.env.BETTER_AUTH_URL, // set this to your ngrok URL
trustedOrigins: [
"http://localhost:5173",
"https://your-ngrok-subdomain.ngrok-free.app" // add ngrok URL
],
// ...
});Also make sure For the email verification specifically, the flow is:
Check your If you're using ngrok's free tier, note that the URL changes each session. You'd need to update the config each time, or use a fixed subdomain with a paid plan. |
|
I've been struggling with BetterAuth not registering its routes for a few hours now and I think I found the issue. This is the code at const toSvelteKitHandler = auth => {
return event => auth.handler(event.request)
}
const svelteKitHandler = async ({ auth, event, resolve, building }) => {
if (building) return resolve(event)
const { request, url } = event
if (isAuthPath(url.toString(), auth.options)) return auth.handler(request)
return resolve(event)
}
function isAuthPath(url, options) {
const _url = new URL(url)
const baseURLStr =
typeof options.baseURL === "string" ? options.baseURL : void 0
const baseURL = new URL(
`${baseURLStr || _url.origin}${options.basePath || "/api/auth"}`,
)
if (_url.origin !== baseURL.origin) return false
if (
!_url.pathname.startsWith(
baseURL.pathname.endsWith("/")
? baseURL.pathname
: `${baseURL.pathname}/`,
)
)
return false
return true
}The function We can verify this by completely removing export const authOptions: BetterAuthOptions & {
readonly plugins: readonly [AdminPlugin, UsernamePlugin, SvelteKitCookiesPlugin]
readonly database: Database<DB>
} = {
appName: pkg.name,
// baseURL: BETTER_AUTH_URL.toString(),Result: But if we add it back: logger.info({ BETTER_AUTH_URL }, 'BETTER_AUTH_URL')
export const authOptions: BetterAuthOptions & {
readonly plugins: readonly [AdminPlugin, UsernamePlugin, SvelteKitCookiesPlugin]
readonly database: Database<DB>
} = {
appName: pkg.name,
baseURL: BETTER_AUTH_URL.toString(),We get a 404: To fix it, we can copy const handleBetterAuth: Handle = (async ({ event, resolve }) => {
if (building) return resolve(event)
const { request, url } = event
if (isAuthPath(url, auth.options)) return auth.handler(request)
return resolve(event)
}) satisfies Handle
function isAuthPath(url: URL, options: BetterAuthOptions) {
return url.pathname.startsWith(options.basePath || "/api/auth/")
}We can replace |
Uh oh!
There was an error while loading. Please reload this page.
As the title says, I update the
baseUrlto the temp ngrok url while testing. The cookies work fine, I can use the app with a registered user. But none of the BA routes work. I have the email verification enabled and I can't verify emails, because the verify link lands on 404. The url itself is correct, it has the correct domain, but it just throws 404.All reactions