def signup_handler(remote, *args, **kwargs):
    if current_user.is_authenticated:
        return redirect('/')
    oauth_token = token_getter(remote)
    if not oauth_token:
        return redirect('/')
    session_prefix = token_session_key(remote.name)
    if not session.get(session_prefix + '_autoregister', False):
        return redirect(url_for('.login', remote_app=remote.name))
    form = create_registrationform(request.form)
    if form.validate_on_submit():
        account_info = session.get(session_prefix + '_account_info')
        response = session.get(session_prefix + '_response')
        user = oauth_register(form)
        if user is None:
            raise OAuthError('Could not create user.', remote)
        session.pop(session_prefix + '_autoregister', None)
        token = token_setter(remote, oauth_token[0], secret=oauth_token[1],
                             user=user)
        handlers = current_oauthclient.signup_handlers[remote.name]
        if token is None:
            raise OAuthError('Could not create token for user.', remote)
        if not token.remote_account.extra_data:
            account_setup = handlers['setup'](token, response)
            account_setup_received.send(
                remote, token=token, response=response,
                account_setup=account_setup
            )
            db.session.commit()
            account_setup_committed.send(remote, token=token)
        else:
            db.session.commit()
        if not oauth_authenticate(remote.consumer_key, user,
                                  require_existing_link=False):
            return redirect(url_for('security.login'))
        session.pop(session_prefix + '_account_info', None)
        session.pop(session_prefix + '_response', None)
        next_url = get_session_next_url(remote.name)
        if next_url:
            return redirect(next_url)
        else:
            return redirect('/')
    account_info = session.get(session_prefix + '_account_info')
    if not form.is_submitted():
        form = fill_form(form, account_info['user'])
    return render_template(
        current_app.config['OAUTHCLIENT_SIGNUP_TEMPLATE'],
        form=form,
        remote=remote,
        app_title=current_app.config['OAUTHCLIENT_REMOTE_APPS'][
            remote.name].get('title', ''),
        app_description=current_app.config['OAUTHCLIENT_REMOTE_APPS'][
            remote.name].get('description', ''),
        app_icon=current_app.config['OAUTHCLIENT_REMOTE_APPS'][
            remote.name].get('icon', None),
    )