Claude Code के साथ Cookie और Session Management कैसे Implement करें
Claude Code का उपयोग करके cookie और session management implement करना सीखें। Practical code examples और step-by-step guidance शामिल है।
Cookie Management का महत्व
Cookie user authentication और session management का आधार है, लेकिन security setting में गलती गंभीर vulnerability बन सकती है। Claude Code का उपयोग करके, secure cookie management system सही तरीके से implement किया जा सकता है।
Safe Cookie Operation Utility
> Security settings सहित cookie operation utility बनाओ।
> HttpOnly, Secure, SameSite settings अनिवार्य बनाओ।
interface CookieOptions {
maxAge?: number;
path?: string;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: 'strict' | 'lax' | 'none';
}
const DEFAULT_OPTIONS: CookieOptions = {
path: '/',
secure: true,
httpOnly: true,
sameSite: 'lax',
maxAge: 86400, // 1 दिन
};
class CookieManager {
static set(name: string, value: string, options: CookieOptions = {}) {
const opts = { ...DEFAULT_OPTIONS, ...options };
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
if (opts.maxAge) parts.push(`Max-Age=${opts.maxAge}`);
if (opts.path) parts.push(`Path=${opts.path}`);
if (opts.domain) parts.push(`Domain=${opts.domain}`);
if (opts.secure) parts.push('Secure');
if (opts.httpOnly) parts.push('HttpOnly');
if (opts.sameSite) parts.push(`SameSite=${opts.sameSite}`);
return parts.join('; ');
}
static parse(cookieHeader: string): Record<string, string> {
return cookieHeader.split(';').reduce((acc, pair) => {
const [key, ...vals] = pair.trim().split('=');
if (key) acc[decodeURIComponent(key)] = decodeURIComponent(vals.join('='));
return acc;
}, {} as Record<string, string>);
}
static delete(name: string, path = '/') {
return this.set(name, '', { maxAge: 0, path });
}
}
Express में Session Management
> Express में secure session management implement करो।
> Redis को session store बनाओ और CSRF protection भी लगाओ।
import session from 'express-session';
import RedisStore from 'connect-redis';
import { createClient } from 'redis';
import csrf from 'csurf';
const redisClient = createClient({ url: process.env.REDIS_URL });
redisClient.connect();
const sessionConfig: session.SessionOptions = {
store: new RedisStore({ client: redisClient }),
secret: process.env.SESSION_SECRET!,
name: '__session',
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
sameSite: 'lax',
maxAge: 24 * 60 * 60 * 1000, // 24 घंटे
},
};
app.use(session(sessionConfig));
app.use(csrf({ cookie: false })); // Session-based CSRF
Client Side पर Safe Cookie Operations
Browser side पर operate करने वाली cookie (HttpOnly नहीं) की management भी Claude Code से generate करवा सकते हैं।
// Client side cookie utility (theme और language जैसी non-sensitive information के लिए)
export const clientCookie = {
get(name: string): string | null {
const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`));
return match ? decodeURIComponent(match[1]) : null;
},
set(name: string, value: string, days = 365) {
const expires = new Date(Date.now() + days * 864e5).toUTCString();
document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)};expires=${expires};path=/;SameSite=Lax`;
},
remove(name: string) {
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
},
};
// Usage example: Theme setting save करना
clientCookie.set('theme', 'dark');
const theme = clientCookie.get('theme'); // 'dark'
Session Security मज़बूत करना
// Session fixation attack prevention
app.post('/login', async (req, res) => {
const user = await authenticate(req.body);
// Login success पर session ID regenerate करें
req.session.regenerate((err) => {
if (err) return res.status(500).json({ error: 'Session error' });
req.session.userId = user.id;
res.json({ success: true });
});
});
Summary
Claude Code का उपयोग करके, secure cookie settings से session management, CSRF protection तक consistently implement किया जा सकता है। Authentication overall के लिए authentication implementation guide, JWT authentication से comparison के लिए JWT authentication article देखें। Security details के लिए security audit भी reference बन सकता है।
Session management best practices के लिए OWASP Session Management देखें।
मुफ़्त PDF: 5 मिनट में Claude Code चीटशीट
बस अपना ईमेल दर्ज करें और हम तुरंत A4 एक-पृष्ठ चीटशीट PDF भेज देंगे।
हम आपकी व्यक्तिगत जानकारी की सुरक्षा करते हैं और स्पैम नहीं भेजते।
लेखक के बारे में
Masa
Claude Code का गहराई से उपयोग करने वाले इंजीनियर। claudecode-lab.com चलाते हैं, जो 10 भाषाओं में 2,000 से अधिक पेजों वाला टेक मीडिया है।
संबंधित लेख
Claude Code ke liye 7 CLAUDE.md templates jo aap real projects me copy kar sakte hain
Solo app, content site, API, team repo aur legacy codebase ke liye 7 practical CLAUDE.md templates, plus common failure cases.
Claude Code Approval aur Sandbox Guide | Roz ke kaam ke liye safe settings
Claude Code me allow, ask, deny aur sandbox ko kaise baantna chahiye - practical settings, hooks aur real workflow examples ke saath.
Claude Code की सम्पूर्ण शुरुआती गाइड 2026 | शून्य से प्रोफेशनल उपयोग तक 7 स्टेप्स में
पहली बार Claude Code उपयोग करने वालों के लिए पूरी गाइड। इंस्टॉलेशन से लेकर असली डेवलपमेंट वर्कफ्लो में शामिल करने तक — Masa के शुरुआती अनुभव के आधार पर।