T
TanStack4mo ago
wise-white

Accessing `request` in middleware

Wondering if you can access the request object in global middleware? I only have the response, along with other things.
13 Replies
sensitive-blue
sensitive-blue4mo ago
on the alpha branch you can for 'request' middlewares
wise-white
wise-whiteOP4mo ago
Is there an example @Manuel Schiller ?
sensitive-blue
sensitive-blue4mo ago
we have e2e tests in the repo aside from that, what are you trying to do here?
wise-white
wise-whiteOP4mo ago
need to authenticate my app and store the session in the context or headers so I can reuse it elsewhere
wise-white
wise-whiteOP4mo ago
this is what I hav so far https://pastebin.com/A6GhLVPK
Pastebin
import { RequestedTokenType } from '@shopify/shopify-api'import { ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
sensitive-blue
sensitive-blue4mo ago
Server Functions | TanStack Start Solid Docs
What are Server Functions? Server functions allow you to specify logic that can be invoked almost anywhere (even the client), but run only on the server. In fact, they are not so different from an API...
wise-white
wise-whiteOP4mo ago
ah sweet, didn't come across this one, thanks
rival-black
rival-black3mo ago
Hey Manuel, right now, getWebRequest doesn't return the expected url when used in a server function inside beforeLoad (https://github.com/TanStack/router/issues/4115). Would you have a workaround in the meantime to get the url pathname?
GitHub
Issues · TanStack/router
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - Issues · TanStack/router
sensitive-blue
sensitive-blue3mo ago
did you try on the alpha version? it should just work https://github.com/TanStack/router/discussions/2863#discussioncomment-13104960
flat-fuchsia
flat-fuchsia3mo ago
It works correctly on the alpha:
import { getWebRequest, setResponseStatus } from '@tanstack/react-start/server'

export const authMiddleware = createMiddleware({ type: 'function' })
.server(async ({ next }) => {
const request = getWebRequest()
// Logic here
return next({
context,
})
})
import { getWebRequest, setResponseStatus } from '@tanstack/react-start/server'

export const authMiddleware = createMiddleware({ type: 'function' })
.server(async ({ next }) => {
const request = getWebRequest()
// Logic here
return next({
context,
})
})
sensitive-blue
sensitive-blue3mo ago
if you could comment that on the GitHub issue please then I can close that
flat-fuchsia
flat-fuchsia3mo ago
the OP is about the middleware but @pvman seems to be asking about other thing not related to it, request.url on a server function ( called from client ) should never actual return the current router url but return the actual URI of the RPC being called, for me thats thats intended behaviour. :p ill share my opinion there and on SSR indeed returns the correct url https://github.com/TanStack/router/issues/4115#issuecomment-2946266243
rival-black
rival-black3mo ago
Yes exactly, my bad that was a misunderstanding on my part. What I was looking for was
beforeLoad: async (args) => { const pathname = args.location.pathname; ... }
beforeLoad: async (args) => { const pathname = args.location.pathname; ... }
, in fact I need to update a cookie at each route change (both initial page load and client side routing) so beforeLoad was needed. But now I have another problem, I get i18n data in the loader and it doesn't fire on client side routing, so I was trying to invalidate the router in beforeLoad. If you have some time here is the link to the issue https://discord.com/channels/719702312431386674/1381935859607011358/1381935859607011358

Did you find this page helpful?