Customer Logged in Programmatically using Magento 2.

You can do customer logged in Programmatically using Magento 2.

You required only Customer id to logged-in customer programmatically without going to the login page.

You need to first get Customer object using CustomerRepositoryInterface interface. Use Customer Object to customer session with setCustomerDataAsLoggedIn function.

Using the above way, You can do Programmatically customer login by customer id.

Pass Customer Id in above function to login Customer.

Check Customer Logout programmatically in Magento 2.