Skip to main content

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.

javascript
const profile = await sdk.auth.getProfile();
console.log(profile);

Returns

  • profile Promise<object> — A promise that resolves to the user's profile object.

Example Response

json
{
  "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

  • profileData object (required) — An object containing the profile fields to update.
    • locale string — The user's preferred language locale (e.g., 'en').
    • inviter string — The DID of the user who invited the current user.
    • metadata any — An object for storing custom user data.
    • address any — The user's address information.
javascript
const updatedProfile = await sdk.auth.saveProfile({
  metadata: { twitter: '@johndoe' },
  locale: 'en-US'
});
console.log('Profile saved:', updatedProfile);

Returns

  • updatedProfile Promise<object> — A promise that resolves to the updated profile object.

getUserPublicInfo

Fetches publicly available information for any user, identified by their DID.

Parameters

  • options object (required)
    • did string (required) — The Decentralized Identifier (DID) of the user.
javascript
const publicInfo = await sdk.auth.getUserPublicInfo({ did: 'z8ia...' });
console.log(publicInfo.fullName);

Returns

  • UserPublicInfo Promise<object> — A promise that resolves to a UserPublicInfo object.
    • avatar string — URL of the user's avatar.
    • did string — The user's DID.
    • fullName string — The user's full name.
    • sourceAppPid string | null — The PID of the source application.

refreshProfile

Forces a synchronization of the user's profile from its original source, ensuring the data is up-to-date.

javascript
await sdk.auth.refreshProfile();
console.log('Profile refresh initiated.');

Returns

  • void Promise<void> — A promise that resolves when the refresh request has been sent.

updateDidSpace

Updates the user's DID Space configuration, which determines where their data is stored.

Parameters

  • options object (required)
    • spaceGateway object (required) — An object containing the new DID Space gateway details.
      • did string (required) — DID of the space.
      • name string (required) — Name of the space.
      • url string (required) — URL of the space.
      • endpoint string (required) — API endpoint of the space.
javascript
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

  • void Promise<void> — A promise that resolves upon successful update.

getProfileUrl

Constructs the URL for a user's public profile page.

Parameters

  • options object (required)
    • did string (required) — The user's DID.
    • locale string (required) — The desired locale for the profile page.
javascript
const url = await sdk.auth.getProfileUrl({ did: 'z8ia...', locale: 'en' });
console.log('Profile URL:', url);

Returns

  • url Promise<string> — A promise that resolves to the full profile URL string.

Settings Management

Manage user-specific settings for privacy and notifications.

getUserPrivacyConfig

Retrieves the privacy settings for a specified user.

Parameters

  • options object (required)
    • did string (required) — The DID of the user whose privacy settings are being requested.
javascript
const privacyConfig = await sdk.auth.getUserPrivacyConfig({ did: 'z8ia...' });
console.log('Is email public?', privacyConfig.isEmailPublic);

Returns

  • PrivacyConfig Promise<object> — A promise that resolves to the user's PrivacyConfig object, where keys are setting names and values are booleans.

saveUserPrivacyConfig

Saves the privacy settings for the currently authenticated user.

Parameters

  • config object (required) — An object with key-value pairs representing the privacy settings.
javascript
const newConfig = { isEmailPublic: false, allowFriendRequests: false };
const savedConfig = await sdk.auth.saveUserPrivacyConfig(newConfig);
console.log('Privacy config saved.');

Returns

  • PrivacyConfig Promise<object> — A promise that resolves to the saved PrivacyConfig object.

getUserNotificationConfig

Gets the notification configuration for the current user, including webhooks and channel preferences.

javascript
const notificationConfig = await sdk.auth.getUserNotificationConfig();
console.log('Webhooks:', notificationConfig.webhooks);

Returns

  • NotificationConfig Promise<object> — A promise that resolves to the NotificationConfig object.
    • webhooks array
      • webhook object
        • type 'slack' | 'api' (required)
        • url string (required)
    • notifications object
      • email boolean
      • wallet boolean
      • phone boolean

saveUserNotificationConfig

Saves new notification settings for the current user.

Parameters

  • config object (required) — The new notification configuration object.
    • webhooks array
      • webhook object
        • type 'slack' | 'api' (required)
        • url string (required)
    • notifications object
      • email boolean
      • wallet boolean
      • phone boolean
javascript
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

  • NotificationConfig Promise<object> — A promise that resolves to the saved NotificationConfig object.

testNotificationWebhook

Tests a webhook configuration to ensure it can receive test notifications.

Parameters

  • webhook object (required) — The webhook object to test.
    • type 'slack' | 'api' (required)
    • url string (required)
javascript
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

  • result Promise<object> — A promise that resolves to an object with a success property.
    • success boolean — Indicates if the test was successful.

Social Interactions

Manage social connections between users.

followUser

Follow another user.

Parameters

  • options object (required)
    • userDid string (required) — The DID of the user to follow.
javascript
const userToFollow = 'z8ia...';
await sdk.auth.followUser({ userDid: userToFollow });
console.log(`Successfully followed ${userToFollow}.`);

Returns

  • void Promise<void> — A promise that resolves when the operation is complete.

unfollowUser

Unfollow a user.

Parameters

  • options object (required)
    • userDid string (required) — The DID of the user to unfollow.
javascript
const userToUnfollow = 'z8ia...';
await sdk.auth.unfollowUser({ userDid: userToUnfollow });
console.log(`Successfully unfollowed ${userToUnfollow}.`);

Returns

  • void Promise<void> — A promise that resolves when the operation is complete.

isFollowingUser

Check if the current user is following a specific user.

Parameters

  • options object (required)
    • userDid string (required) — The DID of the user to check.
javascript
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

  • result Promise<object> — A promise that resolves to an object containing an isFollowing property.
    • isFollowing boolean — True if the current user is following the specified user.

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

  • options object
    • visitorId string — The ID of a specific device/session to log out.
    • status string — The status of the session to log out.
    • includeFederated boolean — If true, also logs out from all applications in the Federated Login Group.
javascript
// 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

  • void Promise<void> — A promise that resolves when the logout process is complete.

destroyMyself

Permanently deletes the currently authenticated user's account. This action is irreversible and should be used with extreme caution.

javascript
// 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

  • result Promise<object> — A promise that resolves to an object containing the DID of the deleted user.
    • did string — The DID of the deleted user.