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
cje
cje2y ago
check for a user at the start of the procedure and then put an if statement?
Froxx
Froxx2y ago
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
Want results from more Discord servers?
Add your server