Session-based Auth in Svelte

From the SvelteKit cheat sheet · Authentication · verified Jul 2026

Session-based Auth

Cookie-based authentication

javascript
// Login action
export const actions = {
  login: async ({ request, cookies }) => {
    const data = await request.formData();
    
    // Verify credentials...
    
    cookies.set('session', sessionId, {
      path: '/',
      httpOnly: true,
      secure: true,
      maxAge: 60 * 60 * 24 * 7
    });
    
    throw redirect(303, '/dashboard');
  }
};
🍪 Secure HTTP-only cookies for sessions
🔒 Server-side session validation
⚡ Automatic session extension
🛡️ Protected routes in hooks

More Svelte tasks

Back to the full SvelteKit cheat sheet