Docs Menu

Link User Identities - Swift SDKicons/link.png

On this page

  • Overview
  • Example
  • Async/Await Example

MongoDB Realm provides multiple ways for users to log into your application. Because each method of authentication corresponds to a unique user identity, Realm lets you merge multiple identities corresponding to a single user into one user identity.

For instance, consider an application that enables users to authenticate via anonymous authentication so they may explore functionality without linking to an existing SSO provider or generating a new password. When that user finally decides to create a full account with an SSO provider or email/password authentication, you need some way of persisting the user's original anonymous identity with their new permanent identity.

You can link identities using the linkUser(credentials:Credentials) method of the User object of a logged in User.

New in version 10.16.0.

If you're using Swift 5.5 with an iOS target of 15.0 or higher, you can use the async/await version of User.linkUser.

let app = App(id: YOUR_REALM_APP_ID)
func logInAnonymously() async throws -> User {
let anonymousUser = try await app.login(credentials: Credentials.anonymous)
// User uses app, then later registers an account
let newAccountLinkedUser = try await registerNewAccount(anonymousUser: anonymousUser)
return newAccountLinkedUser
func registerNewAccount(anonymousUser: User) async throws -> User {
let email = ""
let password = "ganondorf"
try await app.emailPasswordAuth.registerUser(email: email, password: password)
// Successfully created account, now link it
// with the existing anon user
let linkedUser = try await link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password))
return linkedUser
func link(user: User, with credentials: Credentials) async throws -> User {
try await user.linkUser(credentials: credentials)
do {
let linkedUser = try await logInAnonymously()
print("Successfully linked user async: \(linkedUser)")
} catch {
print("Failed to link user: \(error.localizedDescription)")
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.