Set a cookie when locales are accessed

Allows server to know language with each request
This commit is contained in:
Robbie Antenesse 2020-02-24 11:29:07 -07:00
parent 1aa4d6346d
commit 9f736d4731
2 changed files with 9 additions and 1 deletions

View File

@ -11,7 +11,12 @@ async function routes(fastify, options) {
response.locale = fastify.i18n[request.params.locale];
}
return response;
return reply.setCookie('lang', request.params.locale, {
path: '/',
expires: new Date('December 31, 9999'), // Don't expire
maxAge: new Date('December 31, 9999'), // Both are set as a "just in case"
sameSite: true, // Prevents the cookie from being used outside of this site
}).send(response);
});
fastify.get('/locales/:locale/page/:page', async (request, reply) => {

View File

@ -82,6 +82,9 @@ fastify.addHook('onRequest', async (request, reply) => {
request.user = user;
}
}
if (typeof request.cookies.lang !== 'undefined') {
request.language = request.cookies.lang;
}
});
// Store i18n files in fastify object and register locales routes