session?.set("user", { _link: record.id }); but const userId = session.get("user"); is null

this is sign-up.ts
import { save, ActionOptions, applyParams } from "gadget-server";

export const run: ActionRun = async ({
params,
record,
logger,
api,
session,
}) => {
applyParams(params, record);
record.lastSignedIn = new Date();
await save(record);
// associate the current user record with the active session
session?.set("user", { _link: record.id });
};

export const onSuccess: ActionOnSuccess = async ({
params,
record,
logger,
api,
}) => {
// Your logic goes here
};

export const options: ActionOptions = {
actionType: "update",
triggers: {
googleOAuthSignIn: true,
emailSignIn: true,
},
};
import { save, ActionOptions, applyParams } from "gadget-server";

export const run: ActionRun = async ({
params,
record,
logger,
api,
session,
}) => {
applyParams(params, record);
record.lastSignedIn = new Date();
await save(record);
// associate the current user record with the active session
session?.set("user", { _link: record.id });
};

export const onSuccess: ActionOnSuccess = async ({
params,
record,
logger,
api,
}) => {
// Your logic goes here
};

export const options: ActionOptions = {
actionType: "update",
triggers: {
googleOAuthSignIn: true,
emailSignIn: true,
},
};
this is routes
/**
* Route handler for POST genSubtitle
*
* @type { RouteHandler }
*/
const route = async ({ request, reply, api,session, logger }) => {
const userId = session.get("user");
logger.info({ userId }, "userId");
#it output null

...
/**
* Route handler for POST genSubtitle
*
* @type { RouteHandler }
*/
const route = async ({ request, reply, api,session, logger }) => {
const userId = session.get("user");
logger.info({ userId }, "userId");
#it output null

...
but in routes it is null ,How can I get the userId? Is getting it from the frontend the only way?
4 Replies
Chocci_Milk
Chocci_Milk6mo ago
Hello, The route possibly doesn't have the same context. Where are you calling the route from?
leshu
leshuOP6mo ago
I'm calling it from the frontend.
Chocci_Milk
Chocci_Milk6mo ago
Are you using useFetch or another method? If you're using useFetch or api.fetch, we add context to the call, otherwise we can't Sorry, also, which frontend? Theme app extension or embedded admin UI? If its from a theme app extension, there's nothing that we can do and you should consider reading this thread: https://discord.com/channels/836317518595096598/1323272209694851082/1323343266967453708 If it's from the admin UI, why aren't you using a global action instead?
leshu
leshuOP6mo ago
neither app extension or embedded admin UI, is a Web app , use tailwind tks,Antonine, The handleGenerateTitle function has been updated to use api.fetch instead of the standard fetch. This change is working correctly. before
const handleGenerateTitle = async () => {
setIsGenerating(true);
try {
const response = await fetch("/genSubtitle", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ longText: description }),
});
if (!response.ok) {
throw new Error("generate title failed");
}
const data = await response.json();
if (data.title) {
setTitle(data.title);
}
} catch (error) {
console.error("generate title failed:", error);
} finally {
setIsGenerating(false);
}
};
const handleGenerateTitle = async () => {
setIsGenerating(true);
try {
const response = await fetch("/genSubtitle", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ longText: description }),
});
if (!response.ok) {
throw new Error("generate title failed");
}
const data = await response.json();
if (data.title) {
setTitle(data.title);
}
} catch (error) {
console.error("generate title failed:", error);
} finally {
setIsGenerating(false);
}
};
and now
const handleGenerateTitle = async () => {
setIsGenerating(true);
try {
const response = await api.fetch("/genSubtitle", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ longText: description }),
});
if (!response.ok) {
throw new Error("generate title failed");
}
const data = await response.json();
if (data.title) {
setTitle(data.title);
}
} catch (error) {
console.error("generate title failed:", error);
} finally {
setIsGenerating(false);
}
};
const handleGenerateTitle = async () => {
setIsGenerating(true);
try {
const response = await api.fetch("/genSubtitle", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ longText: description }),
});
if (!response.ok) {
throw new Error("generate title failed");
}
const data = await response.json();
if (data.title) {
setTitle(data.title);
}
} catch (error) {
console.error("generate title failed:", error);
} finally {
setIsGenerating(false);
}
};

Did you find this page helpful?