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 देखें।
Related Posts
Claude Code से Productivity 3 गुना बढ़ाने की 10 Tips
Claude Code से ज़्यादा पाने की 10 practical tips जानें। Prompt strategies से workflow shortcuts तक, ये techniques आज से ही आपकी efficiency boost करेंगी।
Claude Code के साथ Canvas/WebGL Optimization
Claude Code का उपयोग करके Canvas/WebGL optimization के बारे में जानें। Practical tips और code examples शामिल हैं।
Claude Code के साथ Markdown Implementation
Claude Code का उपयोग करके markdown implementation सीखें। Practical tips और code examples शामिल हैं।