How to get custom post_author?

Looking at your if-else block, I see there’s a $post_id variable, so if it’s the ID of the post that you want to assign to the user you’re creating, then you could just call wp_update_post() in your else block, i.e. right after you create the user.

So just replace the wp_insert_user($userdata); in your code with this:

$user_id = wp_insert_user($userdata);

if ( ! is_wp_error( $user_id ) ) {
    wp_update_post( array(
        'ID'          => $post_id,
        'post_author' => $user_id,
    ) );
}