fix: Improve authentication handling in fetchWithAuth.#1373
fix: Improve authentication handling in fetchWithAuth.#1373anuragchvn-blip wants to merge 1 commit intosupabase:masterfrom
Conversation
| const accessToken = (await getAccessToken()) ?? supabaseKey | ||
| let accessToken: string | null | ||
| try { | ||
| accessToken = (await getAccessToken()) ?? supabaseKey |
There was a problem hiding this comment.
It's great to see the error handling here! Just a thought: the catch block will only execute if getAccessToken() throws an error. If getAccessToken() returns null (and doesn't throw an error), the catch block won't be reached. We might want to add a check to ensure accessToken has a valid value after the try block, or clarify the expected behavior of getAccessToken() to ensure it always either returns a token or throws an error.
There was a problem hiding this comment.
Thanks for the feedback, @m1nsuppp! You're absolutely right—if getAccessToken() returns null without throwing an error, the catch block won't be triggered. I’ll update the implementation to explicitly check whether accessToken is valid after the try block. We could either throw an error manually if it's null or ensure getAccessToken() always throws when it fails.
Would love your thoughts on the best approach here!
|
Hi @anuragchvn-blip ! Thank you for taking the time to contribute to Supabase and for thinking about improving error handling! I appreciate your attention to detail. However, I need to close this PR because the current implementation is actually working as intended. Let me explain why the existing code handles all the expected scenarios correctly. The current line: already provides robust handling for all scenarios:
Adding error logging when
Example: In a blog app, when an anonymous visitor loads the homepage to see public posts, As @m1nsuppp correctly pointed out, the catch block would only execute if The current implementation follows the principle of "simple and predictable" - it handles both success and expected failure cases without unnecessary complexity. Thanks again for taking the time to contribute to Supabase, and please don't hesitate to submit future PRs! Your engagement with the codebase is really appreciated! |
🚀 What This PR Does
fetchWithAuthby adding better error handling forgetAccessToken().🔧 Why This Is Important
✅ How to Test
getAccessToken()to verify error logging.Would love feedback from maintainers! 🚀