2019-09-07 07:15:43 +02:00
|
|
|
async function routes(fastify, options) {
|
2019-09-17 23:10:46 +02:00
|
|
|
fastify.get('/api/test-db-connect', async (request, reply) => {
|
|
|
|
fastify.pg.connect((err, client, release) => {
|
|
|
|
if (err) return reply.send(err);
|
|
|
|
|
|
|
|
client.query(
|
|
|
|
'SELECT * FROM test', [], (err, result) => {
|
|
|
|
release();
|
|
|
|
return reply.send(err || result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
/*fastify.get('/login', async (request, reply) => {
|
2019-09-07 07:15:43 +02:00
|
|
|
reply.view('login.hbs', { text: request.isLoggedInUser ? JSON.stringify(fastify.jwt.decode(request.cookies.token)) : 'you are NOT logged in' });
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.post('/login-validate', async (request, reply) => {
|
|
|
|
if (typeof request.body.email === "undefined" || typeof request.body.password === "undefined") {
|
|
|
|
reply.redirect('/login', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = fastify.jwt.sign({ email: request.body.email, password: request.body.password });
|
|
|
|
const expireTime = fastify.siteConfig.tokenExpireDays * (24 * 60 * 60e3); // The section in parentheses is milliseconds in a day
|
|
|
|
reply
|
|
|
|
.setCookie('token', token, {
|
|
|
|
path: '/',
|
|
|
|
expires: new Date(Date.now() + expireTime),
|
|
|
|
maxAge: new Date(Date.now() + expireTime), // Both are set as a "just in case"
|
|
|
|
httpOnly: true, // Prevents JavaScript on the front end from grabbing it
|
|
|
|
sameSite: true, // Prevents the cookie from being used outside of this site
|
|
|
|
})
|
|
|
|
.redirect('/', 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.get('/logout', async (request, reply) => {
|
|
|
|
reply.clearCookie('token', { path: '/' }).redirect('/?loggedout');
|
2019-09-17 23:10:46 +02:00
|
|
|
});*/
|
2019-09-07 07:15:43 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:46 +02:00
|
|
|
module.exports = routes;
|