I was trying to understand how to use the passport.js in Mongodb native driver,Express now the problem is that all the reference or tutorials are showing the LOCAL-STRATEGY by using mongoose which creates a schema or model........so now iam STUCK
-
1I think you may be mis-understanding what a `local-strategy` is. All `local-strategy` means you are not using an authentication provider (Google, FaceBook) etc...but using a *custom* `username & password` to authenticate with. You will still need some form of a database for this. – Intellidroid Jul 26 '19 at 14:32
-
2ya i got what you said.yes iam not using a auth provider.I am having mongoDB with native driver so the question is how to use the local-strategy while using the mongoDB native driver..NOT MONGOOSE !! and WITHOUT A SCHEMA OR MODEL !! – Varun M Jul 27 '19 at 09:45
-
May I ask why you don't want to use either models or mongoose? What are you trying to achieve by not using them? In any event I have posted an answer which is probably what you are looking for? – Intellidroid Jul 27 '19 at 19:11
3 Answers
Sorry for being here for a little bit late, but maybe my answer would be helpful to others who seeking answer for this kind of question.
I assume that you were struggling with these problems:
- How to reuse database connection among your project
You can define your MongoClient object once and reuse this across multiple modules in your project as follow:
dbUtil.js
hold definition of MongoClient object:
const MongoClient = require('mongodb').MongoClient;
const SERVER_URI = // link to your server, for example: 'http://localhost:27017';
const DB_NAME = // database name, for example: 'test';
/* @WHY?:
(option 1?) : to let the server assign objectId instead of Node driver,
(option 2 & 3?) : to get rid of deprecation warnings
*/
const clientObj = new MongoClient(`${SERVER_URI}/${DB_NAME}`, {
forceServerObjectId: true,
useNewUrlParser: true,
useUnifiedTopology: true
});
module.exports = {
client: clientObj,
dbName: DB_NAME
}
In another module where you need to use the defined connection:
const { client, dbName } = require('dbUtil');
// Because client.connect() return a Promise, you should wrap everything
// inside an immediately-invoked expression like this
(async () => {
await client.connect(); // at first you need to open the connection client
const dbO = await client.db(dbName); // get the connection to database
/* perform database operations, for example:
dbO.collection(users).insertOne({ name:'mongoadmin' });
*/
client.close(); // remember to close the connection when you're done
})();
So instead of the Mongoose way of using User.find().exec()
, in Mongo native driver you have to activate connection to Client first and then use client.dbO.collection('users')
(which return a Promise).
- What the heck is Passport and why it's needed for your project
Passport is authentication middleware for Express that support authentication from Facebook, Google, JWT,... and many other authentication strategies. It can be helpful when you need to you want to support authentication from multiple authentication portal. However, it's not a must-have.
Sometimes applying another layer of abstraction from third-party libraries not only bring no sense to you & your project, but also over-complicate your existed code base. You'd chose not to use Mongoose and adapted MongoDb native driver instead, stated that you didn't need schema
& model
stuffs. For the same logic, I don't see any necessity of adapting Passport. This link can be helpful to you in some way: another Stackoverflow post
To apply authentication using JSON web token to your Express routes, you need to do these following steps:
- Generate token for user signed in
- Verify token
- Define protected routes and write middlewares for those
All these tasks can be done without any third-party modules/libraries!

- 58
- 5
I believe your question stems from using mongodb schema validation instead of mongoose schema. You can use another means of authentication like JWT which does not directly need models for its authentication.

- 11
- 4