Assign role to user on first login, if they are first (after admin)

To set is the connected user belongs to the blog, you need to do that on hook wp_login that means the user is connected :

const ROLE_ADMINISTRATOR = "administrator";
const ROLE_CONTRIBUTOR = "contributor";


add_action("wp_login", function ($user_login, \WP_User $user) {


    if (    !is_multisite()
        ||  is_user_member_of_blog()
    ) {
        return;
    }


    $usersAdministrator = get_users([
        "role" => ROLE_ADMINISTRATOR,
    ]);


    if (count($usersAdministrator) > 0) {
        $newRole = ROLE_CONTRIBUTOR;
    } else {
        $newRole = ROLE_ADMINISTRATOR;
    }

    add_user_to_blog(
          get_current_blog_id()
        , $user->ID
        , $newRole
    );


}, 10, 2);