# Controller callbacks, manual authorization and conditional responses

> Execute jsonstring, respond, done, successful, authorize, custom and Last-Modified handling.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 787a7a2bd01ce5f203e2d87e26c6bd63f6e6db685bd180c8648853021e8f2bd0
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

These are ordinary route-controller methods, distinct from the action context. `$.jsonstring()` takes already-serialized JSON. `$.respond()` selects the method named by `$.response.output`, defaulting to JSON. `$.done(true)` forwards the callback result inside the success envelope; another argument selects a fixed value. `$.successful(fn)` calls your function only when there is no error.

`await $.authorize()` resolves with the authorization result; an anonymous result must still be handled by your code. For conditional responses pass a **UTC header string** to `$.notmodified()`: in 0.0.19 comparison is exact string equality. `$.httpcache()` sets headers but `$.json()` overwrites cache policy, so the example sends a raw buffer after setting them. Use public caching only for public representations. `$.custom()` transfers response ownership to your handler; finish the Node response yourself.

```js definitions/auth.js
AUTH(function($) {
	if ($.headers['x-demo-user'] === 'alice')
		$.success({ id: 'alice' });
	else
		$.invalid();
});
```

```js controllers/completion.js
exports.install = function() {
	ROUTE('GET /serialized/', $ => $.jsonstring('{"ok":true}'));
	ROUTE('GET /respond/', function($) { $.response.output = 'plain'; $.respond('hello'); });
	ROUTE('GET /done/', $ => $.done(true)(null, { id: 7 }));
	ROUTE('GET /successful/', $ => $.successful(value => $.json({ value: value }))(null, 9));
	ROUTE('GET /callback-error/', $ => $.successful(() => $.json({ wrong: true }))('failure'));
	ROUTE('GET /manual-auth/', async function($) {
		var user = await $.authorize();
		if (user)
			$.json({ id: $.user.id });
		else
			$.invalid(401);
	});
	ROUTE('GET /conditional/', function($) {
		var modified = 'Mon, 05 Jan 2026 00:00:00 GMT';
		if ($.notmodified(modified))
			return;
		$.httpcache(modified);
		$.binary(Buffer.from('public version 1'), 'text/plain');
	});
	ROUTE('GET /custom/', function($) {
		$.custom();
		$.res.writeHead(202, { 'content-type': 'text/plain' });
		$.res.end('accepted');
	});
};
```
