Cookies #710
-
Is it possible to manage cookies? Or is there a better way to manage authentication? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello @fabioziliani , In order to manage (read and set) cookies, you might need to connect You can achieve it by using After that, const authMw = createMiddleware({
input: z.object({}),
middleware: async ({request, response}) => {
const { cookies } = request; // here are your cookies
// do something
response.cookie('anotherOne', 'some value'); // send another cookie
return { cookies }; // provide cookies to other middlewares and endpoint handlers as options
// or just return { isAuthenticated: true } // depending on received cookie examination
}
}); so the final assembly would look this way: const myFactory = defaultEndpointsFactory
.use(cookieParser())
.addMiddleware(authMw);
const myEndpoint = myFactory.build({ /* ... */ });
If this approach is not enough for your case, please provide more details on what exactly is missing or would be great to have. |
Beta Was this translation helpful? Give feedback.
-
Hello @RobinTail and thanks for the reply. |
Beta Was this translation helpful? Give feedback.
Hello @fabioziliani ,
In order to manage (read and set) cookies, you might need to connect
cookie-parser
express middleware.https://expressjs.com/en/api.html#req.cookies
You can achieve it by using
.use()
or.addExpressMiddleware()
method ofEndpointsFactory
.After that,
request.cookies
will be populated by the received cookies. In order to handle them (for authentication purposes) you can write a middleware like this: