I want change in run time the database connection after user login. I use the method set of Config Facade. I know that I can use it only on Middleware or Controller constructor. So I created these
Route::get("login", "Login_Controller@login");
Route::group(["middleware" => "test"], function() {
Route::post("login", "Login_Controller@login");
});
And then I created the "Test" Middleware called after login submit:
public function handle($request, Closure $next) {
// Validazione dei dati
$validator = Validator::make($request->all(), [
"codice_azienda" => "required",
"username" => "required",
"password" => "required"
]);
if ($validator->fails()) {
return redirect()->back()->withInput()->withErrors($validator);
}
// Verifico i dati immessi
$codice_azienda = $request->get("codice_azienda");
$username = $request->get("username");
$password = $request->get("password");
$objOperatore = new Operatore();
$cliente = $objOperatore->loginOperatore($codice_azienda, $username, $password);
if (empty($cliente)) {
throw new \App\Exceptions\LoginFailedException;
}
Config::set("DB_HOST", Crypt::decrypt($cliente->Server));
Config::set("DB_DATABASE", $cliente->NomeDB);
Config::set("DB_USERNAME", Crypt::decrypt($cliente->Username));
Config::set("DB_PASSWORD", Crypt::decrypt($cliente->Password));
$operatori = Operatore_Model::all();
\App\Http\Controllers\Log_Controller::debug($operatori, true);
return $next($request);
}
But the metoed all() of Operatore_Model doesn't return anything
Logs return this error:
"[2017-02-16 16:44:52] local.ERROR: exception 'PDOException' with message 'SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'Operatori'.' in C:\xampp\htdocs\dashboard\www\e730\vendor\laravel\framework\src\Illuminate\Database\Connection.php:323"
I want use a multi DB connection, one for each customer.
auth.login. In that case, you wouldn't have to worry about throwing aLoginFailedExceptionor validationg usernames and password.