Cola Motorolla
Cola Motorolla

Reputation: 21

How can I fetch data by load method with authorization headers from Context="module"

I used the classic scheme with async onMount fetching. This works pretty well but now I want to use recommended way with Svelte's Kit load fetch.

In ssr mode I can't get the cookie data. I tried to use Hooks (External Fetch) but headers are empty.

Can this moment be resolved?

hooks.ts

/** @type {import('@sveltejs/kit').ExternalFetch} */
export async function externalFetch(request: any) {
    
    let headers: object = {}

    const token: string | undefined = parse(request.headers.get('Cookie'))['jwt'] || undefined;
    if (token){
        headers = {headers: {'Authorization': 'Bearer ' + jwt}};
    }
    
    return fetch(request, headers);
}

Upvotes: 1

Views: 1347

Answers (1)

Cola Motorolla
Cola Motorolla

Reputation: 21

I solved by changing the param in svelte.config.js to

prerender: {
            default :false
        },

after that I got session data and sent fetch with header

Upvotes: 1

Related Questions