iOS-Swift Linking Accounts

Sample Project

Download this sample project configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 1.1.1
  • Version 8.2 (8C38)
  • iPhone 6 - iOS 10.2 (14C89)
Show requirements

Before Starting

You should be familiar with previous tutorials. This tutorial assumes that:

It is highly recommended that you take a look at the linking accounts documentation to understand the 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:

import Lock
Lock
    .classic()
    .onAuth { credentials in
        guard let accessToken = credentials.accessToken, let idToken = credentials.idToken else { return }
        // Store accessToken to retrieve user profile, store idToken for linking
    }
    .present(from: self)

Upon success, you need to store the dToken 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.
  • 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
let id = ... // the id of the user
let idToken = ... // the user idToken
let otherUserToken = ... // the idToken from the account you want to link the user with
Auth0
    .users(token: idToken)
    .link(id, withOtherUserToken: otherUserToken)
    .start { result in
        switch result {
        case .success:
            // linked account!
        case .failure(let error):
            // deal with error
        }
    }

Retrieve Linked Accounts

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

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

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

Auth0
    .users(token: idToken)
    .get(userId, 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 a Profile identity object. For further information on this object, check out the profile 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):

import Auth0
let id = ... // the user id
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
            }
     }
Use Auth0 for FREECreate free Account