first commit

This commit is contained in:
eric committed 2022-11-09 21:28:20 +01:00
commit 6857e301fa
23 files changed
+5500

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
import { NextApiHandler } from "next";
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import { PrismaAdapter } from '@next-auth/prisma-adapter'
import GitHubProvider from 'next-auth/providers/github'
import EmailProvider from 'next-auth/providers/email'
import prisma from '../../../lib/prisma'
const authHandler: NextApiHandler = (req, res) => NextAuth(req, res, options);
export default authHandler;
const options = {
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
EmailProvider({
server: {
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
},
from: process.env.SMTP_FROM,
}),
],
adapter: PrismaAdapter(prisma),
secret: process.env.SECRET,
};
+26
View File
@@ -0,0 +1,26 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react';
import prisma from '../../../lib/prisma'
// DELETE /api/post/:id
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const postId = req.query.id;
const session = await getSession({ req })
if (req.method === "DELETE") {
if (session) {
const post = await prisma.post.delete({
where: { id: Number(postId) },
});
res.json(post);
} else {
res.status(401).send({ message: 'Unauthorized' })
}
} else {
throw new Error(
`The HTTP ${req.method} method is not supported at this route.`
);
}
}
+25
View File
@@ -0,0 +1,25 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import prisma from '../../../lib/prisma'
import { getSession } from 'next-auth/react'
// POST /api/post
// Required fields in body: title
// Optional fields in body: content
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const { title, content } = req.body;
const session = await getSession({ req });
if (session) {
const result = await prisma.post.create({
data: {
title: title,
content: content,
author: { connect: { email: session?.user?.email } },
},
});
res.json(result);
} else {
res.status(401).send({ message: 'Unauthorized' })
}
}
+19
View File
@@ -0,0 +1,19 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react';
import prisma from '../../../lib/prisma'
// PUT /api/publish/:id
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const postId = req.query.id;
const session = await getSession({ req })
if (session) {
const post = await prisma.post.update({
where: { id: Number(postId) },
data: { published: true },
});
res.json(post);
} else {
res.status(401).send({ message: 'Unauthorized' })
}
}