Programmatically Login by using wp_set_auth_cookie

I have user id and need to login that user in WordPress but it will not Work

$user_id= intval($_POST[‘user_id’]);
$user = get_user_by(‘id’, $user_id);

    if ( ! is_wp_error( $user ) ) {

        wp_clear_auth_cookie();
        wp_set_auth_cookie( $user_id, false, is_ssl() );
        wp_set_current_user( $user_id );
        do_action('wp_login', $user->user_login, $user);
        ob_end_flush();
        wp_safe_redirect(home_url('/my-orders'));    
        exit();
    }

Please review the above code and let me know what is wrong i did that is not work with bedrock, Please help me why this code not works and possible solution in bedrock.