2018-06-11
JiangRen Mr
Authentication is an important issue when creating a dynamic web application. This article should clear things up and provide a basic instruction.
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:
In this example here I will use
for writing the authentication.
For the login mask I will use the awesome template from w3layouts.
Following packages are used
The tutorial will be structured in:
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)
MongoDB is a document database, which stores JSON like objects. The model/schema describes what this objects should contain.
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;
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');
}
});
}
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:
//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();
})
});
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! 🚀
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.
//use sessions for tracking logins
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false
}));
//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.
// 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.
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.
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.
//use sessions for tracking logins
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
Meta Town Virtual Office SaaS P3项目01期
2025/02/05 06:58 (Sydney)
Python零基础入门
2025/02/12 03:14 (Sydney)
Business Analyst产品经理+实习
2025/02/22 02:38 (Sydney)
地址
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 5000Disclaimer
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-2024 JR Academy Pty Ltd. All rights reserved.
ABN 26621887572