Handle unlinking of remote account.

    :param remote: The remote application.
    :returns: The HTML response.

def disconnect_handler(remote, *args, **kwargs):
    """Handle unlinking of remote account.

    :param remote: The remote application.
    :returns: The HTML response.
    """
    if not current_user.is_authenticated:
        return current_app.login_manager.unauthorized()

    remote_account = RemoteAccount.get(user_id=current_user.get_id(),
                                       client_id=remote.consumer_key)
    external_ids = [i.id for i in current_user.external_identifiers
                    if i.method == GLOBUS_EXTERNAL_METHOD]

    if external_ids:
        oauth_unlink_external_id(dict(id=external_ids[0],
                                      method=GLOBUS_EXTERNAL_METHOD))

    if remote_account:
        with db.session.begin_nested():
            remote_account.delete()

    return redirect(url_for('invenio_oauthclient_settings.index'))