匠人学院 JR Academy学AI来匠人
匠人学院 JR Academy学AI来匠人

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png
EN

关于公司

关于我们元宇宙课堂新闻资讯匠人工作成为导师匠人导师联系我们匠人商店J3.Club

匠人资源

工作内推匠人活动1对1私教行业白皮书线上学习平台面试中心分享面试经验Internship会员中心

AI 工具

AI 工具箱考证匠 Cert Master求职匠 Job Hunter牛小匠 UniMate AI

AI 学习方向

全部学习方向AI EngineerContext EngineeringVibe CodingPrompt MasterAI BuilderAI 产品经理Python 入门

AI 应用提效

AI 办公提效AI 数据分析AI 财务AI 内容创作AI 视觉创作前端开发Hermes AgentOpenClaw 本地智能体

大学资源

墨尔本大学昆士兰大学新南威尔士大学悉尼大学莫那什大学阿德莱德大学RMITQUTUTS

少儿 AI 教育

Airbotix 少儿 AI 编程澳洲家长实用资料库NAPLAN 成绩单怎么看My School 学校数据指南悉尼私校学费 2026少儿编程课程与训练营

移民服务

澳洲移民技术移民189/190/491雇主担保482/186/494投资移民188/888英国移民美国移民加拿大移民

企业合作

P3职业孵化器Enterprise (EN)企业培训实习合作招聘合作申请合作

求职代理

岗位代投职位监控LinkedIn代运营LinkedIn人脉代加了解P3项目

匠人支持

FAQsTerms & ConditionsPrivacy PolicyCancellation & Refund PolicySite map

Top Categories

Web全栈班DevOps项目班数据工程全栈班数据分析项目班编程入门班Business Analyst实习算法集训营

求职就业

BA和产品经理实习数据科学实习数据分析实习Marketing实习简历修改面试指导导师指导VIP

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

联系方式

hello@jiangren.com.au0421-672-555

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2026 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572

首页/资源中心/文章详情
JR Academy · Blog职业洞察

Starting with Authentication (A tutorial with Node.js and MongoDB)

Starting with Authentication (A tutorial with Node.js and MongoDB) Authentication is an important issue when creating a dynamic web application. This article should clear things...

发布日期2018-06-11
阅读时长6 分钟
作者JiangRen Mr

快速导航

  • Authentication?
  • What I will use for this introduction
  • User registration
  • Sessions and Cookies
  • Creating custom middleware
  • A note on scalability with sessions
  • Wrap up

Authentication is an important issue when creating a dynamic web application. This article should clear things up and provide a basic instruction.

Authentication?

Authentication is for identifying users and provide different access rights and content depending on their id. In most cases the application provides a login form with certain credentials to verify a user.

It’s necessary to understand:

  • authentication as
  • authorization as
  • a session
  • cookies

What I will use for this introduction

Development Environment

In this example here I will use

  • Plain JavaScript
  • Node.js
  • Express (JS framework)
  • MongoDB (Database)
  • Yarn (package management)
  • Visual Studio Code as editor

for writing the authentication.

For the login mask I will use the awesome template from w3layouts.

Dependencies

Following packages are used

  • body-parser (for parsing incoming requests)
  • express (to make the application run)
  • nodemon (restarting server when changes occur)
  • mongoose (object data modeling to simplify interactions with MongoDB)
  • bcrypt (for hashing and salting passwords)
  • express session (to handle sessions)
  • connect-mongo (for storing sessions in MongoDB)

Structure

The tutorial will be structured in:

  • User registration (setting up routes and database)
  • Sessions and Cookies (connecting them to login routes)
  • Creating custom middleware (to improve the performance)

User registration

I’ll start with a basic express starter setup, which simply creates a webserver and serves the static files from the template on the home route. (see on Github commit)

Connect to MongoDB

  • install Mongoose
  • install mongodb
  • setup up mongod if you haven’t (tutorial)
  • be sure to start nodemon again with the running mongod on localhost!

Create a schema

MongoDB is a document database, which stores JSON like objects. The model/schema describes what this objects should contain.

  • create a schema according to the docs and store it in an own folder
  • the schema should describe the fields we have in our form and specify the data it can expect

It should look something like this:

var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
trim: true
},
username: {
type: String,
unique: true,
required: true,
trim: true
},
password: {
type: String,
required: true,
},
passwordConf: {
type: String,
required: true,
}
});
var User = mongoose.model('User', UserSchema);
module.exports = User;

Insert data into MongoDB

  • add body-parser for parsing incoming request bodies in a middleware
  • create a POST route for sending the data to the server
  • store the values of the filled out form and store the data in the db with the schema
  • it should look like this:
if (req.body.email &&
req.body.username &&
req.body.password &&
req.body.passwordConf) {
  var userData = {
email: req.body.email,
username: req.body.username,
password: req.body.password,
passwordConf: req.body.passwordConf,
}
  //use schema.create to insert data into the db
User.create(userData, function (err, user) {
if (err) {
return next(err)
} else {
return res.redirect('/profile');
}
});
}
  • use the mongo shell to see if your data has been saved to the database (it should show a document in db.users.find() )

Hashing and salting

Cryptographic hash functions take a piece of information and return a string, representing this information. Hash values cannot easily be “unhashed” or decrypted and that’s why they are a perfect fit for passwords.

Salt values are random data that is included with the input for the hash function.

In this tutorial we are using bcrypt.

So next:

  • install the bcrypt package
  • add a prehook to your mongoose schema. should look like this:
