Q:

Magento 2: Login user programmatically showing wrong default header message "Default welcome msg!" instead of showing "Welcome, user!"

When you login to Magento 2.2.6 programmatically with code below, header show default message "Default welcome msg!" instead of "Welcome, user!"
// Load customer session
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerAsLoggedIn($customer);

Expected result

Session should get started
Header should show "Welcome, user!"

Actual result

Session getting started
Header shows "Default welcome msg!"

Unlike when you login with magento login page, it is not picking customer fullname during login with code.

A:

The welcome message on header using customer data scope.
https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Theme/view/frontend/templates/html/header.phtml
You'd need to trigger invalidate for customerData after your action
customerData.invalidate(['customer']);
customerData.reload(['customer'], true);

For example

Event trigger
$(document).on('ajaxComplete', function (event, xhr, settings) {
    var sections,
        redirects;

    if (settings.type.match(/post|put/i)) {
        sections = sectionConfig.getAffectedSections(settings.url);

        if (sections) {
            customerData.invalidate(sections);
            redirects = ['redirect', 'backUrl'];

            if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) {
                return;
            }
            customerData.reload(sections, true);
        }
    }
});

Use section sections.xml file to etc/frontend for controller/action
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
<action name="braintree/paypal/placeOrder">
    <section name="cart"/>
    <section name="checkout-data"/>
</action>
</config>

