Skip to content

Commit 2948b71

Browse files
committed
Add credentials to parameters
- Adding the credentials via dsn causes problems when the password contains an @ symbol
1 parent b2e8833 commit 2948b71

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/Jenssegers/Mongodb/Connection.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ protected function createConnection($dsn, array $config, array $options)
136136
$driverOptions = $config['driver_options'];
137137
}
138138

139+
$options['username'] = $config['username'];
140+
$options['password'] = $config['password'];
141+
139142
return new Client($dsn, $options, $driverOptions);
140143
}
141144

@@ -175,20 +178,7 @@ protected function getDsn(array $config)
175178
}
176179
}
177180

178-
// The database name needs to be in the connection string, otherwise it will
179-
// authenticate to the admin database, which may result in permission errors.
180-
$auth = '';
181-
if (! empty($username)) {
182-
$auth .= $username;
183-
}
184-
if (! empty($password)) {
185-
$auth .= ':' . urlencode($password);
186-
}
187-
if ($auth) {
188-
$auth .= '@';
189-
}
190-
191-
return "mongodb://" . $auth . implode(',', $hosts) . "/{$database}";
181+
return "mongodb://" . implode(',', $hosts) . "/{$database}";
192182
}
193183

194184
/**

0 commit comments

Comments
 (0)