Performance: Why authentication needs server-side logic
This affects page load speed and interaction responsiveness by controlling secure data flow and reducing client-side processing.
// src/pages/api/login.js export async function POST({ request, cookies }) { const formData = await request.formData(); const username = formData.get('username'); const password = formData.get('password'); const valid = await verifyUserOnServer(username, password); if (valid) { // Set secure HTTP-only cookie cookies.set('auth', 'true', { httpOnly: true, secure: true, sameSite: 'strict' }); return new Response(JSON.stringify({ success: true }), { status: 200, headers: { 'Content-Type': 'application/json' } }); } return new Response(JSON.stringify({ success: false }), { status: 401, headers: { 'Content-Type': 'application/json' } }); };
const login = (username, password) => {
// Client-side only authentication
if (username === 'user' && password === 'pass') {
localStorage.setItem('auth', 'true');
return true;
}
return false;
};| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side authentication | Minimal DOM changes | 0 reflows | Low paint cost | [X] Bad - security risk and blocks interaction |
| Server-side authentication | Minimal DOM changes | 0 reflows | Low paint cost | [OK] Good - secure and responsive |
export async function post({ request }) {
const { username, password } = await request.json();
if (username === 'user' && password === 'pass') {
return new Response('Success', { status: 200 });
} else {
return new Response('Fail', { status: 401 });
}
}export async function post({ request }) {
const { username, password } = await request.json();
if (username = 'admin' && password = '1234') {
return new Response('Logged in', { status: 200 });
}
return new Response('Unauthorized', { status: 401 });
}Pass the test and this pattern moves unknown