Skip to content

Commit

Permalink
Fixed middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
luukbrauckmann committed Oct 1, 2024
1 parent e2daaa6 commit e1785bf
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export const hashSecret = async (secret: string) => {
};

export const datocms = defineMiddleware(async ({ locals }, next) => {
locals.datocmsEnvironment = datocmsEnvironment;
locals.datocmsToken = getSecret('DATOCMS_READONLY_API_TOKEN');
Object.assign(locals, {
datocmsEnvironment: datocmsEnvironment,
datocmsToken: getSecret('DATOCMS_READONLY_API_TOKEN')
});
const response = await next();
return response;
});
Expand All @@ -32,16 +34,18 @@ const i18n = defineMiddleware(async ({ params, request }, next) => {
Object.assign(params, { locale });
}
setLocale(params.locale as SiteLocale);

const response = await next();
return response;
});

const preview = defineMiddleware(async ({ cookies, locals }, next) => {
const previewSecret = getSecret('HEAD_START_PREVIEW_SECRET');
locals.isPreview = getSecret('HEAD_START_PREVIEW');
locals.isPreviewAuthOk = Boolean(previewSecret) && cookies.get(previewCookieName)?.value === await hashSecret(previewSecret);
locals.previewSecret = previewSecret;
const previewSecret = getSecret('HEAD_START_PREVIEW_SECRET') || '';
Object.assign(locals, {
isPreview: getSecret('HEAD_START_PREVIEW'),
isPreviewAuthOk: Boolean(previewSecret) && cookies.get(previewCookieName)?.value === await hashSecret(previewSecret),
previewSecret: previewSecret
});
const response = await next();

if (locals.isPreview) {
Expand Down

0 comments on commit e1785bf

Please sign in to comment.