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 Starting

You should be familiar with previous tutorials. This tutorial assumes that you've integrated the Auth0.swift dependencies in your project and you're familiar with presenting the Login. For further information, check out the Login Guide and the User Sessions Guide first.

It is highly recommended that you take a look at the linking accounts documentation to understand the general process of linking accounts.

Enter Account Credentials

Here's the scenario: You have a user who is logged in and wants to link one (or multiple) accounts to that logged in account, such that the user can login with any of them and get into that account.

Typically, you will need to present an extra login dialog to make users enter the credentials for any account they want to link with their main account. You can present this login as we saw in the Login Tutorial:

First, import the Auth0 module:

import Auth0

Then present the hosted login screen:

// HomeViewController.swift

Auth0
    .webAuth()
    .audience("https://YOUR_AUTH0_DOMAIN/userinfo")
    .start {
        switch $0 {
        case .failure(let error):
            // Handle the error
            print("Error: \(error)")
        case .success(let credentials):
            guard let accessToken = credentials.accessToken, let idToken = credentials.idToken else { return }
            // Store accessToken to retrieve user profile, store idToken for linking
        }
}

Upon success, you need to store the idToken value for later use, which is the idToken for the secondary account that the user is linking with.

Linking an account is simple. You have a user, and another account you want to link with that user. All you need to grab is these three values:

  • id: The id from the user that is logged in. (See profile.sub)
  • idToken: The idToken obtained upon your user login.
  • otherUserToken: The idToken from the account you want to link the user with. This is the value you stored in step 1.
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 Linked Accounts

Linked accounts, a.k.a. the user's identities, can be retrieved by fetching the user's profile, a process that we already know from the User Sessions Tutorial:

// SessionManager.swift

Auth0
    .authentication()
    .userInfo(withAccessToken: accessToken)
    .start { result in
        switch(result) {
        case .success(let profile):
            // Store profile
        case .failure(let error):
            // Handle error
        }

Once you have the sub from the profile you can retrieve the users identities through a management API call as follows:

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

Any linked account is handled as an Identity instance. For further information on this object, check out the Identity class documentation

The unlinking process is quite similar to the linking one. This time, you just need the id, the user's idToken, and the identity object that you want to unlink (you will only use its userId and provider values):

// 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
Use Auth0 for FREECreate free Account