Added authentication

This commit is contained in:
Thomas Hii 2024-07-05 00:04:58 +08:00
parent 094fe740c0
commit c18e0dabb5
4 changed files with 17 additions and 5 deletions

View file

@ -1,5 +1,10 @@
# Change Log # Change Log
## 8.2.2
* Updated README
* Updated example with MongoDB authentication
## 8.2.1 ## 8.2.1
* Updated README * Updated README

View file

@ -66,10 +66,19 @@ See the tests for more usage examples.
## **Important Notes** ## **Important Notes**
When running with locally installed instance of MongoDB or docker based MongoDB, the following connection string is not supported by the underlying MongoDB driver yet. Best option at the moment is to run MongoDB with the authentication off or use MongoDB Atlas. When connecting to the locally installed instance of MongoDB or docker based MongoDB with authentication enabled, the following connection string is not supported by the MongoDB driver yet.
```dart ```dart
var db = Db('mongodb://<username>:<password>@localhost:27017/local'); var db = Db('mongodb://<username>:<password>@localhost:27017/local');
await db.open();
```
Use the following instead.
```dart
var db = Db('mongodb://localhost:27017/testDB');
await db.open();
await db.authenticate("<username>", "<password>", authDb: "admin");
``` ```
* `<username>` is MongoDB username * `<username>` is MongoDB username

View file

@ -6,9 +6,8 @@ import 'package:mongo_dart/mongo_dart.dart';
void main() async { void main() async {
var app = Angel(reflector: MirrorsReflector()); var app = Angel(reflector: MirrorsReflector());
var db = Db('mongodb://localhost:27017/testDB'); var db = Db('mongodb://localhost:27017/testDB');
//var db = Db('mongodb://root:Qwerty@localhost:27017/testDB');
await db.open(); await db.open();
//await db.authenticate("root", "Qwerty"); await db.authenticate("root", "Qwerty", authDb: "admin");
var service = app.use('/api/users', MongoService(db.collection('users'))); var service = app.use('/api/users', MongoService(db.collection('users')));

View file

@ -27,7 +27,6 @@ void main() {
late AngelHttp transport; late AngelHttp transport;
late http.Client client; late http.Client client;
var db = Db('mongodb://localhost:27017/testDB'); var db = Db('mongodb://localhost:27017/testDB');
//var db = Db('mongodb://root:Qwerty@localhost:27017/testDB');
late DbCollection testData; late DbCollection testData;
String? url; String? url;
@ -39,7 +38,7 @@ void main() {
transport = AngelHttp(app); transport = AngelHttp(app);
client = http.Client(); client = http.Client();
await db.open(); await db.open();
//await db.authenticate("root", "Qwerty"); await db.authenticate("root", "Qwerty", authDb: "admin");
testData = db.collection('testData'); testData = db.collection('testData');
// Delete anything before we start // Delete anything before we start