Skip to main content
skill CX Codex

session-management · secondsky — Claude Code skill: JWT, Redis sessions

A coding-agent skill for building sign-in: JWT access and refresh tokens, session storage in Redis, and cookie flags that keep tokens out of reach.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add secondsky/claude-skills --skill session-management -a claude-code
Open on GitHub

What session-management does

The skill walks through token-based sessions in JavaScript. It shows how to sign an access token and a refresh token with jsonwebtoken, a SessionStore class on top of the redis client with methods to create a session and wipe every session for a user, cookie settings with httpOnly, secure and sameSite, and a POST /auth/refresh handler that checks the token type before issuing a new pair.

A separate section lists the security rules: HTTPS only, httpOnly and sameSite on cookies, bounded token lifetimes, a distinct secret per environment, signature validation on every request. Then the anti-patterns — no sensitive data inside a token, no tokens in URL parameters, no weak or shared secrets, no skipping signature checks.

The examples read JWT_SECRET, REFRESH_SECRET and SESSION_SECRET from the environment. Leave those unset and nothing runs, so check your config before this code goes anywhere near production. What you get is reference code to paste into your own project and adapt, not a drop-in library.

What it helps with

  • issue an access and refresh token pair for a user
  • keep active sessions in Redis and revoke them all at once
  • configure cookies with httpOnly, secure and sameSite
  • build a refresh endpoint that validates the token type
  • close common holes like passing tokens in URL parameters

Example prompts for the agent

  • add JWT login to this project: one-hour access token, seven-day refresh token
  • store sessions in Redis with a method that clears all sessions for a user
  • review my session cookie settings: httpOnly, secure, sameSite

How to install session-management

1Skills CLI (Vercel)

npx skills add secondsky/claude-skills --skill session-management -a claude-code

2Manually: put the files in place

~/.claude/skills/session-management/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/session-management/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about session-management

How do I install session-management in Claude?

Run npx skills add secondsky/claude-skills --skill session-management -a claude-code. The skill file then lands at ~/.claude/skills/session-management/SKILL.md.

What does session-management do?

It shows how to build sessions on JWT: token generation, Redis storage, refresh flow and cookie configuration, plus a checklist of security requirements and mistakes to avoid.

What do the examples need to run?

The jsonwebtoken and redis packages, and the JWT_SECRET, REFRESH_SECRET and SESSION_SECRET environment variables. Without the secrets the sample code will not start.

Source text

SKILL.md · MIT
---
name: session-management
description: Implements secure session management with JWT tokens, Redis storage, refresh flows, and proper cookie configuration. Use when building authentication systems, managing user sessions, or implementing secure logout functionality.
license: MIT
---

# Session Management

Implement secure session management with proper token handling and storage.

## Token-Based Sessions

```javascript
const jwt = require('jsonwebtoken');

function generateTokens(user) {
  const accessToken = jwt.sign(
    { userId: user.id, role: user.role, type: 'access' },
    process.env.JWT_SECRET,
    { expiresIn: '1h' }
  );

  const refreshToken = jwt.sign(
    { userId: user.id, type: 'refresh' },
    process.env.REFRESH_SECRET,
    { expiresIn: '7d' }
  );

  return { accessToken, refreshToken };
}
```

## Redis Session Storage

```javascript
const redis = require('redis');
const client = redis.createClient();

class SessionStore {
  async create(userId, sessionData) {
    const sessionId = crypto.randomUUID();
    await client.hSet(`sessions:${userId}`, sessionId, JSON.stringify({
      ...sessionData,
      createdAt: Date.now()
    }));
    await client.expire(`sessions:${userId}`, 86400 * 7);
    return sessionId;
  }

  async invalidateAll(userId) {
    await client.del(`sessions:${userId}`);
  }
}
```

## Cookie Configuration

```javascript
app.use(session({
  name: 'session',
  secret: process.env.SESSION_SECRET,
  cookie: {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    sameSite: 'strict',
    maxAge: 3600000, // 1 hour
    domain: '.example.com'
  },
  resave: false,
  saveUninitialized: false
}));
```

## Token Refresh Flow

```javascript
app.post('/auth/refresh', async (req, res) => {
  const { refreshToken } = req.cookies;

  try {
    const payload = jwt.verify(refreshToken, process.env.REFRESH_SECRET);
    if (payload.type !== 'refresh') throw new Error('Invalid token type');

    const user = await User.findById(payload.userId);
    const tokens = generateTokens(user);

    res.cookie('accessToken', tokens.accessToken, cookieOptions);
    res.json({ success: true });
  } catch (err) {
    res.status(401).json({ error: 'Invalid refresh token' });
  }
});
```

## Security Requirements

- Use HTTPS exclusively
- Set httpOnly and sameSite on cookies
- Implement proper token expiration
- Use strong, unique secrets per environment
- Validate signatures on every request

## Never Do

- Store sensitive data in tokens
- Transmit tokens via URL parameters
- Use weak or shared secrets
- Skip signature validation

© secondsky · MIT · secondsky/claude-skills

Similar skills

Everything for Security →

session-management by other authors comes first.

From the same set89

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor