Skip to content

Instantly share code, notes, and snippets.

@dbauszus-glx
Last active April 17, 2019 15:50
Show Gist options
  • Save dbauszus-glx/e007240929c593bb92423b96099ac3d2 to your computer and use it in GitHub Desktop.
Save dbauszus-glx/e007240929c593bb92423b96099ac3d2 to your computer and use it in GitHub Desktop.
// Set jsrender module for server-side templates.
const jsr = require('jsrender');
module.exports = {route, view};
function route(fastify) {
fastify.route({
method: 'GET',
url: '/',
preValidation: fastify.auth([
(req, res, next) => fastify.authToken(req, res, next, {
public: global.public,
login: true
})
]),
handler: view
});
fastify.route({
method: 'POST',
url: '/',
handler: (req, res) => fastify.login.post(req, res, {
view: view
})
});
};
function view(req, res, token = { access: 'public' }) {
// Build the template with jsrender and send to client.
res
.type('text/html')
.send(
jsr
.templates('./public/views/desktop.html')
.render({ token: token.signed || '""' })
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment