How to do authentication with Node.js, Express and Mongoose?

Authenticationnode.jsExpressMongoose

Authentication Problem Overview


I've made simple nodejs application by using nodejs+express. Now I want to make user authentification. I want to realize session handling by using mongoose. Can you advise some example?

Authentication Solutions


Solution 1 - Authentication

Some useful links:

https://stackoverflow.com/questions/7990890/how-to-implement-login-auth-in-node-js/8003291#8003291

https://stackoverflow.com/questions/8051631/creating-registration-and-login-form-in-node-js-and-mongodb/8111646#8111646

Also session management isn't done by Mongoose, but by connect-mongodb or connect-redis. You can checkout an example on how to do user auth and session management in a real application here:

https://github.com/alexyoung/nodepad/blob/master/app.js

Further explanations for that app you can find here: http://dailyjs.com/tag/lmawa or http://dailyjs.com/2010/12/06/node-tutorial-5/

Solution 2 - Authentication

Just use mongoose-auth by Brian Noguchi https://github.com/bnoguchi/mongoose-auth

It's a drop in solution for your question, it's well documented and extensible.

EDIT

mongoose-auth is no longer maintained. If you need to make it work with more recent versions of mongoose (ie > v3.x.x) and express (ie. > v3.x.x), here's an excerpt from a package.json file I'm currently using in production (It's hacky but it works):

"everyauth": "https://github.com/bnoguchi/everyauth/tarball/express3",
"mongoose-3x-types": "~1.0.5",
"mongoose-auth": "https://github.com/cbou/mongoose-auth/tarball/everymodule-fix",

I you are starting a new project don't use mongoose-auth, instead try out passport. It offers the same functionality, it's very flexible, however it has a different api. It's part of the locomotive MVC framework and as such it's actively maintained.

Solution 3 - Authentication

I've posted a complete example of a complete auth system using mongoose + expressjs on here, in case you want to take a look:

https://stackoverflow.com/questions/19024878/simple-login-page-in-nodejs-using-express-and-passport-with-mongodb/19025505#19025505

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErikView Question on Stackoverflow
Solution 1 - AuthenticationalessioalexView Answer on Stackoverflow
Solution 2 - Authenticationalexandru.topliceanuView Answer on Stackoverflow
Solution 3 - AuthenticationDavid OliverosView Answer on Stackoverflow