partial.js logo
Latest version
v1.3.1 (RC2)

How does authorization work?


partial.js has the simple authorization mechanism.

Authorization


Authorization moduleExample

EXAMPLE FOR UNDERSTANDING


framework.onAuthorization = function(req, res, flags, next) {

	var userId = req.cookie('user').parseInt();

	if (userId === 0) {

		// unlogged user
		next(false);
		return;
	}

	var self = this;

	self.database('users').one(function(doc) { return doc.id === userId; }, function(user) {

		if (user === null) {

			// unlogged user
			next(false);
			return;
		}

		// user is logged
		req.user = user;
		next(true);

	});

}

TIP: REDIRECTING OF USERS


exports.install = function(framework) {
	framework.route('#401', error401);
};

// Unauthorized
function error401() {
	var self = this;

	if (self.flags.indexOf('logged') === -1) {
		self.redirect('/login/form/?r=' + encodeURIComponent(self.url));
		return;
	}

	self.redirect('/user/profile/');
}