iOS Swift Linking Accounts

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 1.2.1
  • Version 8.3.2 (8E2002)
  • iPhone 7 - iOS 10.3 (14E269)
Show requirements

Before You Start

Before you continue with this tutorial, make sure that you have completed the previous tutorials. This tutorial assumes that:

  • You have integrated Auth0.swift as a dependency in your project.
  • You are familiar with presenting the login screen. To learn more, see the Login and the User Sessions tutorials.

We recommend that you read the Linking Accounts documentation to understand the process of linking accounts.

Enter Account Credentials

Your users may want to link their other accounts to the account they are logged in to.

To achieve this, present an additional login dialog where your users can enter the credentials for any additional account. You can present this dialog in the way described in the Login tutorial.

After the user authenticates, save the idToken value for the secondary account.

Now, you can link the accounts. To do this, you need the following values:

  • id: the logged-in user's ID (see profile.sub)
  • idToken: the ID Token for the saved account the user initially logged in to
  • otherUserToken: the ID Token for the second account received in the last login response

import Auth0
// UserIdentitiesViewController.swift

Auth0
    .users(token: idToken)
    .link(id, withOtherUserToken: otherUserToken)
    .start { result in
        switch result {
        case .success:
            // The account was linked
        case .failure(let error):
            // Handler Error
        }
    }

Retrieve the Linked Accounts

Once you have the sub value from the profile, you can retrieve user identities. Call the management API:

// SessionManager.swift

Auth0
    .users(token: idToken)
    .get(profile.sub, fields: ["identities"], include: true)
    .start { result in
        switch result {
        case .success(let user):
            let identityValues = user["identities"] as? [[String: Any]] ?? []
            let identities = identityValues.flatMap { Identity(json: $0) }
        case .failure(let error):
            // Handle error
        }
  }

A linked account is handled as an Identity instance. Read more about this object in the Identity class documentation.

To unlink the accounts, you need to specify the following:

  • id: the logged-in user's ID (see profile.sub)
  • userId and provider: the values in the identity object you want to unlink

Unlink the accounts:

// UserIdentitiesViewController.swift

let id = ... // the user id. (See profile.sub)
let idToken = ... // the user idToken
let identity: Identity = ... // the identity (account) you want to unlink from the user
Auth0
    .users(token: idToken)
    .unlink(identityId: identity.identifier, provider: identity.provider, fromUserId: id)
    .start { result in
            switch result {
            case .success:
                // Unlinked account!
            case .failure(let error):
                // Deal with error
            }
     }
Previous Tutorial
5. Authorization
Was this article helpful?
Use Auth0 for FREECreate free Account