Skip to content

Commit

Permalink
chore: add libs for auth & db
Browse files Browse the repository at this point in the history
  • Loading branch information
ayusshrathore committed Jun 5, 2023
1 parent db87323 commit e5edfc0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
5 changes: 5 additions & 0 deletions libs/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import axios from "axios";

const fetcher = (url: string) => axios.get(url).then((res) => res.data);

export default fetcher;
11 changes: 11 additions & 0 deletions libs/prismadb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PrismaClient } from "@prisma/client";

declare global {
var prisma: PrismaClient | undefined;
}

const client = globalThis.prisma || new PrismaClient();

if (process.env.NODE_ENV !== "production") globalThis.prisma = client;

export default client;
27 changes: 27 additions & 0 deletions libs/serverAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NextApiRequest, NextApiResponse } from "next";

import prisma from "@/libs/prismadb";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { getServerSession } from "next-auth";

const serverAuth = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getServerSession(req, res, authOptions);

if (!session?.user?.email) {
throw new Error("Unauthorized");
}

const currentUser = await prisma.user.findUnique({
where: {
email: session.user.email,
},
});

if (!currentUser) {
throw new Error("Unauthorized");
}

return { currentUser };
};

export default serverAuth;
13 changes: 0 additions & 13 deletions pages/_document.tsx

This file was deleted.

0 comments on commit e5edfc0

Please sign in to comment.