[data-reveal]{opacity:1!important;transform:none!important}
AntelopeJS DMS
DocumentationExternal identities

External identities

Bind the accounts a user owns on external login providers, list them, and read the `UserExternalIdentity` table and model.

Overview

An external identity binds an account a user owns on a login provider, such as GitHub, Google, or any provider the deployment configures, to a User row. Signing in through that provider then resolves to the same account every time. The provider login flow reads the binding. The provider account id identifies it, not the email.

The two functions live in @antelopejs/interface-dms/auth. The table and its model live in @antelopejs/interface-dms/auth/db.

typescript
import {
  getExternalIdentities,
  linkExternalIdentity,
} from "@antelopejs/interface-dms/auth";
import {
  USER_EXTERNAL_IDENTITIES_TABLE_NAME,
  UserExternalIdentity,
  UserExternalIdentityModel,
  buildExternalIdentityId,
} from "@antelopejs/interface-dms/auth/db";

These rows serve authentication only. A provider connection behind a product feature, such as repository access or deployments, owns its own storage and must never be derived from these rows. They say who the user is, not what the deployment may do on the user's behalf.

Bind a provider account

typescript
linkExternalIdentity(
  userId: string,
  identity: ExternalIdentityInput,
): Promise<ExternalIdentityLinkResult>
typescript
interface ExternalIdentityInput {
  provider: string;
  providerAccountId: string;
  email: string;
}

interface ExternalIdentityLinkResult {
  wasCreated: boolean;
}
FieldDescription
providerProvider identifier, as declared in the OAuth configuration of the instance.
providerAccountIdImmutable account identifier on the provider side. Never the email, because users change their provider email without changing account.
emailEmail the provider reported, stored lowercased for support and audit only.

wasCreated is true for a first binding and false for a refresh. A login flow reads it to tell whether the user just connected a new provider or signed in again.

typescript
import { linkExternalIdentity } from "@antelopejs/interface-dms/auth";

const { wasCreated } = await linkExternalIdentity(user._id, {
  provider: "github",
  providerAccountId: profile.id,
  email: profile.email,
});

The call is idempotent for the owning user. Linking the same provider account again only refreshes its email and lastLoginAt. Binding an account that another user already owns throws 409 with the message error.oauth.identity_already_linked, because a provider account is a single-user credential.

The deterministic primary key of the row enforces uniqueness, not a secondary index. Two concurrent bindings of the same provider account therefore collide instead of creating a duplicate. The call that loses reads the row again, and either refreshes it or throws the 409.

List a user's identities

typescript
getExternalIdentities(userId: string): Promise<UserExternalIdentity[]>

It returns an empty array for a user who only signs in with a password. An account settings screen reads it to decide whether to offer disconnecting a provider.

The UserExternalIdentity table

Table user_external_identities in the DMS core schema. Like users, its rows are shared across tenants. USER_EXTERNAL_IDENTITIES_TABLE_NAME holds the name.

FieldTypeDescription
_idstring<provider>:<providerAccountId>. As the primary key, it makes the binding unique.
userIdstringOwning user. Indexed, related to User.
providerstringProvider identifier.
providerAccountIdstringAccount identifier on the provider side.
emailstringEmail reported at the last successful login. Account resolution never reads it back.
createdAtDateSet on insert. Indexed.
lastLoginAtDateRefreshed on every successful login through this identity.

buildExternalIdentityId(provider, providerAccountId) composes the id. Use it instead of formatting the string yourself, so a lookup by id keeps matching what linkExternalIdentity writes.

UserExternalIdentityModel

UserExternalIdentityModel extends BasicDataModel(UserExternalIdentity, USER_EXTERNAL_IDENTITIES_TABLE_NAME) and adds three methods:

MethodSignatureDescription
getByProviderAccount(provider: string, providerAccountId: string) => Promise<UserExternalIdentity | undefined>The identity a provider account is bound to, or undefined when the account is unknown.
getByUserId(userId: string) => Promise<UserExternalIdentity[]>Every identity bound to a user, read through the userId index.
deleteByUserId(userId: string) => Promise<void>Deletes every identity of a user.

getByProviderAccount is the lookup on the login side. Resolve the provider profile to an identity, then its userId to the account to sign in.

typescript
import { GetModel } from "@antelopejs/interface-database-decorators";
import {
  UserExternalIdentityModel,
  UserModel,
} from "@antelopejs/interface-dms/auth/db";

const identities = GetModel(UserExternalIdentityModel);
const identity = await identities.getByProviderAccount("github", profile.id);
const user = identity
  ? await GetModel(UserModel).get(identity.userId)
  : undefined;

An unknown provider account means no account exists for it yet. Hand the flow to signup or to invite redemption, and do not create an account implicitly. deleteByUserId belongs to account deletion. Identities left behind would let a recycled provider account resolve to a user row that no longer exists.