Added GET and POST routes.

This commit is contained in:
Daniel 2023-02-09 00:41:33 +03:30
parent e5ed2ec5db
commit 0bf1ec0d2a
12 changed files with 198 additions and 36 deletions

2
.env
View File

@ -1,2 +0,0 @@
DATABASE_URL="postgresql://daniel:password@localhost:5432/mydb?schema=public"
NEXTAUTH_SECRET=very_sensitive_secret

3
.gitignore vendored
View File

@ -25,8 +25,9 @@ yarn-debug.log*
yarn-error.log* yarn-error.log*
.pnpm-debug.log* .pnpm-debug.log*
# local env files # env files
.env*.local .env*.local
.env
# vercel # vercel
.vercel .vercel

View File

@ -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<Collections[]>([]);
useEffect(() => {
fetch("/api/routes/collections/getCollections")
.then((res) => res.json())
.then((data) => setCollections(data.response));
}, []);
return (
<div className="flex flex-wrap">
{collections.map((e) => {
return (
<div className="p-5 bg-gray-100 m-2 w-max ">
<p>{e.name}</p>
</div>
);
})}
</div>
);
}

View File

@ -1,3 +1,3 @@
export { default } from "next-auth/middleware"; export { default } from "next-auth/middleware";
export const config = { matcher: ["/"] }; export const config = { matcher: ["/", "/dashboard"] };

View File

@ -14,6 +14,7 @@ export const authOptions: AuthOptions = {
credentials: {}, credentials: {},
async authorize(credentials, req) { async authorize(credentials, req) {
const { email, password } = credentials as { const { email, password } = credentials as {
id: string;
email: string; email: string;
password: string; password: string;
}; };
@ -28,21 +29,6 @@ export const authOptions: AuthOptions = {
console.log(findUser); 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; let passwordMatches: boolean = false;
if (findUser?.password) { if (findUser?.password) {
@ -50,13 +36,24 @@ export const authOptions: AuthOptions = {
} }
if (passwordMatches) { if (passwordMatches) {
return { name: findUser?.name, email: findUser?.email }; return {
id: findUser?.id,
name: findUser?.name,
email: findUser?.email,
};
} else return null as any; } else return null as any;
}, },
}), }),
], ],
pages: { pages: {
signIn: "/auth/login", signIn: "/login",
},
callbacks: {
session: async ({ session, token }) => {
session.user.id = token?.sub;
return session;
},
}, },
}; };

View File

@ -1,10 +1,10 @@
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "pages/api/auth/[...nextauth]"; import { authOptions } from "pages/api/auth/[...nextauth]";
import { prisma } from "@/lib/db";
type Data = { type Data = {
message: string; response: object[] | string;
data?: object;
}; };
export default async function handler( export default async function handler(
@ -13,14 +13,32 @@ export default async function handler(
) { ) {
const session = await getServerSession(req, res, authOptions); const session = await getServerSession(req, res, authOptions);
if (!session) { if (!session?.user?.email) {
res.status(401).json({ message: "You must be logged in." }); return res.status(401).json({ response: "You must be logged in." });
return;
} }
console.log(session?.user?.email); const email: string = session.user.email;
return res.json({ const findCollection = await prisma.user.findFirst({
message: "Success", 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 || [],
}); });
} }

View File

@ -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<Data>
) {
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",
});
}

21
pages/dashboard.tsx Normal file
View File

@ -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 (
<div className="p-5">
<div>
<p className="text-3xl font-bold text-center mb-10">Linkwarden</p>
</div>
<div>
<p>Welcome {user?.name?.toLocaleUpperCase()}!</p>
</div>
<Collections />
</div>
);
}

View File

@ -1,7 +1,10 @@
import { useRouter } from "next/router";
import { useEffect } from "react";
export default function Home() { export default function Home() {
return ( const router = useRouter();
<div className="p-5">
<p className="text-3xl font-bold text-center mb-10">Linkwarden</p> useEffect(() => {
</div> router.push("/dashboard");
); }, []);
} }

View File

@ -15,8 +15,8 @@ export default function Login() {
useEffect(() => { useEffect(() => {
if (session.status === "authenticated") { if (session.status === "authenticated") {
router.push("/");
console.log("Already logged in."); console.log("Already logged in.");
router.push("/");
} }
}, []); }, []);

View File

@ -15,8 +15,8 @@ export default function Register() {
useEffect(() => { useEffect(() => {
if (session.status === "authenticated") { if (session.status === "authenticated") {
router.push("/");
console.log("Already logged in."); console.log("Already logged in.");
router.push("/");
} }
}, [session]); }, [session]);

9
types/next-auth.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
import NextAuth, { DefaultSession } from "next-auth";
declare module "next-auth" {
interface Session {
user: {
id: any;
} & DefaultSession["user"];
}
}