N
Nuxt2w ago
ants

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
supabase: {
redirectOptions: {
login: '/login',
callback: '/auth/callback',
exclude: ['/', '/login', '/auth/callback', '/api/scan'],
},
},
supabase: {
redirectOptions: {
login: '/login',
callback: '/auth/callback',
exclude: ['/', '/login', '/auth/callback', '/api/scan'],
},
},
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
supabase: {
redirect: false,
},
supabase: {
redirect: false,
},
2 Replies
kapa.ai
kapa.ai2w ago
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:
supabase: {
redirectOptions: {
login: '/login',
callback: '/auth/callback',
exclude: ['/', '/login', '/auth/callback', '/api/scan/**'],
},
},

supabase: {
redirectOptions: {
login: '/login',
callback: '/auth/callback',
exclude: ['/', '/login', '/auth/callback', '/api/scan/**'],
},
},

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:
export default defineNuxtConfig({
// ... other config
routeRules: {
'/api/scan/**': { redirect: false }
},
// ... supabase config
})

export default defineNuxtConfig({
// ... other config
routeRules: {
'/api/scan/**': { redirect: false }
},
// ... supabase config
})

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: ----------------------
kapa.ai
kapa.ai2w ago
<@1088882379260690453> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Want results from more Discord servers?
Add your server