Skip to content

Commit

Permalink
✨ Added error handling to api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dnrm committed Sep 11, 2022
1 parent 8a31abc commit 6757956
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 0 additions & 1 deletion pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter"
import clientPromise from "../../../lib/mongodb"

Expand Down
25 changes: 16 additions & 9 deletions pages/api/delete/[id].ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { connectToDatabase } from '../../../lib/mongodb-old'
import { connectToDatabase } from "../../../lib/mongodb-old";
import { getSession } from "next-auth/react";
import { ObjectID } from "mongodb";

export default async (req: NextApiRequest, res: NextApiResponse) => {
const { db } = await connectToDatabase()
const { db } = await connectToDatabase();
const session = await getSession({ req });

if (!req.query.id) {
return res.status(400).send("No ID provided");
}

const id: string = req.query.id.toString();

if (session && session.user) {
try {
const posts = await db.collection('posts').deleteOne({ _id: ObjectID(id) })
res.status(200).send(posts)
} catch (e) {
res.status(500).json(e)
}
try {
const posts = await db
.collection("posts")
.deleteOne({ _id: ObjectID(id) });
res.status(200).send(posts);
} catch (e) {
res.status(500).json(e);
}
} else {
res.send({ message: 'Not logged in' })
res.send({ message: "Not logged in" });
}
};
5 changes: 5 additions & 0 deletions pages/api/post/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ObjectId } from "bson";

export default async (req: NextApiRequest, res: NextApiResponse) => {
const { db } = await connectToDatabase();

if (!req.query.id) {
return res.status(400).send("No ID provided");
}

const id: string = req.query.id.toString();

const post = await db
Expand Down

0 comments on commit 6757956

Please sign in to comment.