AuthService
The AuthService provides a comprehensive API for managing all aspects of a user's account. It handles user profiles, privacy and notification settings, social interactions like following other users, and critical authentication actions such as logging out and deleting an account. You can access it through the SDK instance at sdk.auth.
This service works closely with the TokenService to manage authentication states, but abstracts away the low-level token handling for most operations.
User Profile Management#
These methods allow you to fetch, update, and manage user profile data.
getProfile#
Retrieves the complete profile for the currently authenticated user.
const profile = await sdk.auth.getProfile();
console.log(profile);Returns
Example Response
{
"did": "z8ia29UsENBg6tLZUKi2HABj38Cw1LmHZocbQ",
"fullName": "John Doe",
"email": "john.doe@example.com",
"avatar": "https://example.com/avatar.png",
"bio": "Developer and enthusiast.",
"metadata": {}
}saveProfile#
Updates the profile of the currently authenticated user. You can update fields like locale, metadata, and more.
Parameters
const updatedProfile = await sdk.auth.saveProfile({
metadata: { twitter: '@johndoe' },
locale: 'en-US'
});
console.log('Profile saved:', updatedProfile);Returns
getUserPublicInfo#
Fetches publicly available information for any user, identified by their DID.
Parameters
const publicInfo = await sdk.auth.getUserPublicInfo({ did: 'z8ia...' });
console.log(publicInfo.fullName);Returns
refreshProfile#
Forces a synchronization of the user's profile from its original source, ensuring the data is up-to-date.
await sdk.auth.refreshProfile();
console.log('Profile refresh initiated.');Returns
updateDidSpace#
Updates the user's DID Space configuration, which determines where their data is stored.
Parameters
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 updated successfully.');Returns
getProfileUrl#
Constructs the URL for a user's public profile page.
Parameters
const url = await sdk.auth.getProfileUrl({ did: 'z8ia...', locale: 'en' });
console.log('Profile URL:', url);Returns
Settings Management#
Manage user-specific settings for privacy and notifications.
getUserPrivacyConfig#
Retrieves the privacy settings for a specified user.
Parameters
const privacyConfig = await sdk.auth.getUserPrivacyConfig({ did: 'z8ia...' });
console.log('Is email public?', privacyConfig.isEmailPublic);Returns
saveUserPrivacyConfig#
Saves the privacy settings for the currently authenticated user.
Parameters
const newConfig = { isEmailPublic: false, allowFriendRequests: false };
const savedConfig = await sdk.auth.saveUserPrivacyConfig(newConfig);
console.log('Privacy config saved.');Returns
getUserNotificationConfig#
Gets the notification configuration for the current user, including webhooks and channel preferences.
const notificationConfig = await sdk.auth.getUserNotificationConfig();
console.log('Webhooks:', notificationConfig.webhooks);Returns
saveUserNotificationConfig#
Saves new notification settings for the current user.
Parameters
const newConfig = {
webhooks: [
{ type: 'api', url: 'https://example.com/webhook' }
],
notifications: {
email: true,
wallet: false
}
};
const savedConfig = await sdk.auth.saveUserNotificationConfig(newConfig);
console.log('Notification config saved:', savedConfig);Returns
testNotificationWebhook#
Tests a webhook configuration to ensure it can receive test notifications.
Parameters
const webhookToTest = {
type: 'slack',
url: 'https://hooks.slack.com/services/...'
};
const result = await sdk.auth.testNotificationWebhook(webhookToTest);
console.log('Webhook test successful:', result.success);Returns
Social Interactions#
Manage social connections between users.
followUser#
Follow another user.
Parameters
const userToFollow = 'z8ia...';
await sdk.auth.followUser({ userDid: userToFollow });
console.log(`Successfully followed ${userToFollow}.`);Returns
unfollowUser#
Unfollow a user.
Parameters
const userToUnfollow = 'z8ia...';
await sdk.auth.unfollowUser({ userDid: userToUnfollow });
console.log(`Successfully unfollowed ${userToUnfollow}.`);Returns
isFollowingUser#
Check if the current user is following a specific user.
Parameters
const userToCheck = 'z8ia...';
const { isFollowing } = await sdk.auth.isFollowingUser({ userDid: userToCheck });
if (isFollowing) {
console.log(`You are following ${userToCheck}.`);
} else {
console.log(`You are not following ${userToCheck}.`);
}Returns
Authentication & Account Actions#
Perform critical actions related to the user's session and account lifecycle.
logout#
Logs the current user out. It can be configured to log out from a specific device or all sessions.
Parameters
// Simple logout from the current session
await sdk.auth.logout({});
console.log('Logged out successfully.');
// Log out from a specific device and all federated apps
await sdk.auth.logout({ visitorId: 'some-visitor-id', includeFederated: true });Returns
destroyMyself#
Permanently deletes the currently authenticated user's account. This action is irreversible and should be used with extreme caution.
// This is a destructive action. Usually, you would confirm this with the user.
try {
const result = await sdk.auth.destroyMyself();
console.log(`Account ${result.did} has been permanently deleted.`);
} catch (error) {
console.error('Failed to delete account:', error);
}Returns