auth0-authentication by mindrally/skills
npx skills add https://github.com/mindrally/skills --skill auth0-authentication您是 Auth0 身份验证实施方面的专家。在任何项目中使用 Auth0 时,请遵循以下指南。
# Required Auth0 Configuration
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secret
AUTH0_AUDIENCE=your-api-audience
AUTH0_CALLBACK_URL=https://your-app.com/callback
AUTH0_LOGOUT_URL=https://your-app.com
对于公共客户端,始终使用 PKCE:
import { Auth0Client } from '@auth0/auth0-spa-js';
const auth0 = new Auth0Client({
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
authorizationParams: {
redirect_uri: window.location.origin,
audience: process.env.AUTH0_AUDIENCE,
},
cacheLocation: 'localstorage', // 使用 'memory' 以获得更高的安全性
useRefreshTokens: true,
});
// Express.js 示例
const { auth } = require('express-openid-connect');
app.use(
auth({
authRequired: false,
auth0Logout: true,
secret: process.env.AUTH0_SECRET,
baseURL: process.env.BASE_URL,
clientID: process.env.AUTH0_CLIENT_ID,
issuerBaseURL: `https://${process.env.AUTH0_DOMAIN}`,
})
);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
Actions 已取代 Rules。请遵循以下指南:
exports.onExecutePostLogin = async (event, api) => {
// 1. 为提高效率,尽早返回
if (!event.user.email_verified) {
api.access.deny('Please verify your email before logging in.');
return;
}
// 2. 对敏感数据使用 secrets(在 Auth0 仪表板中配置)
const apiKey = event.secrets.EXTERNAL_API_KEY;
// 3. 尽量减少外部调用 - 它们会影响登录延迟
// 4. 切勿记录敏感信息
console.log(`User logged in: ${event.user.user_id}`);
// 5. 谨慎添加自定义声明
api.idToken.setCustomClaim('https://myapp.com/roles', event.authorization?.roles || []);
api.accessToken.setCustomClaim('https://myapp.com/roles', event.authorization?.roles || []);
};
// 始终在服务器端验证令牌
const { auth, requiredScopes } = require('express-oauth2-jwt-bearer');
const checkJwt = auth({
audience: process.env.AUTH0_AUDIENCE,
issuerBaseURL: `https://${process.env.AUTH0_DOMAIN}/`,
tokenSigningAlg: 'RS256',
});
// 要求特定作用域
const checkScopes = requiredScopes('read:messages');
app.get('/api/private-scoped', checkJwt, checkScopes, (req, res) => {
res.json({ message: 'Protected resource' });
});
// 状态参数由 Auth0 SDK 自动处理
// 对于自定义实现,始终验证状态参数
const state = generateSecureRandomString();
sessionStorage.setItem('auth0_state', state);
// 实施会话超时
const sessionConfig = {
absoluteDuration: 86400, // 24 小时
inactivityDuration: 3600, // 1 小时不活动
};
// 对敏感操作强制执行 MFA
exports.onExecutePostLogin = async (event, api) => {
// 检查 MFA 是否已完成
if (!event.authentication?.methods?.find(m => m.name === 'mfa')) {
// 触发 MFA 质询
api.authentication.challengeWithAny([
{ type: 'otp' },
{ type: 'push-notification' },
]);
}
};
try {
await auth0.loginWithRedirect();
} catch (error) {
if (error.error === 'access_denied') {
// 用户拒绝访问或电子邮件未验证
handleAccessDenied(error);
} else if (error.error === 'login_required') {
// 会话已过期
handleSessionExpired();
} else {
// 通用错误处理
console.error('Authentication error:', error.message);
showUserFriendlyError();
}
}
Auth0 提供了一个用于 AI 辅助开发的 MCP 服务器:
# 为 Cursor 初始化 Auth0 MCP 服务器
npx @auth0/auth0-mcp-server init --client cursor
这使您可以在 IDE 内进行自然语言的 Auth0 管理操作。
每周安装量
107
代码仓库
GitHub 星标数
43
首次出现
2026 年 1 月 25 日
安全审计
安装于
gemini-cli89
opencode89
codex85
cursor81
claude-code80
github-copilot80
You are an expert in Auth0 authentication implementation. Follow these guidelines when working with Auth0 in any project.
# Required Auth0 Configuration
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secret
AUTH0_AUDIENCE=your-api-audience
AUTH0_CALLBACK_URL=https://your-app.com/callback
AUTH0_LOGOUT_URL=https://your-app.com
Always use PKCE for public clients:
import { Auth0Client } from '@auth0/auth0-spa-js';
const auth0 = new Auth0Client({
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
authorizationParams: {
redirect_uri: window.location.origin,
audience: process.env.AUTH0_AUDIENCE,
},
cacheLocation: 'localstorage', // Use 'memory' for higher security
useRefreshTokens: true,
});
// Express.js example
const { auth } = require('express-openid-connect');
app.use(
auth({
authRequired: false,
auth0Logout: true,
secret: process.env.AUTH0_SECRET,
baseURL: process.env.BASE_URL,
clientID: process.env.AUTH0_CLIENT_ID,
issuerBaseURL: `https://${process.env.AUTH0_DOMAIN}`,
})
);
Actions have replaced Rules. Follow these guidelines:
exports.onExecutePostLogin = async (event, api) => {
// 1. Early returns for efficiency
if (!event.user.email_verified) {
api.access.deny('Please verify your email before logging in.');
return;
}
// 2. Use secrets for sensitive data (configured in Auth0 Dashboard)
const apiKey = event.secrets.EXTERNAL_API_KEY;
// 3. Minimize external calls - they affect login latency
// 4. Never log sensitive information
console.log(`User logged in: ${event.user.user_id}`);
// 5. Add custom claims sparingly
api.idToken.setCustomClaim('https://myapp.com/roles', event.authorization?.roles || []);
api.accessToken.setCustomClaim('https://myapp.com/roles', event.authorization?.roles || []);
};
// Always validate tokens server-side
const { auth, requiredScopes } = require('express-oauth2-jwt-bearer');
const checkJwt = auth({
audience: process.env.AUTH0_AUDIENCE,
issuerBaseURL: `https://${process.env.AUTH0_DOMAIN}/`,
tokenSigningAlg: 'RS256',
});
// Require specific scopes
const checkScopes = requiredScopes('read:messages');
app.get('/api/private-scoped', checkJwt, checkScopes, (req, res) => {
res.json({ message: 'Protected resource' });
});
// State parameter is automatically handled by Auth0 SDKs
// For custom implementations, always validate the state parameter
const state = generateSecureRandomString();
sessionStorage.setItem('auth0_state', state);
// Implement session timeouts
const sessionConfig = {
absoluteDuration: 86400, // 24 hours
inactivityDuration: 3600, // 1 hour of inactivity
};
// Enforce MFA for sensitive operations
exports.onExecutePostLogin = async (event, api) => {
// Check if MFA has been completed
if (!event.authentication?.methods?.find(m => m.name === 'mfa')) {
// Trigger MFA challenge
api.authentication.challengeWithAny([
{ type: 'otp' },
{ type: 'push-notification' },
]);
}
};
try {
await auth0.loginWithRedirect();
} catch (error) {
if (error.error === 'access_denied') {
// User denied access or email not verified
handleAccessDenied(error);
} else if (error.error === 'login_required') {
// Session expired
handleSessionExpired();
} else {
// Generic error handling
console.error('Authentication error:', error.message);
showUserFriendlyError();
}
}
Auth0 provides an MCP server for AI-assisted development:
# Initialize Auth0 MCP server for Cursor
npx @auth0/auth0-mcp-server init --client cursor
This enables natural language Auth0 management operations within your IDE.
Weekly Installs
107
Repository
GitHub Stars
43
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli89
opencode89
codex85
cursor81
claude-code80
github-copilot80
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
96,200 周安装