0
0
Svelteframework~20 mins

API authentication patterns in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Authentication Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the Svelte component tries to fetch data with expired token?

Consider a Svelte component that fetches user data from an API using a stored JWT token. The token is expired. What will be the component's behavior?

Svelte
import { onMount } from 'svelte';
let userData = null;
let error = null;
const token = 'expired.jwt.token';

onMount(async () => {
  try {
    const res = await fetch('https://api.example.com/user', {
      headers: { 'Authorization': `Bearer ${token}` }
    });
    if (!res.ok) throw new Error('Unauthorized');
    userData = await res.json();
  } catch (e) {
    error = e.message;
  }
});
AThe component retries fetching automatically until token is valid.
BThe component successfully fetches user data despite expired token.
CThe component throws a runtime error and crashes.
DThe component sets error to 'Unauthorized' and userData remains null.
Attempts:
2 left
💡 Hint

Think about what happens when the API rejects the request due to expired token.

📝 Syntax
intermediate
1:30remaining
Which Svelte code correctly adds a Bearer token to fetch headers?

Choose the correct way to add a Bearer token to the fetch request headers in a Svelte component.

Afetch(url, { headers: { 'Authorization': Bearer token } })
Bfetch(url, { headers: { Authorization: 'Bearer ' + token } })
Cfetch(url, { headers: { Authorization: `Bearer ${token}` } })
Dfetch(url, { headers: { Authorization: 'token Bearer' } })
Attempts:
2 left
💡 Hint

Remember how to use template literals to insert variables inside strings.

state_output
advanced
2:00remaining
What is the value of 'isAuthenticated' after token refresh in this Svelte component?

Given a Svelte component that refreshes an expired token and updates authentication state, what is the final value of isAuthenticated?

Svelte
import { onMount } from 'svelte';
import { writable } from 'svelte/store';

const isAuthenticated = writable(false);
let token = 'expired.jwt.token';

async function refreshToken() {
  const res = await fetch('https://api.example.com/refresh', {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${token}` }
  });
  if (res.ok) {
    const data = await res.json();
    token = data.token;
    isAuthenticated.set(true);
  } else {
    isAuthenticated.set(false);
  }
}

onMount(async () => {
  await refreshToken();
});
Aundefined
Btrue
Cfalse
Dnull
Attempts:
2 left
💡 Hint

If the refresh API call succeeds, what does the code do to isAuthenticated?

🔧 Debug
advanced
2:00remaining
Why does this Svelte component fail to send Authorization header?

Identify the reason why the Authorization header is missing in the fetch request below.

Svelte
let token = 'abc123';

async function getData() {
  const response = await fetch('https://api.example.com/data', {
    headers: { Authorization: `Bearer ${token}` }
  });
  return await response.json();
}

getData();
AThe string 'Bearer token' is literal; it should use the variable token instead.
BThe fetch call is missing the method property.
CHeaders object keys must be lowercase 'authorization'.
DThe token variable is not declared with const.
Attempts:
2 left
💡 Hint

Check how the token variable is used inside the string.

🧠 Conceptual
expert
2:30remaining
Which authentication pattern best fits a Svelte app needing secure API access with token refresh?

Choose the best API authentication pattern for a Svelte app that must securely access APIs and handle token expiration by refreshing tokens automatically.

AStore JWT in localStorage and refresh token on 401 responses using a fetch wrapper.
BSend username and password with every API request to avoid token management.
CStore JWT in memory only and reload the app to get a new token when expired.
DStore JWT in a cookie without HttpOnly flag and refresh token manually via user action.
Attempts:
2 left
💡 Hint

Consider security and user experience for token storage and refresh.