# Routes beschermen met AUTH

> Authenticeer elk verzoek met AUTH() en beperk routes tot ingelogde of anonieme gebruikers met de voorvoegsels + en -.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: nl
Content-SHA256: 093168a5fb669ae86b41fb46db5f1d123d7ab6dd77a312efb4557d1d1dc7bb01
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`AUTH()` draait bij elk verzoek. Roep `$.success(user)` aan als het verzoek is geauthenticeerd en `$.invalid()` als dat niet zo is. Het gebruikersobject is dan in elke handler beschikbaar als `$.user`.

Zet `+` voor een route om alleen geauthenticeerde gebruikers toe te staan en `-` om alleen anonieme toe te staan. Een route zonder voorvoegsel staat open voor iedereen.

```js definitions/auth.js
AUTH(function($) {

	// This demo reads a cookie. In a real app verify a signed session instead.
	var token = $.cookie('auth');

	if (token === 'secret-token')
		$.success({ id: '1', name: 'Peter' });
	else
		$.invalid();
});
```

```js controllers/private.js
exports.install = function() {
	ROUTE('+GET /private/', private_page);
	ROUTE('-GET /login/', login_page);
	ROUTE('GET /whoami/', whoami);
};

function private_page($) {
	$.json({ user: $.user.name });
}

function login_page($) {
	$.json({ message: 'Please sign in' });
}

function whoami($) {
	$.json({ user: $.user ? $.user.name : null });
}
```
