Patterns for implementing authentication and authorization in backend applications
View on GitHubplugins/aai-dev-backend/skills/authentication-patterns/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-dev-backend/skills/authentication-patterns/SKILL.md -a claude-code --skill authentication-patternsInstallation paths:
.claude/skills/authentication-patterns/# Authentication Patterns Skill
Patterns for implementing secure authentication and authorization.
## Authentication Methods
### JWT (JSON Web Tokens)
```typescript
import jwt from 'jsonwebtoken';
const JWT_SECRET = process.env.JWT_SECRET!;
const JWT_EXPIRES_IN = '15m';
const REFRESH_TOKEN_EXPIRES_IN = '7d';
// Generate tokens
function generateTokens(userId: string) {
const accessToken = jwt.sign(
{ userId, type: 'access' },
JWT_SECRET,
{ expiresIn: JWT_EXPIRES_IN }
);
const refreshToken = jwt.sign(
{ userId, type: 'refresh' },
JWT_SECRET,
{ expiresIn: REFRESH_TOKEN_EXPIRES_IN }
);
return { accessToken, refreshToken };
}
// Verify token
function verifyToken(token: string): JwtPayload {
return jwt.verify(token, JWT_SECRET) as JwtPayload;
}
// Refresh token endpoint
app.post('/auth/refresh', async (req, res) => {
const { refreshToken } = req.body;
try {
const payload = verifyToken(refreshToken);
if (payload.type !== 'refresh') {
throw new Error('Invalid token type');
}
// Check if refresh token is still valid in database
const storedToken = await getStoredRefreshToken(refreshToken);
if (!storedToken || storedToken.revoked) {
throw new Error('Token revoked');
}
// Generate new tokens
const tokens = generateTokens(payload.userId);
// Revoke old refresh token
await revokeRefreshToken(refreshToken);
// Store new refresh token
await storeRefreshToken(tokens.refreshToken, payload.userId);
res.json(tokens);
} catch (error) {
res.status(401).json({ error: 'Invalid refresh token' });
}
});
```
### API Keys
```typescript
// Generate API key
function generateApiKey(): string {
return `sk_${crypto.randomBytes(32).toString('hex')}`;
}
// Hash API key for storage
function hashApiKey(key: string): string {
return crypto.createHash('sha256').update(key).digest('hex');
}
// Validate API key
async function validateApiKey(key: string) {
const has