optional authenticated tRPC function
Hi, how can I build a function that can handle a user possibly signed in?
I feel like it’s pretty common pattern, but I fine myself defining 2 functions that are basically the same, one for authenticated users and one for unauthenticated.
Thanks for the help
2 Replies
check for a user at the start of the procedure and then put an if statement?
Usually Auth is handled in a procedure. But if an endpoint shall be conditionally open, I would create a trpc endpoint based on a not authed procedure, check for a session in the endpoint and conditionally throw an unauthorized error. You could of course write a util function for that and implement it in all endpoints like that