simplify `feed.all`-resolver
Secondly, I think the supabase user should be propagated in the context so we don't even need to pass in a userId. Right now any user can get any other user's feed.
Someone is attempting to deploy a commit to a Personal Account owned by @thomas-coldwell on Vercel.
@thomas-coldwell first needs to authorize it.
Good point - I've got it sending it's JWT over now and decoding using supabase.auth.api.getUser() in the createContext function. Setting up some middleware to check the user in the ctx is not null - using this a a guide https://trpc.io/docs/middlewares#createprotectedrouter-helper . One issue I did face was that I had to apply the middleware to each sub-router individually rather than the main router as otherwise user would still show up as User | null - let me know if there is a better way to handle this 😀
The approach I'm using right now is that
- I have a
viewerRouterwhich has all the procedures that are individual to a the logged in user - this subroute has that middleware in the top - I use a
splitLinkto make sureviewerprocedures are never done in the same request as the rest - Then, I can then safely-edge add cache headers on all requests on that doesn't contain a
viewerin the procedures called - like this
Does the viewerRouter have sub routers as well then e.g.
viewerRouter()
.middleware(authorizationFunction)
.merge("user.", userRouter)
.merge("feed.", feedRouter);
or are the endpoints added onto the viewerRouter directly:
viewerRouter()
.middleware(authorizationFunction)
.query("user.byId", () => {})
In the second case any queries / mutation have access to the validated context where ctx.user is not null (due to the middleware check, but in the first case the sub-routers are not aware of this middleware and therefore ctx.user can be null, even though it won't be as there is auth middleware higher up checking it.
Is there possible something that could be done as part of the merge that would make sub-routers automatically aware of the context if it is altered by middleware?