diff --git a/.env b/.env index fea3a81..d697001 100644 --- a/.env +++ b/.env @@ -1,4 +1,3 @@ NEXT_PUBLIC_GITEA_USERNAME=Guusvanmeerveld NEXT_PUBLIC_GITEA_SERVER=git.guusvanmeerveld.dev -DATABASE_URL=postgresql://portfolio:portfolio@localhost:5432/portfolio?schema=public -# NEXT_PUBLIC_ALLOW_REGISTRATION=true \ No newline at end of file +DATABASE_URL=postgresql://portfolio:portfolio@localhost:5432/portfolio?schema=public \ No newline at end of file diff --git a/prisma/migrations/20230214170729_add_admin_to_user/migration.sql b/prisma/migrations/20230214170729_add_admin_to_user/migration.sql new file mode 100644 index 0000000..621b41b --- /dev/null +++ b/prisma/migrations/20230214170729_add_admin_to_user/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - Made the column `name` on table `User` required. This step will fail if there are existing NULL values in that column. + +*/ +-- AlterTable +ALTER TABLE "User" ADD COLUMN "admin" BOOLEAN NOT NULL DEFAULT false, +ALTER COLUMN "name" SET NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f565ef7..ded4add 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -11,8 +11,9 @@ datasource db { } model User { - id Int @id @default(autoincrement()) - email String @unique + id Int @id @default(autoincrement()) + email String @unique + admin Boolean @default(false) password String name String posts Post[] diff --git a/src/pages/api/blog/signup.ts b/src/pages/api/blog/signup.ts index 24f7927..9264f1d 100644 --- a/src/pages/api/blog/signup.ts +++ b/src/pages/api/blog/signup.ts @@ -45,7 +45,8 @@ const handle: NextApiHandler = async (req, res) => { data: { email: signupCredentials.data.email, name: signupCredentials.data.name, - password + password, + admin: process.env.ADMIN_EMAIL === signupCredentials.data.email } }) .then(async (user) => { diff --git a/src/pages/blog/admin.tsx b/src/pages/blog/admin.tsx new file mode 100644 index 0000000..43cfa92 --- /dev/null +++ b/src/pages/blog/admin.tsx @@ -0,0 +1,35 @@ +import { NextPage } from "next"; +import { NextSeo } from "next-seo"; + +import { Post, User } from "@prisma/client"; + +import Layout from "@components/Layout"; +import { withSessionSsr } from "@utils/session"; + +const AdminPage: NextPage<{ user: User; posts: Post & { author: User } }> = ({ + user, + posts +}) => { + return ( + + + Welcome {user.name} + + ); +}; + +export const getServerSideProps = withSessionSsr(async ({ req }) => { + const user = req.session.user; + + if (user === undefined || !user.admin) return { notFound: true }; + + const posts = await prisma.post.findMany({ + orderBy: { createdAt: "desc" }, + take: 5, + include: { author: true } + }); + + return { props: { user, posts } }; +}); + +export default AdminPage; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index f1061e5..dbba8f4 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -25,6 +25,7 @@ export const getStaticProps: GetStaticProps = async () => { repositories.reduce((prev, current) => prev.stars_count > current.stars_count ? prev : current ); + return { props: { isAvailable,