#middleware

11 messages · Page 1 of 1 (latest)

analog axle
#

Why does my middleware doesn't execute knowing that it has the path src/middleware.ts and the code inside it is :
import {defineMiddleware} from "astro/middleware";
import { checkToken } from "../db/utils";
export const onRequest = defineMiddleware((context, next) => {
console.log("middleware checked !!!!")
context.locals.user = {username:"middlewareWorked !", email:"", orders:[]};
const auth_token = context.cookies.get("auth_token");
if(!auth_token || !auth_token.value){
context.locals.user = null;
next();
}
checkToken(auth_token.value??"");
})

arctic bisonBOT
#

Hi @analog axle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It seems that you're missing a call to next() in your middleware. Middleware needs to either return a Response object or call the next function. In your code, you're not calling next() when the auth_token is valid. Update your middleware code like this:

import { defineMiddleware } from "astro/middleware";
import { checkToken } from "../db/utils";

export const onRequest = defineMiddleware((context, next) => {
  console.log("middleware checked !!!!");
  context.locals.user = { username: "middlewareWorked !", email: "", orders: [] };
  const auth_token = context.cookies.get("auth_token");
  if (!auth_token || !auth_token.value) {
    context.locals.user = null;
    next();
  } else {
    checkToken(auth_token.value ?? "");
    next();
  }
});

Now, the middleware should execute properly. If you still encounter issues, make sure that you have enabled middleware in your astro.config.mjs file as shown in the Astro 2.4 blog post (https://astro.build/blog/astro-240):

import { defineConfig } from "astro/config";

export default defineConfig({
  experimental: {
    middleware: true,
  },
});

Keep in mind that the middleware feature was experimental in Astro 2.4. In Astro 2.6, the middleware API is stable and available without a flag. You can read more about it in the Astro 2.6 blog post (https://astro.build/blog/astro-260) and the Middleware documentation guide (https://docs.astro.build/en/guides/middleware).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Astro

A handful of experimental features go stable in Astro 2.6 including middleware, hybrid SSR output mode, custom client directives, and CSS inlining. Experimental support for a new redirects API is also introduced.

#

@analog axle kapa.ai is still learning and improving, please let us how it did by reacting below

analog axle
#

why does my middleware context.cookies are empty knowing that i have cookies @arctic bison

arctic bisonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

It's difficult to determine the exact cause of the issue without more information. However, I can provide some suggestions based on the information extracts provided.

  1. Make sure you're using the correct version of Astro that supports middleware. Middleware was introduced as an experimental feature in Astro 2.4 (https://astro.build/blog/astro-240) and became stable in Astro 2.6 (https://astro.build/blog/astro-260). Update your Astro version if needed.

  2. Ensure that you have enabled middleware in your astro.config.mjs file as shown in the Astro 2.4 blog post (https://astro.build/blog/astro-240):

import { defineConfig } from "astro/config";

export default defineConfig({
  experimental: {
    middleware: true,
  },
});
  1. Verify that your cookies are being set correctly in the browser and that they are being sent with the request. You can check this using your browser's developer tools.

  2. If you're still having issues, consider providing a minimal reproducible example or more information about your setup, such as the Astro version you're using, your browser, and any relevant code snippets.

For more information on using middleware and context.cookies, refer to the Astro documentation (https://docs.astro.build/en/reference/api-reference#contextcookies).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Astro

Stronger CSS scoping • Code component improvements • Middleware • CSS inlining • More!

Astro Documentation

Build faster websites with less client-side JavaScript.

#

@analog axle kapa.ai is still learning and improving, please let us how it did by reacting below

analog axle
#

how can i upgrade to astro's latest version @arctic bison

arctic bisonBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To upgrade your project to the latest version of Astro, use your package manager to update the Astro dependency. Here are the commands for different package managers:

npm:

npm install astro@latest

pnpm:

pnpm install astro@latest

Yarn:

yarn add astro@latest

Additionally, you should also update any Astro integrations you're using to their latest versions. For example, if you're using React and Tailwind integrations:

npm:

npm install @astrojs/react@latest @astrojs/tailwind@latest

pnpm:

pnpm install @astrojs/react@latest @astrojs/tailwind@latest

Yarn:

yarn add @astrojs/react@latest @astrojs/tailwind@latest

For more information on upgrading to the latest version of Astro, refer to the Upgrade to Astro v2 guide (https://docs.astro.build/en/guides/upgrade-to/v2).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: