Added GET and POST routes.
This commit is contained in:
@@ -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;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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 || [],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user