Loading...
looks like i have a middleware issue. here is some code that might help with redirects/ user login steps.
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs"; import { NextResponse } from "next/server"; import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) { console.log("Middleware running for path:", req.nextUrl.pathname);
const res = NextResponse.next(); const supabase = createMiddlewareClient({ req, res });
// Refresh session if it exists const { data: { session }, } = await supabase.auth.getSession();
console.log("Session in middleware:", session ? "Valid Session" : "No Session");
// Protected routes - routes requiring authentication const protectedRoutes = [ "/dashboard", "/sales", "/customers", "/profile", "/invoices", "/estimates", "/projects", ];
const isProtectedRoute = protectedRoutes.some((route) => req.nextUrl.pathname.startsWith(route));
if (!session && isProtectedRoute) { console.log("No session found, redirecting to login"); return NextResponse.redirect(new URL("/login", req.url)); }
return res; }
// Middleware only runs on specific paths export const config = { matcher: [ "/dashboard/:path*", "/sales/:path*", "/customers/:path*", "/profile/:path*", "/invoices/:path*", "/estimates/:path*", "/projects/:path*", ], };
Loading...
we might have to also update the user login function for redirects as the user once logged in can not access there dashboard
import { supabase } from "./supabase"; // Ensure you have the correct Supabase client
async function handleLogin(email: string, password: string) { try { const { data, error } = await supabase.auth.signInWithPassword({ email, password });
if (error) {
console.error("Login Error:", error);
return {
success: false,
error: error.message,
};
}
if (data.user) {
console.log("User logged in:", data.user);
// Redirect to user-specific dashboard
window.location.href = `/dashboard/${data.user.id}`;
return { success: true, data: data.user };
} else {
return {
success: false,
error: "Login successful, but no user data returned",
};
}
} catch (error) { console.error("Error in handleLogin:", error); return { success: false, error: "An unknown error occurred", }; } }
export { handleLogin };