AuthService 提供了一个全面的 API,用于管理用户账户的各个方面。它处理用户个人资料、隐私和通知设置、关注其他用户等社交互动,以及注销和删除账户等关键身份验证操作。您可以通过 SDK 实例的 sdk.auth 访问它。
该服务与 TokenService 紧密合作以管理身份验证状态,但在大多数操作中抽象了底层的令牌处理。
用户个人资料管理
这些方法允许您获取、更新和管理用户个人资料数据。
getProfile
检索当前已验证用户的完整个人资料。
const profile = await sdk.auth.getProfile();
console.log(profile);返回
- profile
Promise<object>— 一个解析为用户个人资料对象的 promise。
响应示例
{
"did": "z8ia29UsENBg6tLZUKi2HABj38Cw1LmHZocbQ",
"fullName": "John Doe",
"email": "john.doe@example.com",
"avatar": "https://example.com/avatar.png",
"bio": "Developer and enthusiast.",
"metadata": {}
}saveProfile
更新当前已验证用户的个人资料。您可以更新 locale、metadata 等字段。
参数
- profileData
object(required) — 一个包含要更新的个人资料字段的对象。- locale
string— 用户的首选语言区域设置(例如 'en')。 - inviter
string— 邀请当前用户的用户的 DID。 - metadata
any— 一个用于存储自定义用户数据的对象。 - address
any— 用户的地址信息。
- locale
const updatedProfile = await sdk.auth.saveProfile({
metadata: { twitter: '@johndoe' },
locale: 'en-US'
});
console.log('个人资料已保存:', updatedProfile);返回
- updatedProfile
Promise<object>— 一个解析为更新后的个人资料对象的 promise。
getUserPublicInfo
获取任何用户的公开信息,通过其 DID 进行识别。
参数
- options
object(required)- did
string(required) — 用户的去中心化标识符(DID)。
- did
const publicInfo = await sdk.auth.getUserPublicInfo({ did: 'z8ia...' });
console.log(publicInfo.fullName);返回
- UserPublicInfo
Promise<object>— 一个解析为 UserPublicInfo 对象的 promise。- avatar
string— 用户头像的 URL。 - did
string— 用户的 DID。 - fullName
string— 用户的全名。 - sourceAppPid
string | null— 源应用程序的 PID。
- avatar
refreshProfile
强制从其原始来源同步用户的个人资料,确保数据是最新的。
await sdk.auth.refreshProfile();
console.log('已启动个人资料刷新。');返回
- void
Promise<void>— 一个在刷新请求已发送时解析的 promise。
updateDidSpace
更新用户的 DID Space 配置,该配置决定了他们的数据存储位置。
参数
- options
object(required)- spaceGateway
object(required) — 一个包含新 DID Space 网关详细信息的对象。- did
string(required) — 空间的 DID。 - name
string(required) — 空间的名称。 - url
string(required) — 空间的 URL。 - endpoint
string(required) — 空间的 API 端点。
- did
- spaceGateway
const spaceGateway = {
did: 'zNK...',
name: 'My Personal Space',
url: 'https://space.example.com',
endpoint: 'https://space.example.com/api'
};
await sdk.auth.updateDidSpace({ spaceGateway });
console.log('DID Space 更新成功。');返回
- void
Promise<void>— 一个在更新成功时解析的 promise。
getProfileUrl
构建用户公开个人资料页面的 URL。
参数
- options
object(required)- did
string(required) — 用户的 DID。 - locale
string(required) — 个人资料页面所需的区域设置。
- did
const url = await sdk.auth.getProfileUrl({ did: 'z8ia...', locale: 'en' });
console.log('个人资料 URL:', url);返回
- url
Promise<string>— 一个解析为完整个人资料 URL 字符串的 promise。
设置管理
管理用户的隐私和通知特定设置。
getUserPrivacyConfig
检索指定用户的隐私设置。
参数
- options
object(required)- did
string(required) — 正在请求其隐私设置的用户的 DID。
- did
const privacyConfig = await sdk.auth.getUserPrivacyConfig({ did: 'z8ia...' });
console.log('邮箱是否公开?', privacyConfig.isEmailPublic);返回
- PrivacyConfig
Promise<object>— 一个解析为用户 PrivacyConfig 对象的 promise,其中键是设置名称,值是布尔值。
saveUserPrivacyConfig
保存当前已验证用户的隐私设置。
参数
- config
object(required) — 一个包含表示隐私设置的键值对的对象。
const newConfig = { isEmailPublic: false, allowFriendRequests: false };
const savedConfig = await sdk.auth.saveUserPrivacyConfig(newConfig);
console.log('隐私配置已保存。');返回
- PrivacyConfig
Promise<object>— 一个解析为已保存的 PrivacyConfig 对象的 promise。
getUserNotificationConfig
获取当前用户的通知配置,包括 Webhook 和频道偏好。
const notificationConfig = await sdk.auth.getUserNotificationConfig();
console.log('Webhooks:', notificationConfig.webhooks);返回
- NotificationConfig
Promise<object>— 一个解析为 NotificationConfig 对象的 promise。- webhooks
array- webhook
object- type
'slack' | 'api'(required) - url
string(required)
- type
- webhook
- notifications
object- email
boolean - wallet
boolean - phone
boolean
- email
- webhooks
saveUserNotificationConfig
为当前用户保存新的通知设置。
参数
- config
object(required) — 新的通知配置对象。- webhooks
array- webhook
object- type
'slack' | 'api'(required) - url
string(required)
- type
- webhook
- notifications
object- email
boolean - wallet
boolean - phone
boolean
- email
- webhooks
const newConfig = {
webhooks: [
{ type: 'api', url: 'https://example.com/webhook' }
],
notifications: {
email: true,
wallet: false
}
};
const savedConfig = await sdk.auth.saveUserNotificationConfig(newConfig);
console.log('通知配置已保存:', savedConfig);返回
- NotificationConfig
Promise<object>— 一个解析为已保存的 NotificationConfig 对象的 promise。
testNotificationWebhook
测试 Webhook 配置以确保其可以接收测试通知。
参数
- webhook
object(required) — 要测试的 Webhook 对象。- type
'slack' | 'api'(required) - url
string(required)
- type
const webhookToTest = {
type: 'slack',
url: 'https://hooks.slack.com/services/...'
};
const result = await sdk.auth.testNotificationWebhook(webhookToTest);
console.log('Webhook 测试成功:', result.success);返回
- result
Promise<object>— 一个解析为具有 success 属性的对象的 promise。- success
boolean— 指示测试是否成功。
- success
社交互动
管理用户之间的社交联系。
followUser
关注另一个用户。
参数
- options
object(required)- userDid
string(required) — 要关注的用户的 DID。
- userDid
const userToFollow = 'z8ia...';
await sdk.auth.followUser({ userDid: userToFollow });
console.log(`成功关注 ${userToFollow}。`);返回
- void
Promise<void>— 一个在操作完成时解析的 promise。
unfollowUser
取消关注一个用户。
参数
- options
object(required)- userDid
string(required) — 要取消关注的用户的 DID。
- userDid
const userToUnfollow = 'z8ia...';
await sdk.auth.unfollowUser({ userDid: userToUnfollow });
console.log(`成功取消关注 ${userToUnfollow}。`);返回
- void
Promise<void>— 一个在操作完成时解析的 promise。
isFollowingUser
检查当前用户是否正在关注特定用户。
参数
- options
object(required)- userDid
string(required) — 要检查的用户的 DID。
- userDid
const userToCheck = 'z8ia...';
const { isFollowing } = await sdk.auth.isFollowingUser({ userDid: userToCheck });
if (isFollowing) {
console.log(`您正在关注 ${userToCheck}。`);
} else {
console.log(`您没有关注 ${userToCheck}。`);
}返回
- result
Promise<object>— 一个解析为包含 isFollowing 属性的对象的 promise。- isFollowing
boolean— 如果当前用户正在关注指定用户,则为 True。
- isFollowing
身份验证和账户操作
执行与用户会话和账户生命周期相关的关键操作。
logout
将当前用户注销。可以配置为从特定设备或所有会话中注销。
参数
- options
object- visitorId
string— 要注销的特定设备/会话的 ID。 - status
string— 要注销的会话的状态。 - includeFederated
boolean— 如果为 true,则也从联合登录组中的所有应用程序注销。
- visitorId
// 从当前会话简单注销
await sdk.auth.logout({});
console.log('注销成功。');
// 从特定设备和所有联合应用程序注销
await sdk.auth.logout({ visitorId: 'some-visitor-id', includeFederated: true });返回
- void
Promise<void>— 一个在注销过程完成时解析的 promise。
destroyMyself
永久删除当前已验证用户的账户。此操作不可逆,应极其谨慎使用。
// 这是一个破坏性操作。通常,您需要与用户确认此操作。
try {
const result = await sdk.auth.destroyMyself();
console.log(`账户 ${result.did} 已被永久删除。`);
} catch (error) {
console.error('删除账户失败:', error);
}返回
- result
Promise<object>— 一个解析为包含已删除用户 DID 的对象的 promise。- did
string— 已删除用户的 DID。
- did