Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/content/docs/en/guides/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -733,20 +733,20 @@ Use the [`getActionContext()` function](/en/reference/modules/astro-actions/#get
The following example rejects all action requests that do not have a valid session token. If the check fails, a "Forbidden" response is returned. Note: this method ensures that actions are only accessible when a session is present, but is _not_ a substitute for secure authorization.

```ts title="src/middleware.ts"
import { defineMiddleware } from 'astro:middleware';
import { getActionContext } from 'astro:actions';
import { defineMiddleware } from "astro:middleware";
import { getActionContext } from "astro:actions";

export const onRequest = defineMiddleware(async (context, next) => {
const { action } = getActionContext(context);
// Check if the action was called from a client-side function
if (action?.calledFrom === 'rpc') {
if (action?.calledFrom === "rpc") {
// If so, check for a user session token
if (!context.cookies.has('user-session')) {
return new Response('Forbidden', { status: 403 });
if (!context.cookies.has("user-session")) {
return new Response("Forbidden", { status: 403 });
}
}
context.cookies.set('user-session', /* session token */);

context.cookies.set("user-session", "session-token-value");
return next();
});
```
Expand Down
4 changes: 2 additions & 2 deletions src/content/docs/en/guides/on-demand-rendering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ This content will be server-rendered on demand!
Just add an adapter integration for a server runtime!
All other pages are statically-generated at build time!
-->
<html>
</html>
```

The following example shows opting out of prerendering in order to display a random number each time the endpoint is hit:
Expand Down Expand Up @@ -106,7 +106,7 @@ export const prerender = true
`output: 'server'` is configured, but this page is static!
The rest of my site is rendered on demand!
-->
<html>
</html>
```

Add `export const prerender = true` to any page or route to prerender a static page or endpoint:
Expand Down
9 changes: 7 additions & 2 deletions src/content/docs/en/guides/server-islands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ To access information from the page's URL, you can check the [Referer](https://d

```astro
---
const referer = Astro.request.headers.get('Referer');
const referer = Astro.request.headers.get("Referer");

if (!referer) {
throw new Error("Referer header is missing");
}

const url = new URL(referer);
const productId = url.searchParams.get('product');
const productId = url.searchParams.get("product");
---
```

Expand Down
4 changes: 3 additions & 1 deletion src/content/docs/en/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ const cart = await Astro.session?.get('cart');
In API endpoints, the session object is available on the `context` object. For example, to add an item to a shopping cart:

```ts title="src/pages/api/addToCart.ts" "context.session"
import type { APIContext } from "astro";

export async function POST(context: APIContext) {
const cart = await context.session?.get('cart') || [];
const data = await context.request.json<{ item: string }>();
const data = await context.request.json();
if(!data?.item) {
return new Response('Item is required', { status: 400 });
}
Expand Down
Loading