New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SvelteKit] Error: The "host" request header is not available at isSecureEnvironment - When fetching from API routes #408
Comments
I'm new to coding so take it with a grain of salt. I was having the same issue, and I think it's because the fetch at /api/product/+server.ts is not receiving the host header from hooks.server.ts. I added a handleFetch function like this in my hooks.server.ts and it solved the issue for me.
|
Inside hooks.server.ts It seems like the request does not include a |
this then blocs +layout.ts where I try to get supabase user import type { LayoutLoad } from './$types'
import { getSupabase } from '@supabase/auth-helpers-sveltekit'
export const load: LayoutLoad = async (event) => {
const { session } = await getSupabase(event)
console.log("LayoutLoad. User = ", session?.user.id);
return { session }
} +hooks.server.ts import '$lib/supabaseClient'
import { getSupabase } from '@supabase/auth-helpers-sveltekit'
import type { Handle, HandleFetch } from '@sveltejs/kit'
export const handleFetch = (async ({event, request, fetch }) => {
// if (request.url.startsWith('https://api.my-domain.com/')) {
const host = event.request.headers.get('host')
if (host) request.headers.set('host', host);
// }
return fetch(request);
}) satisfies HandleFetch;
export const handle = (async ({ event, resolve }) => {
console.log("Ran HooksServerHandle");
const { session, supabaseClient } = await getSupabase(event)
event.locals.supabaseServerClient = supabaseClient
event.locals.session = session
return resolve(event)
}) satisfies Handle; console log |
One thing i noticed is that you are saving the supabase client to locals. |
For the moment you can also get rid of this error by manually settings the import { createClient } from "@supabase/auth-helpers-sveltekit"
import {
PUBLIC_SUPABASE_ANON_KEY,
PUBLIC_SUPABASE_URL,
} from "$env/static/public"
export const supabaseClient = createClient(
PUBLIC_SUPABASE_URL,
PUBLIC_SUPABASE_ANON_KEY,
{},
{
secure: true
}
) |
yeah I followed this youtuber approach of handling authentication https://www.youtube.com/watch?v=lSm0GNnh-0I&t=341s and for the second answer is there documentation on that "secure: true" param? Also, I don't think it will solve my problem that handleFetch is blocking the Handle function but I will try it anyway. |
There isn´t currently but we are working on better docs. Manually settings the |
FYI, I am also facing the same issue, I does not seem to be breaking anything but is at least polluting the logs. |
Also having the same issue. Scenario:
|
This makes my error go away but I can't sign out from the app |
Can't replicate this issue. If this is still an issue provide an reproducible example as the one provided is using outdated code. |
Bug report
Describe the bug
When fetching from an API route from a client side or server side page, Supabase auth lib will throw an error:
Error: The "host" request header is not available at isSecureEnvironment
To Reproduce
Example repo to reproduce:
https://github.com/chanmathew/sk-supabase-authhelpers-host-request-header-issue
/routes/api/products/+server.ts
/routes/products/+page.server.ts
/products
page in browser and check logs in server console, you will see this error from Supabase auth helpers:Error: The "host" request header is not available at isSecureEnvironment
Expected behavior
This error should not be thrown, especially given the API route is not fetching data from Supabase and is completely unrelated.
System information
The text was updated successfully, but these errors were encountered: