Astro middleware are functions that run on the server before a page is displayed to the user. Astro locals are a way to store data that is specific to the current request. We can use these features to store the user's account data, so that it is available to all pages.

Create a new file in the src/ directory called middleware.js:


To ensure the locals object is typed correctly, we can add a type definition for it in a env.d.ts file at the root of the project:


Now, use the locals object in the home page to redirect based on the user's login status. Overwrite the file in the src/pages directory called index.astro:


If the user is logged in, they will be redirected to the account page. If they are not logged in, they will be redirected to the sign up page.