Stytch Pages Plugin 是一个中间件,用于验证所有请求及其 session_token。
npm i @cloudflare/pages-plugin-stytchyarn add @cloudflare/pages-plugin-stytchpnpm add @cloudflare/pages-plugin-stytchbun add @cloudflare/pages-plugin-stytchimport stytchPlugin from "@cloudflare/pages-plugin-stytch";
import { envs } from "@cloudflare/pages-plugin-stytch/api";
export const onRequest: PagesFunction = stytchPlugin({
project_id: "YOUR_STYTCH_PROJECT_ID",
secret: "YOUR_STYTCH_PROJECT_SECRET",
env: envs.live,
});建议将 secret 存储在 KV 中,而不是像上面那样以明文存储。
Stytch Plugin 接受单个参数(一个对象),包含多个属性。project_id 和 secret 是必填字符串,可在 Stytch 仪表板 ↗ 中找到。env 也是必填字符串,可使用 API 中的 envs.test 或 envs.live 变量填充。默认情况下,Plugin 验证传入请求的 session_token cookie,但如果使用其他机制识别用户会话,也可以可选传入 session_token 或 session_jwt 字符串。最后,还可以传入 session_duration_minutes 以延长会话生命周期。有关这些参数的更多信息,请参阅 Stytch 文档 ↗。
验证后的会话响应(包含用户信息)可在后续 Pages Functions 的 data.stytch.session 上使用。