summaryrefslogtreecommitdiff
path: root/examples/middleware/src/middleware.ts
diff options
context:
space:
mode:
Diffstat (limited to 'examples/middleware/src/middleware.ts')
-rw-r--r--examples/middleware/src/middleware.ts83
1 files changed, 0 insertions, 83 deletions
diff --git a/examples/middleware/src/middleware.ts b/examples/middleware/src/middleware.ts
deleted file mode 100644
index 4854105ca..000000000
--- a/examples/middleware/src/middleware.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-import { defineMiddleware, sequence } from 'astro:middleware';
-import htmlMinifier from 'html-minifier';
-
-const limit = 50;
-
-const loginInfo: {
- token: undefined | string;
- currentTime: undefined | number;
-} = {
- token: undefined,
- currentTime: undefined,
-};
-
-export const minifier = defineMiddleware(async (_context, next) => {
- const response = await next();
- // check if the response is returning some HTML
- if (response.headers.get('content-type') === 'text/html') {
- let headers = response.headers;
- let html = await response.text();
- let newHtml = htmlMinifier.minify(html, {
- removeAttributeQuotes: true,
- collapseWhitespace: true,
- });
- return new Response(newHtml, {
- status: 200,
- headers,
- });
- }
- return response;
-});
-
-const validation = defineMiddleware(async (context, next) => {
- if (context.request.url.endsWith('/admin')) {
- if (loginInfo.currentTime) {
- const difference = new Date().getTime() - loginInfo.currentTime;
- if (difference > limit) {
- console.log('hit threshold');
- loginInfo.token = undefined;
- loginInfo.currentTime = undefined;
- return context.redirect('/login');
- }
- }
- // we naively check if we have a token
- if (loginInfo.token && loginInfo.token === 'loggedIn') {
- // we fill the locals with user-facing information
- context.locals.user = {
- name: 'AstroUser',
- surname: 'AstroSurname',
- };
- return await next();
- } else {
- loginInfo.token = undefined;
- loginInfo.currentTime = undefined;
- return context.redirect('/login');
- }
- } else if (context.request.url.endsWith('/api/login')) {
- const response = await next();
- // the login endpoint will return to us a JSON with username and password
- if (response.headers.get('content-type') === 'application/json') {
- const data = await response.json();
- // we naively check if username and password are equals to some string
- if (data.username === 'astro' && data.password === 'astro') {
- // we store the token somewhere outside of locals because the `locals` object is attached to the request
- // and when doing a redirect, we lose that information
- loginInfo.token = 'loggedIn';
- loginInfo.currentTime = new Date().getTime();
- return context.redirect('/admin');
- }
- }
- return response;
- } else if (context.request.url.endsWith('/api/logout')) {
- const response = await next();
- if (response.ok) {
- loginInfo.token = undefined;
- loginInfo.currentTime = undefined;
- return context.redirect('/login');
- }
- return response;
- }
- return next();
-});
-
-export const onRequest = sequence(validation, minifier);