How to get request details on server functions in Data mode RSC? #14355
Unanswered
MohamedJabarullah
asked this question in
Q&A
Replies: 1 comment
-
What I do is have a middleware (in root.tsx) and use /contextRequestMiddleware.ts import { AsyncLocalStorage } from "node:async_hooks";
import type { MiddlewareFunction } from "react-router";
type AsyncContext = {
request: Request;
};
const context = new AsyncLocalStorage<AsyncContext>();
export const contextRequestMiddleware: MiddlewareFunction<Response> = async (
{ request },
next,
) => context.run({ request }, next);
export const getContextRequest = () => context.getStore()!.request; /root.tsx import { contextRequestMiddleware } from "./middlewares/contextRequestMiddleware";
export const middleware = [contextRequestMiddleware]; /yourServerAction.ts "use server";
export const serverAction = async () => {
const request = getContextRequest();
// at this point u have access to the request, so u can check user session etc.
}; hope it helps! 😄
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Server functions are awesome. But when i try to get the cookie details inside server functions. I dont have any idea about it. Since i am hitting external apis with auth token from cookies.
Beta Was this translation helpful? Give feedback.
All reactions