diff --git a/.env b/.env deleted file mode 100644 index f2c01fa..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -DATABASE_URL="postgresql://daniel:password@localhost:5432/mydb?schema=public" -NEXTAUTH_SECRET=very_sensitive_secret \ No newline at end of file diff --git a/.gitignore b/.gitignore index c87c9b3..814dcf1 100644 --- a/.gitignore +++ b/.gitignore @@ -25,8 +25,9 @@ yarn-debug.log* yarn-error.log* .pnpm-debug.log* -# local env files +# env files .env*.local +.env # vercel .vercel diff --git a/components/Collections.tsx b/components/Collections.tsx new file mode 100644 index 0000000..bef54a4 --- /dev/null +++ b/components/Collections.tsx @@ -0,0 +1,28 @@ +import { useEffect, useState } from "react"; + +interface Collections { + id: any; + name: string; + role: string; +} + +export default function Collections() { + const [collections, setCollections] = useState([]); + useEffect(() => { + fetch("/api/routes/collections/getCollections") + .then((res) => res.json()) + .then((data) => setCollections(data.response)); + }, []); + + return ( +
+ {collections.map((e) => { + return ( +
+

{e.name}

+
+ ); + })} +
+ ); +} diff --git a/middleware.ts b/middleware.ts index 90dc125..0f4fc8c 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,3 +1,3 @@ export { default } from "next-auth/middleware"; -export const config = { matcher: ["/"] }; +export const config = { matcher: ["/", "/dashboard"] }; diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index ded3e96..09e2193 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -14,6 +14,7 @@ export const authOptions: AuthOptions = { credentials: {}, async authorize(credentials, req) { const { email, password } = credentials as { + id: string; email: string; password: string; }; @@ -28,21 +29,6 @@ export const authOptions: AuthOptions = { console.log(findUser); - // const findUser = await prisma.user.findMany({ - // where: { - // email: email, - // }, - // include: { - // collections: { - // include: { - // collection: true, - // }, - // }, - // }, - // }); - - // console.log("BOOM!", findUser[0].collections); - let passwordMatches: boolean = false; if (findUser?.password) { @@ -50,13 +36,24 @@ export const authOptions: AuthOptions = { } if (passwordMatches) { - return { name: findUser?.name, email: findUser?.email }; + return { + id: findUser?.id, + name: findUser?.name, + email: findUser?.email, + }; } else return null as any; }, }), ], pages: { - signIn: "/auth/login", + signIn: "/login", + }, + callbacks: { + session: async ({ session, token }) => { + session.user.id = token?.sub; + + return session; + }, }, }; diff --git a/pages/api/routes/collections/getCollections.ts b/pages/api/routes/collections/getCollections.ts index f57cc24..c92faaa 100644 --- a/pages/api/routes/collections/getCollections.ts +++ b/pages/api/routes/collections/getCollections.ts @@ -1,10 +1,10 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { getServerSession } from "next-auth/next"; import { authOptions } from "pages/api/auth/[...nextauth]"; +import { prisma } from "@/lib/db"; type Data = { - message: string; - data?: object; + response: object[] | string; }; export default async function handler( @@ -13,14 +13,32 @@ export default async function handler( ) { const session = await getServerSession(req, res, authOptions); - if (!session) { - res.status(401).json({ message: "You must be logged in." }); - return; + if (!session?.user?.email) { + return res.status(401).json({ response: "You must be logged in." }); } - console.log(session?.user?.email); + const email: string = session.user.email; - return res.json({ - message: "Success", + const findCollection = await prisma.user.findFirst({ + where: { + email: email, + }, + include: { + collections: { + include: { + collection: true, + }, + }, + }, + }); + + const collections = findCollection?.collections.map((e) => { + return { id: e.collection.id, name: e.collection.name, role: e.role }; + }); + + // console.log(session?.user?.email); + + return res.status(200).json({ + response: collections || [], }); } diff --git a/pages/api/routes/collections/postCollection.ts b/pages/api/routes/collections/postCollection.ts new file mode 100644 index 0000000..eef1636 --- /dev/null +++ b/pages/api/routes/collections/postCollection.ts @@ -0,0 +1,87 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { getServerSession } from "next-auth/next"; +import { authOptions } from "pages/api/auth/[...nextauth]"; +import { prisma } from "@/lib/db"; + +type Data = { + response: object[] | string; +}; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + const session = await getServerSession(req, res, authOptions); + + if (!session?.user?.email) { + return res.status(401).json({ response: "You must be logged in." }); + } + + const email: string = session.user.email; + const collectionName: string = req?.body?.collectionName; + + if (!collectionName) { + return res + .status(401) + .json({ response: "Please enter a valid name for the collection." }); + } + + const findCollection = await prisma.user.findFirst({ + where: { + email, + }, + include: { + collections: { + where: { + collection: { + name: collectionName, + }, + }, + }, + }, + }); + + const checkIfCollectionExists = findCollection?.collections[0]; + + if (checkIfCollectionExists) { + return res.status(400).json({ response: "Collection already exists." }); + } + + // const a = await prisma.user.update({ + // where: { + // id: session.user.id, + // }, + // data: { + // // collections: { + // // create: { name: "Das" }, + // // }, + // }, + // include: { + // collections: { include: { collection: true } }, + // }, + // }); + + await prisma.user.update({ + where: { + id: session.user.id, + }, + data: { + collections: { + create: [ + { + role: "owner", + collection: { + create: { + name: collectionName, + }, + }, + }, + ], + }, + }, + }); + + return res.status(200).json({ + response: "Success", + }); +} diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx new file mode 100644 index 0000000..5a255a1 --- /dev/null +++ b/pages/dashboard.tsx @@ -0,0 +1,21 @@ +import { useSession } from "next-auth/react"; +import Collections from "@/components/Collections"; + +export default function Dashboard() { + const { data: session, status } = useSession(); + + const user = session?.user; + + console.log(); + return ( +
+
+

Linkwarden

+
+
+

Welcome {user?.name?.toLocaleUpperCase()}!

+
+ +
+ ); +} diff --git a/pages/index.tsx b/pages/index.tsx index 06b1c39..7c4db0c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,7 +1,10 @@ +import { useRouter } from "next/router"; +import { useEffect } from "react"; + export default function Home() { - return ( -
-

Linkwarden

-
- ); + const router = useRouter(); + + useEffect(() => { + router.push("/dashboard"); + }, []); } diff --git a/pages/auth/login.tsx b/pages/login.tsx similarity index 100% rename from pages/auth/login.tsx rename to pages/login.tsx index db1013e..f4d568a 100644 --- a/pages/auth/login.tsx +++ b/pages/login.tsx @@ -15,8 +15,8 @@ export default function Login() { useEffect(() => { if (session.status === "authenticated") { - router.push("/"); console.log("Already logged in."); + router.push("/"); } }, []); diff --git a/pages/auth/register.tsx b/pages/register.tsx similarity index 100% rename from pages/auth/register.tsx rename to pages/register.tsx index f1b34b1..de5aa6f 100644 --- a/pages/auth/register.tsx +++ b/pages/register.tsx @@ -15,8 +15,8 @@ export default function Register() { useEffect(() => { if (session.status === "authenticated") { - router.push("/"); console.log("Already logged in."); + router.push("/"); } }, [session]); diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts new file mode 100644 index 0000000..ce3db1a --- /dev/null +++ b/types/next-auth.d.ts @@ -0,0 +1,9 @@ +import NextAuth, { DefaultSession } from "next-auth"; + +declare module "next-auth" { + interface Session { + user: { + id: any; + } & DefaultSession["user"]; + } +}