По сути, я получаю пользователя с именем пользователя @ тип компании, и после взрыва я ищу данные о подключении конкретной базы данных клиента.
После изменения базы данных во время выполнения я пытаюсь войти в систему, ноAuth :: попытка не работает.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\SysClient;
use App\User;
use Config;
use Hash;
use DB;
...
public function authenticate(Request $request)
{
$request->validate([
'username' => 'required|string',
'password' => 'required|string',
]);
$data = explode('@', $request->username);
$username = $data[0];
$alias = $data[1];
$password = $request->password;
//$password = Hash::make($request->password);
$client = SysClient::where('alias', $alias)->first();
$connection = [
"driver" => $client->driver,
"host" => $client->host,
"port" => $client->port,
"database" => $client->database,
"username" => $client->username,
"password" => $client->password,
"unix_socket" => $client->unix_socket,
"charset" => $client->charset,
"collation" => $client->collation,
"prefix" => $client->prefix,
"prefix_indexes" => $client->prefix_indexes,
"strict" => $client->strict,
"engine" => $client->engine
];
$request->session()->put('current_database', $connection); // save $connection in session
Config::set('database.connections.default', $connection); // set new connection
DB::purge('default'); // purge connection settings
DB::reconnect('default'); // reconnect database
//dd(DB::connection()->getDatabaseName()); //it works!
$credentials = [
'username' => $username, // string
'password' => $password, // string
'active' => 1
];
//dd(User::all()); //it works!
//So far everything works, the error happens when executing the next line
if (Auth::attempt($credentials)) {
dd('Authentication passed...');
} else {
dd('fail');
}
}
Я ожидал, что Auth :: попытается вернуть true или false, но, похоже, не хочет работатьс обновленным подключением к базе данных.