From 06c7fa9759fe95b4cb69525d1d35c3224f1acf83 Mon Sep 17 00:00:00 2001 From: Amit Amrutiya Date: Mon, 24 Jun 2024 11:44:05 +0530 Subject: [PATCH] make homepage as server component Signed-off-by: Amit Amrutiya --- frontend/emails/VerificationEmail.tsx | 2 +- frontend/src/actions/user.ts | 9 +++--- frontend/src/app/page.tsx | 2 +- frontend/src/auth.config.ts | 26 +++++++++++----- frontend/src/auth.ts | 11 ++++--- .../src/components/ChatRoomProfileSction.tsx | 2 +- frontend/src/components/Icon-card-button.tsx | 9 ++---- frontend/src/components/JoinRoomForm.tsx | 17 +++------- frontend/src/components/LogoutButton.tsx | 11 +------ frontend/src/components/Navbar.tsx | 31 +++++++++---------- frontend/src/components/RoomIdForm.tsx | 5 ++- .../src/components/ui/background-gradient.tsx | 2 ++ frontend/src/components/ui/form.tsx | 2 ++ frontend/src/components/ui/meteors.tsx | 1 + frontend/src/lib/auth.ts | 7 +++++ frontend/tailwind.config.ts | 6 +--- 16 files changed, 73 insertions(+), 70 deletions(-) create mode 100644 frontend/src/lib/auth.ts diff --git a/frontend/emails/VerificationEmail.tsx b/frontend/emails/VerificationEmail.tsx index 1bce3e3..c7605a5 100644 --- a/frontend/emails/VerificationEmail.tsx +++ b/frontend/emails/VerificationEmail.tsx @@ -26,7 +26,7 @@ export default function VerificationEmail({ Verification Code
- Connect Friends + Meet ChillChat
{ try { const user = await db.user.findFirst({ where: { @@ -16,7 +17,7 @@ export async function getUserByIdentifier(identifier: string) { } } -export async function getUserById(id: string) { +export async function getUserById(id: string): Promise { try { const User = await db.user.findUnique({ where: { diff --git a/frontend/src/app/page.tsx b/frontend/src/app/page.tsx index 00d62c0..b9bd8e8 100644 --- a/frontend/src/app/page.tsx +++ b/frontend/src/app/page.tsx @@ -13,7 +13,7 @@ export default function Home() {
Miss ?
All in one Website for Meet diff --git a/frontend/src/auth.config.ts b/frontend/src/auth.config.ts index b74bc98..1ca4358 100644 --- a/frontend/src/auth.config.ts +++ b/frontend/src/auth.config.ts @@ -3,17 +3,33 @@ import Credentials from "next-auth/providers/credentials"; import Google from "next-auth/providers/google"; import { signInSchema } from "./schemas/signinSchema"; import { getUserByIdentifier } from "./actions/user"; +import bcrypt from "bcryptjs"; export default { providers: [ + Google({ + clientId: process.env.GOOGLE_CLIENT_ID || "", + clientSecret: process.env.GOOGLE_CLIENT_SECRET || "", + }), Credentials({ - async authorize(credentials: any): Promise { + async authorize(credentials): Promise { const validateFields = signInSchema.safeParse(credentials); if (validateFields.success) { try { - const user = await getUserByIdentifier(credentials.identifier); - return user; + const user = await getUserByIdentifier( + validateFields.data.identifier + ); + if (!user || !user.password) return null; + const passwordMatch = await bcrypt.compare( + validateFields.data.password, + user.password + ); + + if (passwordMatch) { + return user; + } + return null; } catch (error) { throw new Error("Error in authorization" + error); } @@ -23,9 +39,5 @@ export default { } }, }), - Google({ - clientId: process.env.GOOGLE_CLIENT_ID || "", - clientSecret: process.env.GOOGLE_CLIENT_SECRET || "", - }), ], } satisfies NextAuthConfig; diff --git a/frontend/src/auth.ts b/frontend/src/auth.ts index e7609d4..d9f0827 100644 --- a/frontend/src/auth.ts +++ b/frontend/src/auth.ts @@ -17,15 +17,16 @@ export const { ...authConfig, callbacks: { async signIn({ account, user }) { - if (account?.provider === "google") { - // return Boolean(user.is_verified && user.email?.endsWith("@gmail.com")); - return true; - } + // if (account?.provider === "google") { + // return Boolean(user.is_verified && user.email?.endsWith("@gmail.com")); + // return true; + // } + if (account?.provider !== "credentials") return true; + const existingUser = await getUserById(user.id!); if (!existingUser || !existingUser.is_verified) { return false; } - return true; }, async jwt({ token, user }) { diff --git a/frontend/src/components/ChatRoomProfileSction.tsx b/frontend/src/components/ChatRoomProfileSction.tsx index a41ae16..77369fc 100644 --- a/frontend/src/components/ChatRoomProfileSction.tsx +++ b/frontend/src/components/ChatRoomProfileSction.tsx @@ -53,7 +53,7 @@ function ChatRoomProfileSction() {
- Connect Friends + Meet ChillChat
diff --git a/frontend/src/components/Icon-card-button.tsx b/frontend/src/components/Icon-card-button.tsx index 01f48d6..721496d 100644 --- a/frontend/src/components/Icon-card-button.tsx +++ b/frontend/src/components/Icon-card-button.tsx @@ -1,16 +1,13 @@ -"use client"; - import React from "react"; import functions from "@/app/data/functions"; import Link from "next/link"; -import { useSession } from "next-auth/react"; import { PinContainer } from "./ui/3d-pin"; import { BackgroundGradient } from "./ui/background-gradient"; import { Meteors } from "./ui/meteors"; +import { currentUser } from "@/lib/auth"; -const IconCardButton = () => { - const session = useSession(); - const user = session.data?.user; +const IconCardButton = async () => { + const user = await currentUser(); return functions.map((f, i) => ( diff --git a/frontend/src/components/JoinRoomForm.tsx b/frontend/src/components/JoinRoomForm.tsx index 0c2a8d5..4cd8dc2 100644 --- a/frontend/src/components/JoinRoomForm.tsx +++ b/frontend/src/components/JoinRoomForm.tsx @@ -1,20 +1,13 @@ -"use client"; - import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; -import { useSession } from "next-auth/react"; -import { Skeleton } from "@/components/ui/skeleton"; import RoomIdForm from "./RoomIdForm"; import SignInForm from "./SignInForm"; import SignupForm from "./SignupForm"; +import { currentUser } from "@/lib/auth"; -function JoinRoomForm() { - const session = useSession(); - const user = session.data?.user; - - return session.status === "loading" ? ( - - ) : ( -
+async function JoinRoomForm() { + const user = await currentUser(); + return ( +
{user ? ( ) : ( diff --git a/frontend/src/components/LogoutButton.tsx b/frontend/src/components/LogoutButton.tsx index 6e50a4d..ca4f75f 100644 --- a/frontend/src/components/LogoutButton.tsx +++ b/frontend/src/components/LogoutButton.tsx @@ -2,18 +2,9 @@ import React from "react"; import { Button } from "./ui/button"; -import { useSession } from "next-auth/react"; -import { Skeleton } from "@/components/ui/skeleton"; import { signOut } from "next-auth/react"; -function LogoutButton() { - const session = useSession(); - const user = session.data?.user; - - if (session.status === "loading") { - return ; - } - +function LogoutButton(user: { user: any }) { if (!user) { return null; } diff --git a/frontend/src/components/Navbar.tsx b/frontend/src/components/Navbar.tsx index eb38c7a..7bf0872 100644 --- a/frontend/src/components/Navbar.tsx +++ b/frontend/src/components/Navbar.tsx @@ -1,42 +1,39 @@ -"use client"; - import React from "react"; import { AudioLines, ArrowLeftRightIcon } from "lucide-react"; -import { User } from "@/type"; import UserAvatar from "./UserAvatar"; -import { useSession } from "next-auth/react"; import LogoutButton from "./LogoutButton"; +import { currentUser } from "@/lib/auth"; +import { User } from "@prisma/client"; export interface NavbarProps { remoteUser?: User | undefined | null; remoteSocketId?: string | undefined | null; } -const Navbar: React.FC = (props) => { +const Navbar: React.FC = async (props) => { const { remoteUser, remoteSocketId } = props; - const session = useSession(); - const currentUser = session.data?.user; + const user = await currentUser(); return (