Skip to content

Commit 64d53ee

Browse files
committed
NODE-811 Added more explicit encodeURIComponent example and explanation
1 parent 393f6b0 commit 64d53ee

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

docs/reference/content/tutorials/connect/authenticating.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,24 @@ Include the name and password and the [authentication database] (https://docs.mo
2626

2727
In the following example, the connection string specifies the user `dave`, password `abc123`, authentication mechanism `DEFAULT`, and authentication database `myproject`.
2828

29+
{{% note class="important" %}}
30+
The user and password should always be **URI** encoded using `encodeURIComponent` to ensure any non URI compliant user or password characters are correctly parsed.
31+
{{% /note %}}
32+
2933
```js
3034
var MongoClient = require('mongodb').MongoClient,
3135
f = require('util').format,
3236
assert = require('assert');
3337

38+
var user = encodeURIComponent('dave');
39+
var password = encodeURIComponent('abc123');
40+
var authMechanism = 'DEFAULT';
41+
var authSource = 'myproject';
42+
3443
// Connection URL
35-
var url = 'mongodb://dave:abc123@localhost:27017?authMechanism=DEFAULT&authSource=myproject';
44+
var url = f('mongodb://%s:%s@localhost:27017?authMechanism=%s&authSource=',
45+
user, password, authMechanism, authSource);
46+
3647
// Use connect method to connect to the Server
3748
MongoClient.connect(url, function(err, db) {
3849
assert.equal(null, err);

0 commit comments

Comments
 (0)