Skip to content

Commit

Permalink
Merge pull request #1 from swisnl/feature/basic-auth
Browse files Browse the repository at this point in the history
Add support for basic authentication
  • Loading branch information
JaZo authored Nov 17, 2023
2 parents 157abf7 + 86d1528 commit 118bd61
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [
'host' => env('ELASTICSEARCH_HOST', 'localhost:9200'),
'username' => env('ELASTICSEARCH_USERNAME'),
'password' => env('ELASTICSEARCH_PASSWORD'),
'index' => env('ELASTICSEARCH_INDEX', 'index'),
'enabled' => env('ELASTICSEARCH_ENABLED', false),
'models' => [
Expand Down
9 changes: 8 additions & 1 deletion src/LaravelElasticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public function configurePackage(Package $package): void
public function packageRegistered(): void
{
$this->app->singleton(Client::class, function () {
return ClientBuilder::create()->setHosts([config('elasticsearch.host')])->build();
$builder = ClientBuilder::create()
->setHosts([config('elasticsearch.host')]);

if (config('elasticsearch.username') && config('elasticsearch.password')) {
$builder->setBasicAuthentication(config('elasticsearch.username'), config('elasticsearch.password'));
}

return $builder->build();
});
}
}

0 comments on commit 118bd61

Please sign in to comment.