UNPKG

nuxt-supabase-team-auth

Version:

Drop-in Nuxt 3 module for team-based authentication with Supabase

64 lines (63 loc) 2.3 kB
import { useTeamAuth } from "../composables/useTeamAuth.js"; import { navigateTo, defineNuxtRouteMiddleware } from "#imports"; export default defineNuxtRouteMiddleware(async (to) => { const { currentUser, currentTeam, isLoading } = useTeamAuth(); if (isLoading.value) { let attempts = 0; const maxAttempts = 20; while (isLoading.value && attempts < maxAttempts) { await new Promise((resolve) => setTimeout(resolve, 100)); attempts++; if (currentUser.value !== void 0) { break; } } if (isLoading.value && attempts >= maxAttempts) { console.warn("[Team Auth] Auth loading timeout in redirect middleware, proceeding anyway"); } } if (currentUser.value) { const redirectTo = to.query.redirect; if (redirectTo) { try { const url = new URL(redirectTo, window.location.origin); if (url.origin === window.location.origin) { return navigateTo(redirectTo); } } catch { } } if (!currentTeam.value) { console.error("[Team Auth] Authenticated user missing team - data integrity issue. User ID:", currentUser.value.id); return navigateTo("/signin?error=account_misconfigured"); } return navigateTo("/dashboard"); } }); export function createRedirectAuthenticated(redirectTo, condition) { return defineNuxtRouteMiddleware(async (to) => { const { currentUser, currentTeam, isLoading } = useTeamAuth(); if (isLoading.value) { let attempts = 0; const maxAttempts = 20; while (isLoading.value && attempts < maxAttempts) { await new Promise((resolve) => setTimeout(resolve, 100)); attempts++; if (currentUser.value !== void 0) { break; } } if (isLoading.value && attempts >= maxAttempts) { console.warn("[Team Auth] Auth loading timeout in custom redirect middleware, proceeding anyway"); } } if (currentUser.value) { if (condition && !condition(currentUser.value, currentTeam.value, to)) { return; } const url = typeof redirectTo === "function" ? redirectTo(currentUser.value, currentTeam.value) : redirectTo; return navigateTo(url); } }); } export const redirectToDashboard = createRedirectAuthenticated("/dashboard");