iOS Objective-C: Linking Accounts

Gravatar for martin.walsh@auth0.com
By Martin Walsh
Auth0

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 as 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 the Swift wrapper and Auth0 library:

#import "Auth0Sample-Swift.h"
@import Auth0;

To link accounts:

// ProfileViewController.m

NSString *id = ... // the id of the user, available in profile.sub
NSString *idToken = ... // the user's idToken
NSString *otherUserToken = ... // the idToken from the account you want to link the user with
[auth linkUserAccountWithIdToken:idToken userId:id otherAccountToken:otherUserToken
      callback:^(NSError * _Nullable error, NSArray<NSDictionary<NSString *,id> *> * _Nullable payload) {
          if (error) {
              // Handler Error
          } else {
              // Success account was linked
          }
}];

Retrieve the Linked Accounts

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

// HomeViewController.m

HybridAuth *auth = [[HybridAuth alloc] init];
[auth userProfileWithIdToken:idToken userId:id callback:^(NSError * _Nullable error, NSDictionary<NSString *, id> * _Nullable user) {
    if (error) {
        // Handle error
    } else {
        NSArray *identities = [[NSArray alloc] init];
        for (NSDictionary *identity in [user objectForKey:@"identities"]) {
           identities = [identities arrayByAddingObject:[[A0Identity alloc] initWithJson:identity]];
        }
    }
}];

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:

// ProfileViewController.m
NSString *id = ... // the id of the user, available in profile.sub
NSString *idToken = ... // the user idToken
A0Identity *identity = ... // the identity (account) you want to unlink from the user
[auth unlinkUserAccountWithIdToken:idToken userId:id identity:identity callback:^(NSError * _Nullable error, NSArray<NSDictionary<NSString *,id> *> * _Nullable payload) {
    if (error) {
        // Handle Error
    } else {
        // Success, account unlinked.
    }
}];
Use Auth0 for FREECreate free Account