taczzy
taczzy
CDCloudflare Developers
Created by taczzy on 7/12/2024 in #pages-help
Trying to get up an running with the @cloudflare/pages-plugin-stytch
I have the following folder/file structure set up for my CF Page Functions:
/functions
|_ _middleware.js
|_ /api (directory)
|__ activities.js (returns JSON)
/functions
|_ _middleware.js
|_ /api (directory)
|__ activities.js (returns JSON)
Inside of my _middleware.ts file I have the following code:
import stytchPlugin from "@cloudflare/pages-plugin-stytch";
import { envs } from "@cloudflare/pages-plugin-stytch/api";

export const onRequest = (context) => {
const url = new URL(context.request.url);
if(url.hostname === "localhost" || url.hostname === "127.0.0.1"){
return context.next();
}
try {
stytchPlugin({
project_id: context.env.PROJECT_ID,
secret: context.env.PROJECT_SECRET,
env: envs.test
});
return context.next();
} catch (error) {
console.log(error);
return context.next();
}
};
import stytchPlugin from "@cloudflare/pages-plugin-stytch";
import { envs } from "@cloudflare/pages-plugin-stytch/api";

export const onRequest = (context) => {
const url = new URL(context.request.url);
if(url.hostname === "localhost" || url.hostname === "127.0.0.1"){
return context.next();
}
try {
stytchPlugin({
project_id: context.env.PROJECT_ID,
secret: context.env.PROJECT_SECRET,
env: envs.test
});
return context.next();
} catch (error) {
console.log(error);
return context.next();
}
};
The documentation on the @Cloudflare/pages-plugin-stytch plugin is pretty light, so I've been reading through the src code for it to better understand it. My assumption is that when a user goes to my-site.pages.dev, this middleware will run, the @Cloudflare/pages-plugin-stytch will add the validated session response containing user information to subsequent Pages Functions on the context.data.stytch.session key. https://github.com/cloudflare/pages-plugins/blob/main/packages/stytch/functions/_middleware.ts#L53-L55 However, when I hit my-site.pages.dev/api/activities in the browser and I check the real-time logs of the context object in that CF Pages Function handler from /api/activities, the data object is empty.
"logs": [
{
"message": [
{
"context": {
"request": {},
"functionPath": "/api/activities",
"params": {},
"data": {},
...
"logs": [
{
"message": [
{
"context": {
"request": {},
"functionPath": "/api/activities",
"params": {},
"data": {},
...
Curious what part of the puzzle I'm missing here. Any help would be much appreciated.
1 replies