You can use the function (user_login_finalize) in your custom code.
Example code to login which you can usein your custom code.
$uid is your user id for which you want to login
$user = User::load($uid); user_login_finalize($user);
You must be use following code in your namespace. If you don't use then user_login_finalize will not work in Drupal 9
© 2020.Zedangle. All Rights Reserved.