<commit_before>@{meta('How does authorization work?')}

<h1>How does authorization work?</h1>
<br />
<div class="message message-alert">
	total.js has the simple authorization mechanism.
</div>
<br />
<div>@{image('authorization.png', 810, 300, 'Authorization')}</div>
<hr />
<br />
<div class="text">
	<ul>
		<li>You can create your own authorization mechanism (<b>total.js gives open options</b>).</li>
		<li>Authorization delegate is async.</li>
		<li>You can authorize user by the cookies / headers or request data.</li>
		<li>In this delegate you can set user roles.</li>
		<li class="red b">Authorization delegate adds [authorize] flag.</li>
		<li><b>IMPORTANT:</b> if you add [authorize] flag then <b>each request</b> (to controller action) goes through an onAuthorize delegate.</li>
	</ul>
</div>
<div class="documentation-example">
	<a href="https://github.com/totaljs/modules/tree/master/authorization">Authorization module</a>
	<a href="https://github.com/totaljs/examples/tree/master/authorization">Example</a>
</div>
<h2>EXAMPLE FOR FURTHER UNDERSTANDING</h2>
<br />
<pre class="brush: jscript">framework.onAuthorization = function(req, res, flags, next) {

	// EXAMPLE:
	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;
		}

		// Documentation: http://docs.totaljs.com/Framework/#framework.onAuthorization
		// user is logged
		next(true, user);

	});

}
</pre>
<br />
<h2>ROUTING + TIP: RE-DIRECTING USERS</h2>
<br />
<pre class="brush: jscript">exports.install = function(framework) {
	framework.route('/', view_homepage_unlogged);
	framework.route('/', view_homepage_logged, ['authorize']);
	framework.route('#401', error401);
};

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

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

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

function view_homepage_unlogged() {
	var self = this;
	self.plain('UNLOGGED');
}

function view_homepage_logged() {
	var self = this;
	self.plain('LOGGED: ' + JSON.stringify(self.user));
}
</pre><commit_msg>change: minor<commit_after>@{meta('How does authorization work?')}

<h1>How does authorization work?</h1>
<br />
<div class="message message-alert">
	total.js has the simple authorization mechanism.
</div>
<br />
<div>@{image('authorization.png', 810, 300, 'Authorization')}</div>
<hr />
<br />
<div class="text">
	<ul>
		<li>You can create your own authorization mechanism (<b>total.js gives open options</b>).</li>
		<li>Authorization delegate is async.</li>
		<li>You can authorize user by the cookies / headers or request data.</li>
		<li>In this delegate you can set user roles.</li>
		<li class="red b">Authorization delegate adds [authorize] flag.</li>
		<li><b>IMPORTANT:</b> if you add [authorize] flag then <b>each request</b> (to controller action) goes through an onAuthorize delegate.</li>
	</ul>
</div>
<div class="documentation-example">
	<a href="https://github.com/totaljs/modules/tree/master/authorization">Authorization module</a>
	<a href="https://github.com/totaljs/examples/tree/master/authorization">Example</a>
</div>
<h2>EXAMPLE FOR FURTHER UNDERSTANDING</h2>
<br />
<pre class="brush: jscript">framework.onAuthorization = function(req, res, flags, next) {

	// EXAMPLE:
	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;
		}

		// Documentation: http://docs.totaljs.com/Framework/#framework.onAuthorization
		// user is logged
		next(true, user);

	});

}
</pre>
<br />
<h2>ROUTING + TIP: REDIRECTING USERS</h2>
<br />
<pre class="brush: jscript">exports.install = function(framework) {
	framework.route('/', view_homepage_unlogged);
	framework.route('/', view_homepage_logged, ['authorize']);
	framework.route('#401', error401);
};

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

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

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

function view_homepage_unlogged() {
	var self = this;
	self.plain('UNLOGGED');
}

function view_homepage_logged() {
	var self = this;
	self.plain('LOGGED: ' + JSON.stringify(self.user));
}
</pre>
{'fraction_non_alphanumeric': 0.1821313240043057, 'fraction_numerical': 0.009903121636167923, 'mean_word_length': 4.537544696066746, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5985757', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1597, 'n_words': 374}