Supabase auth redirect rules
I have a Nuxt app with an API route defined at
/server/api/scan/[club].ts
, but accessing it will redirect the user to the /login
route. This is my Nuxt config
how could I exclude the dynamic server route from the redirect middleware? Also /api/scan/*
and that also doesn't work, while turning off the redirects fixes it obviously
2 Replies
Hi @ants. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the information provided in the knowledge sources, I can offer some insights into your Supabase auth redirect issue with Nuxt.
The problem you're experiencing is related to the default behavior of the Supabase module in Nuxt. As mentioned in the Stack Overflow post about Nuxt.js localhost redirects (https://stackoverflow.com/questions/78746436), the Supabase module automatically redirects to the login page if a non-authenticated user tries to access a guarded route.
To address your specific issue with the dynamic server route, you can try the following approaches:
1. Modify the
exclude
array in your Supabase configuration:
Instead of using /api/scan
, try using a wildcard pattern to exclude all routes under /api/scan
. Update your configuration like this:
The **
wildcard should match all nested routes under /api/scan
.
2. If the above doesn't work, you can try using Nuxt's routeRules
configuration to override the Supabase redirect for your specific API route. Add this to your nuxt.config.ts
:
This approach uses Nuxt's route rules (https://nuxt.com/docs/api/nuxt-config#routerules-1) to disable redirects for the /api/scan
routes.
3. If you need more fine-grained control, you might need to create a custom middleware to handle the authentication logic for your API routes. This would allow you to bypass the Supabase redirect for specific routes while keeping it active for others.
Remember that completely disabling redirects with redirect: false
in the Supabase config is not recommended if you want to maintain authentication for other parts of your application.
If these solutions don't work, you might need to investigate further into how your server routes are being handled and if there are any conflicts with other middleware or configurations in your Nuxt application.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1088882379260690453> Kapa.ai is still learning and improving, please let me know how I did by reacting below.