In a typical JWT authentication flow using Astro, what is the immediate next step after the server verifies the user's credentials?
Think about what the server needs to provide so the client can prove its identity in future requests.
After verifying credentials, the server creates a JWT token that the client stores. This token is used to authenticate future requests without needing to log in again.
Given this Astro server code snippet that verifies a JWT token, what will be the value of user if the token is invalid?
import jwt from 'jsonwebtoken'; const token = request.headers.get('Authorization')?.split(' ')[1]; let user; try { user = jwt.verify(token, 'secretKey'); } catch { user = null; } return user;
What happens when jwt.verify fails inside a try-catch?
If the token is invalid, jwt.verify throws an error caught by the catch block, setting user to null.
Choose the correct code snippet that sets a JWT token as an HTTP-only cookie in an Astro server response.
Remember to include security flags for cookies that hold sensitive tokens.
Option A correctly sets the cookie with Secure and HttpOnly flags, path, and expiration time, which is best practice for JWT cookies.
Review this Astro server code snippet that tries to verify a JWT token but always throws an error. What is the cause?
import jwt from 'jsonwebtoken'; const token = request.headers.get('Authorization'); const user = jwt.verify(token, 'secretKey'); return user;
Check the format of the token string passed to jwt.verify.
The token string includes the 'Bearer ' prefix, so jwt.verify receives an invalid token format causing an error. The prefix must be removed before verification.
Consider an Astro app that stores JWT tokens in localStorage. What is the biggest security risk of this approach?
Think about what malicious scripts can do if they run on your page.
Storing JWT tokens in localStorage exposes them to theft by malicious scripts injected via XSS attacks, since localStorage is accessible by JavaScript.
