Jump to content

Developer Documentation

Merging accounts in a login handler

With some login handlers, particularly those which are OAuth-based, you may need to merge accounts. For example, imagine a user is registered on your community, and then they try to log in using Facebook. In this situation, you don't want to create a new account, but rather prompt the user to link their Facebook account with their existing account. In this case, throw an exception in your authenticate() method:

$exception = new \IPS\Login\Exception( 'generic_error', \IPS\Login\Exception::MERGE_SOCIAL_ACCOUNT );
$exception->handler = 'Example';
$exception->member = $existingAccount;
$exception->details = $token;
throw $exception;
  • $handler
    The key for your login handler
  • $member
    The existing account (as an \IPS\Member object)
  • $details
    Any details you need to link the accounts together, such as the access token.

Then implement a link() method:

/**
 * Link Account
 *
 * @param	\IPS\Member	$member		The member
 * @param	mixed		$details	Details as they were passed to the exception thrown in authenticate()
 * @return	void
 */
public static function link( \IPS\Member $member, $details )
{
    $userData = \IPS\Http\Url::external( "https://www.example.com/userData" )->setQueryString( 'token', $details )->request()->get()->decodeJson();
    $member->my_custom_id = $userData['id'];
    $member->save();
}

Your link() method is called after the user has provided their password and it is safe to link the accounts together. Do whatever is necessary so that on subsequent logins, you can log the user in without intervention. Note that link() is static and cannot use any settings from the login handler.


  Report Document


×
×
  • Create New...