name: identity
description: “用户身份、OAuth连接和设备管理”
emoji: “🪪”
身份 - 完整API参考
管理用户身份、OAuth提供商连接和设备认证。
聊天命令
查看身份
/identity 显示您的身份
/identity status 认证状态
/identity devices 列出链接设备
OAuth提供商
/identity providers 列出可用提供商
/identity link google 连接Google账户
/identity link github 连接GitHub账户
/identity unlink google 断开提供商连接
设备管理
/identity device list 列出设备
/identity device name "工作笔记本" 命名此设备
/identity device revoke <id> 撤销设备访问
/identity device revoke-all 撤销除当前外的所有设备
信任与安全
/identity trust 查看信任级别
/identity sessions 活动会话
/identity session logout <id> 结束会话
/identity security 安全设置
TypeScript API参考
创建身份服务
import { createIdentityService } from 'clodds/identity';
const identity = createIdentityService({
// OAuth提供商
providers: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
},
github: {
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
},
},
// 会话设置
sessionDurationMs: 86400000 * 30, // 30天
deviceTrustDurationMs: 86400000 * 90, // 90天
// 存储
storage: 'sqlite',
dbPath: './identity.db',
});
获取用户身份
const user = await identity.getUser(userId);
console.log(`ID: ${user.id}`);
console.log(`Name: ${user.displayName}`);
console.log(`Email: ${user.email}`);
console.log(`Trust level: ${user.trustLevel}`);
console.log(`Created: ${user.createdAt}`);
链接OAuth提供商
// 生成OAuth URL
const authUrl = identity.getOAuthUrl('google', {
redirectUri: 'https://your-domain.com/auth/callback',
state: 'random-state-string',
scopes: ['email', 'profile'],
});
// 处理回调
const result = await identity.handleOAuthCallback('google', {
code: 'oauth-code-from-callback',
state: 'random-state-string',
});
console.log(`Linked: ${result.provider}`);
console.log(`Email: ${result.email}`);
列出链接的提供商
const providers = await identity.getLinkedProviders(userId);
for (const provider of providers) {
console.log(`${provider.name}: ${provider.email}`);
console.log(` Linked: ${provider.linkedAt}`);
console.log(` Last used: ${provider.lastUsed}`);
}
取消链接提供商
await identity.unlinkProvider(userId, 'google');
设备管理
// 列出设备
const devices = await identity.getDevices(userId);
for (const device of devices) {
console.log(`${device.id}: ${device.name || 'Unknown'}`);
console.log(` Type: ${device.type}`); // 'desktop' | 'mobile' | 'tablet'
console.log(` Browser: ${device.browser}`);
console.log(` OS: ${device.os}`);
console.log(` Last seen: ${device.lastSeen}`);
console.log(` Current: ${device.isCurrent}`);
}
// 命名设备
await identity.nameDevice(userId, deviceId, '工作笔记本');
// 撤销设备
await identity.revokeDevice(userId, deviceId);
// 撤销除当前外的所有设备
await identity.revokeAllDevices(userId, { exceptCurrent: true });
会话管理
// 列出活动会话
const sessions = await identity.getSessions(userId);
for (const session of sessions) {
console.log(`${session.id}: ${session.device}`);
console.log(` Started: ${session.startedAt}`);
console.log(` Last active: ${session.lastActive}`);
console.log(` IP: ${session.ip}`);
}
// 结束会话
await identity.endSession(sessionId);
// 结束所有会话
await identity.endAllSessions(userId);
信任级别
// 获取信任级别
const trust = await identity.getTrustLevel(userId);
console.log(`Trust: ${trust}`); // 'owner' | 'paired' | 'stranger'
// 设置信任级别(仅管理员)
await identity.setTrustLevel(userId, 'paired');
信任级别
| 级别 |
访问权限 |
| owner |
完全管理员访问 |
| paired |
标准用户访问 |
| stranger |
无访问(必须配对) |
OAuth提供商
| 提供商 |
范围 |
| Google |
email, profile |
| GitHub |
user:email |
| Discord |
identify, email |
| Twitter |
users.read |
设备类型
| 类型 |
检测 |
desktop |
Windows, macOS, Linux |
mobile |
iOS, Android |
tablet |
iPad, Android平板 |
unknown |
无法识别的用户代理 |
最佳实践
- 链接多个提供商 — 备份认证方法
- 定期审查设备 — 撤销未使用的设备
- 命名您的设备 — 更容易识别
- 检查会话 — 监控可疑访问
- 使用强认证 — 优先使用OAuth而非密码