//hashing a password before saving it to the database
UserSchema.pre('save', function (next) {
var user = this;
bcrypt.hash(user.password, 10, function (err, hash){
if (err) {
return next(err);
}
user.password = hash;
next();
})
});
  • test with mongod if the new data is inserted with a hashed password (it should work)

Compare with my working commit if needed.


⭐ You have just reached 50% of the whole app and the hardest part is already finished! — Keep up! 🚀

Sessions and Cookies

HTTP is a stateless protocol, which means that web servers don’t keep track of who is visiting a page. Displaying specific content to logged-in users require this tracking. Therefore sessions with a session ID are created. Cookies are key/value pairs managed by browsers. Those correspond with the sessions of the server.

Set up Sessions

  • add the express session package
  • add the session middleware in your app. A simple one looks like that:
//use sessions for tracking logins
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false
}));
  • store the MongoDB userId (_id) in the req.session.userId
  • setup the login route the same way you set up the register route (in the login you only have the username and password)
  • authenticate the input against the data in the database in the user schema. It should look like this:
//authenticate input against database
UserSchema.statics.authenticate = function (email, password, callback) {
User.findOne({ email: email })
.exec(function (err, user) {
if (err) {
return callback(err)
} else if (!user) {
var err = new Error('User not found.');
err.status = 401;
return callback(err);
}
bcrypt.compare(password, user.password, function (err, result) {
if (result === true) {
return callback(null, user);
} else {
return callback();
}
})
});
}

❗ Take your time to understand this block of code, since it is the key function in the whole authentication process in my opinion!


⭐ Now at this point your actual authentication is working. Congratulation!

Compare with my working commit if needed.

 
https://unsplash.com/photos/qCrocisvGwc

Refining the app

  • make sure to adapt your layout accordingly to the sessions (hiding register fields and providing logout buttons)
  • create a middleware to make user IDs available in HTML
  • create a logout route that destroys the session id and redirects back to the home route. It can look like this:
// GET /logout
router.get('/logout', function(req, res, next) {
if (req.session) {
// delete session object
req.session.destroy(function(err) {
if(err) {
return next(err);
} else {
return res.redirect('/');
}
});
}
});

There is much more to add but logging out and destroying the session is important for each authentication system! That’s why I’ve included it here as well.

Creating custom middleware

Middleware runs after a request is received, but before a response is sent back. In this example the body-parser package is used as middleware. It converts incoming requests into a format that is easy to use for a JS program.

Middleware functions can be chained after each other and fit into the request/response cycle of the application. When writing custom middleware, next() always has to be called at the end of that middleware to move to the next one in the cycle.

Middleware can be used in many cases in this example, however, for simplicity reasons, I just reference an example to give an idea.

Example: Creating middleware that requires a login for certain pages.

function requiresLogin(req, res, next) {
if (req.session && req.session.userId) {
return next();
} else {
var err = new Error('You must be logged in to view this page.');
err.status = 401;
return next(err);
}
}
router.get('/profile', mid.requiresLogin, function(req, res, next) {
//...
});

Writing your own middleware gives you the freedom for ultimate flexibility when refining authentication routes.

A note on scalability with sessions

Currently sessions are stored in RAM. To store have more size we can connect the session store to MongoDB. I’ll use the connect-mongo package for that.

  • simply add the store like instructed on their docs. It should now look like this:
//use sessions for tracking logins
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
  • when checking with the mongo shell you should see how the new collection “sessions” is created. When logging in or out the data in that collection changes accordingly.

Wrap up

  • always make sure to transfer credentials in an encrypted way from the browser to the server and backwards
  • be sure to add a security certificate to your HTTP (HTTPS)
  • keep in mind that this way (with sessions and cookies) was only one way to authenticate, others are for example
  • token based authentication with OAuth or JSON Web Tokens
  • or with the passport middleware
 
Preview of the login/register mask
NodejsMongodb
作者JiangRen Mr
一键分享或复制链接
JiangRen Mr
Reviewer: JiangRen Mr

Founder of JR Academy

查看该作者的更多文章 →
← 上一篇澳洲UI设计大赛, APPLE WATCH和AIRPODS等你拿!下一篇 →六月IT招聘第二期|匠人招聘,招募Devops, Frontend等岗位

相关文章推荐

第一份工作就年薪80k,毫无工作经验的他是怎么做到的?

2019-11-21

在 Node.js 中将 SVG 图像转换为PNG,JPEG,TIFF,WEBP和HEIF格式

2019-09-05

Nodejs调试的各种姿势

2019-09-04

12月/December,高配导师手把手教你完整Web全栈(前端+后端)开发! 还有搭配实习可选!再也不怕毕业找不到工作了!

2018-11-27

2018,匠人课程安排 | 从学业到就业

2018-11-26

IT零基础到就业入职:12月,IT匠人成就你的IT梦

2018-11-25
查看全部文章 →
JR Academy
全球华人学习 AI 第一站
✓15000+ 学员
✓50+ 课程
✓AI 驱动学习平台
训练营免费资源AI学习职业辅导
精选推荐
AI 职业影响地图
测测你的职业风险等级,查看转型路径与学习方向
热门工具
AI & 数据训练营
系统化课程 + 真实项目实战,快速提升竞争力
热门课程
1v1 就业辅导
资深导师一对一指导,简历优化 + 面试准备
就业保障
企业内训定制
AI 技能培训方案,助力团队升级
企业服务
热门标签
Vibe CodingAI 编程CursorClaude求职攻略Prompt前端开发后端开发
订阅更新

获取最新 AI 学习资源、技术教程和求职攻略,直接送达邮箱。

我们尊重您的隐私,不会发送垃圾邮件