Bläddra i källkod

feat(auth): add user-scoped import filtering for cintas calendar summary

- Update getImports action to accept optional userId parameter
- Filter imports by user's file ownership when userId is provided
- Add authentication check in cintas calendar summary page
- Load imports only when user is authenticated
- Handle loading states and error cases for unauthenticated users
vidane 6 månader sedan
förälder
incheckning
a7603ecbb6
2 ändrade filer med 48 tillägg och 10 borttagningar
  1. 26 3
      app/actions/imports.ts
  2. 22 7
      app/cintas-calendar-summary/page.tsx

+ 26 - 3
app/actions/imports.ts

@@ -46,10 +46,15 @@ export async function createImport(data: {
   }
 }
 
-// Get all imports
-export async function getImports() {
+// Get all imports for the current user
+export async function getImports(userId?: string) {
   try {
     const imports = await prisma.import.findMany({
+      where: userId ? {
+        fileId: {
+          not: null,
+        },
+      } : {},
       include: {
         layout: true,
       },
@@ -58,7 +63,25 @@ export async function getImports() {
       },
     });
 
-    return { success: true, data: imports };
+    // Filter by userId manually since Prisma doesn't have direct relation
+    let filteredImports = imports;
+    if (userId) {
+      const fileIds = await prisma.file.findMany({
+        where: {
+          userId: userId,
+        },
+        select: {
+          id: true,
+        },
+      });
+
+      const userFileIds = new Set(fileIds.map(f => f.id));
+      filteredImports = imports.filter(imp =>
+        imp.fileId && userFileIds.has(imp.fileId)
+      );
+    }
+
+    return { success: true, data: filteredImports };
   } catch (error) {
     console.error('Error fetching imports:', error);
     return { success: false, error: 'Failed to fetch imports' };

+ 22 - 7
app/cintas-calendar-summary/page.tsx

@@ -7,6 +7,7 @@ import { Upload, FileText, Database, BarChart3, CheckCircle, Loader2, History, P
 import { UploadForm } from '@/app/components/uploadForm';
 import { createCintasImportRecord, processCintasImportData } from '@/app/actions/cintas-workflow';
 import { getImports } from '@/app/actions/imports';
+import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
 
 interface FileData {
   id: string;
@@ -43,6 +44,7 @@ interface ImportRecord {
 }
 
 export default function CintasCalendarSummaryPage() {
+  const { user } = useKindeBrowserClient();
   const [viewMode, setViewMode] = useState<'imports' | 'new-import' | 'summary'>('imports');
   const [currentStep, setCurrentStep] = useState(1);
   const [uploadedFile, setUploadedFile] = useState<FileData | null>(null);
@@ -56,13 +58,22 @@ export default function CintasCalendarSummaryPage() {
   const [loadingImports, setLoadingImports] = useState(true);
 
   useEffect(() => {
-    loadPriorImports();
-  }, []);
+    if (user) {
+      loadPriorImports();
+    }
+  }, [user]);
 
   const loadPriorImports = async () => {
     try {
       setLoadingImports(true);
-      const result = await getImports();
+      setError(null); // Clear any previous errors
+
+      if (!user?.id) {
+        // Don't set error here, just return - user might still be loading
+        return;
+      }
+
+      const result = await getImports(user.id);
       if (result.success && result.data) {
         // Map the data to match our ImportRecord interface
         const mappedData = result.data.map((item: any) => ({
@@ -274,10 +285,14 @@ export default function CintasCalendarSummaryPage() {
                 </div>
               ) : priorImports.length === 0 ? (
                 <div className="text-center py-8">
-                  <p className="text-muted-foreground mb-4">No prior imports found</p>
-                  <Button onClick={handleStartNewImport}>
-                    Create First Import
-                  </Button>
+                  <p className="text-muted-foreground mb-4">
+                    {user ? 'No prior imports found' : 'Please sign in to view imports'}
+                  </p>
+                  {user && (
+                    <Button onClick={handleStartNewImport}>
+                      Create First Import
+                    </Button>
+                  )}
                 </div>
               ) : (
                 <div className="space-y-4">