Complete the code to add a Bearer token to the fetch request headers.
const response = await fetch('/api/data', { headers: { 'Authorization': '[1]' } });
The standard way to send a token in HTTP headers is using the 'Bearer' scheme.
Complete the code to store the JWT token in localStorage after login.
function handleLogin(token) {
[1].setItem('jwt', token);
}localStorage is commonly used to store tokens persistently in the browser.
Fix the error in the Svelte fetch call to include the token from a reactive variable.
let token = ''; async function fetchData() { const res = await fetch('/api/secure', { headers: { 'Authorization': '[1]' } }); return await res.json(); }
Use template literals with backticks and ${} to insert the token variable value.
Fill both blanks to create a Svelte store that holds the auth token and a function to clear it.
import { writable } from 'svelte/store'; export const authToken = writable([1]); export function clearToken() { authToken.[2](''); }
The writable store is initialized with an empty string. The set method updates the store value.
Fill all three blanks to create an authenticated fetch function that adds an Authorization header with the token to fetch calls.
import { get } from 'svelte/store'; import { authToken } from './stores'; export async function authFetch(url, options = {}) { const token = get([1]); options.headers = { ...options.headers, 'Authorization': [2] }; return fetch(url, options); } // Usage: authFetch('/api/data', { method: [3] })
Get the token from the authToken store, add it as a Bearer token in headers, and use 'GET' as the default method.