This should fit much better to your situation, my previous example was from my code in which I don’t need to get connection info from another database.
modify the index.php (the one that is in web for basic template or frontent/backend for advanced)
Comment the application run:
(new yii\web\Application($config))->run();
and add at the end of the file the following:
//(new yii\web\Application($config))->run();
// we only load the configuration so everything goes up
// but the application is not run yet so no db connection.
(new yii\web\Application($config));
// is there a user logged?
if (!Yii::$app->user->isGuest) :
//if yes we setup a different db connection
Yii::$app->db= \yii\db\Connection([
// dsn user and password are from session, set these value during login procedure
'dsn' => Yii::$app->session->get('custorem_connection.dns'),
'username' => Yii::$app->session->get('custorem_connection.username'),
'password' => Yii::$app->session->get('custorem_connection.password'),
]);
endif;
Yii::$app->run(); // this will run the application
In user model (and also on the model that get the connection info) use a secondary connection for the authentication, if not present add the following:
/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb() {
return Yii::$app->get('dbUser');
}
Where dbUser is the connection that holds your user database which also has the connection info for the application database.
To have a dbUser connection just configure it in main config in component section:
'dbUser' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=company_schema',
'username' => 'dbUser',
'password' => 'dbPwd',
'charset' => 'utf8',
]
I suggest you to use a secondary connection for login stuff so your application model will use always the default one "db" and just 2 or 3 model which handles user infos need to be configure with a different connection.
So in login action do something like the following (this is a stub, much depends if you have your custom code or use a module to handle auth)
public function actionLogin() {
.......
if (<authentication is valid>)) {
//code after authentication like user session start
Yii::$app->user->login($user);
// retrive connection information
$appConnection= <here follow the query to your model connection>;
//register connection info in session, these info are retrived before application run
Yii::$app->session->set('custorem_connection.dns', $appConnection->dns);
Yii::$app->session->set('custorem_connection.username', $appConnection->user);
Yii::$app->session->set('custorem_connection.password', $appConnection->password);
......
} else {
.........
Didn’t fully test it, to much to configure … database, tables and so on and I’m lazy 
Hope this help.