From 26e99b8ee5e49c3c131128011f1beff2780a0fa9 Mon Sep 17 00:00:00 2001 From: Priyanshu Verma Date: Sat, 19 Oct 2024 12:19:33 +0000 Subject: [PATCH 1/5] fix: auto generate encryption keys if user do not have --- prisma/schema.prisma | 4 +-- src/app/(app)/dashboard/page.tsx | 50 +++++++++++++++++++++++++++----- src/app/(auth)/sign-in/page.tsx | 30 +++++++++---------- src/app/layout.tsx | 9 +++--- src/components/Navbar.tsx | 14 +++------ src/helpers/checkAndSaveKeys.ts | 35 ++++++++++++++++++++++ 6 files changed, 103 insertions(+), 39 deletions(-) create mode 100644 src/helpers/checkAndSaveKeys.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 243a8c2..d571efc 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -32,7 +32,7 @@ model EncryptionKey { publicKey String @unique userId String @unique @db.ObjectId testEncryption String @default("unsecured") - user User @relation(fields: [userId], references: [id]) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) } model Message { @@ -41,7 +41,7 @@ model Message { userId String @db.ObjectId createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], references: [id], onDelete: Cascade) } model Account { diff --git a/src/app/(app)/dashboard/page.tsx b/src/app/(app)/dashboard/page.tsx index 8cfb382..3a613f3 100644 --- a/src/app/(app)/dashboard/page.tsx +++ b/src/app/(app)/dashboard/page.tsx @@ -10,7 +10,14 @@ import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; import Navbar from "@/components/Navbar"; import { Separator } from "@/components/ui/separator"; -import { Edit3, GlobeLockIcon, ListX, Loader2, RefreshCcw, Search } from "lucide-react"; +import { + Edit3, + GlobeLockIcon, + ListX, + Loader2, + RefreshCcw, + Search, +} from "lucide-react"; import { Input } from "@/components/ui/input"; import { useUsernameModal, @@ -29,16 +36,43 @@ import Messages from "@/components/Messages"; import { useCheckEncryptionKey } from "@/hooks/check-encryptionkey"; import { useProfileUrl } from "@/hooks/useProfileUrl"; - - - import SearchUser from "@/components/search"; +import SearchUser from "@/components/search"; +import checkAndSaveKeys from "@/helpers/checkAndSaveKeys"; const Page = () => { const modal = useUsernameModal(); const hasEncryptionKey = useCheckEncryptionKey(); const changeEncryptionKeyModal = useChangeEncryptionKeyModal(); const deleteMessagesModal = useDeleteModal(); - const { data: session, status } = useSession(); + const { data: session, status, update } = useSession(); + + useEffect(() => { + const generateKeysIfNeeded = async () => { + if (!hasEncryptionKey) { + try { + // checks and generates key and save in IndexDB + const success = await checkAndSaveKeys(); + if (success) { + await update({ + type: "change_key", + key: true, + }); + window.location.reload(); + } else { + toast.error("Error", { + description: "Failed to auto generate keys", + }); + } + } catch (error) { + toast.error("Error", { + description: "An unexpected error occurred while generating keys", + }); + } + } + }; + + generateKeysIfNeeded(); + }, [hasEncryptionKey, update]); const queryClient = useQueryClient(); @@ -116,10 +150,10 @@ const Page = () => { <>
- {!user.hasEncryptionKey && } - {!hasEncryptionKey && } + {session && !user.hasEncryptionKey && } + {session && !hasEncryptionKey && }

Hi {user.username},

- +
{ const router = useRouter(); @@ -81,9 +82,8 @@ const Page = () => { {showLoaderOverlay && } {/* Outer container */}
@@ -174,19 +174,19 @@ const Page = () => { {/* Sign Up and Forgot Password */}
-
-
Don{"'"}t have an account?
- - Sign Up - -
-
- Forgot Password? -
-
+
+
Don{"'"}t have an account?
+ + Sign Up + +
+
+ Forgot Password? +
+
{/* Back to Home Button */}
diff --git a/src/app/layout.tsx b/src/app/layout.tsx index f1b252c..cbebe90 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -27,11 +27,12 @@ export default async function RootLayout({ children: React.ReactNode; }>) { const session = await getServerSession(authOptions); + return ( <> - - + + - - + + ); diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 058554a..dc08e04 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -16,18 +16,16 @@ import { import Image from "next/image"; - const Navbar = () => { const { data: session, status } = useSession(); const loading = status === "loading"; const loggedIn = !!session; - const { resolvedTheme } = useTheme(); - const logoSrc = resolvedTheme === "dark" ? "/assets/logo1.png" : "/assets/logo.png"; + const logoSrc = + resolvedTheme === "dark" ? "/assets/logo1.png" : "/assets/logo.png"; const profileIconSrc = "/assets/profile-icon.jpg"; // Replace with your profile icon source - return (