From 7188d603c20156f10c16294feb6056604e6d12d1 Mon Sep 17 00:00:00 2001 From: Brandon Egger Date: Thu, 18 May 2023 20:32:24 -0500 Subject: [PATCH] users can sign in as the admin account --- src/components/Footer.tsx | 25 ++++++++--- src/server/auth.ts | 88 +++++++++++++++++++++++++-------------- 2 files changed, 76 insertions(+), 37 deletions(-) diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx index 5cc9bf4..ac98c2c 100644 --- a/src/components/Footer.tsx +++ b/src/components/Footer.tsx @@ -1,6 +1,7 @@ import { type NextPage } from "next/types"; import Image from "next/image"; import Link from "next/link"; +import { useSession } from "next-auth/react"; interface QuickLink { label: string; @@ -93,6 +94,23 @@ const ContactInfo = ({ name, title, email, phone }: ContactInfo) => { ); }; +const AdminLogin = () => { + const { data: sessionData } = useSession(); + + if (sessionData?.user) { + return {sessionData.user.name}; + } + + return ( + + Site Admin Login + + ); +}; + const FooterLabeledSection = ({ title, children, @@ -141,12 +159,7 @@ const Footer: NextPage = () => {

Iowa City, IA 52242

- - Site Admin Login - +

Site Designed and Built by{" "} { - // get the username and password from the credientials - const { username, password } = await loginSchema.parseAsync( - credentials - ); + try { + // get the username and password from the credientials + const { username, password } = await loginSchema.parseAsync( + credentials + ); - // check if username exists in the database - const result = await prisma.user.findFirst({ - where: { username }, - }); - if (!result) return null; + // check if username exists in the database + const result = await prisma.user.findFirst({ + where: { username }, + }); + if (!result) return null; - // check if input password match the hashed password - const isValidPassword = await verify(result.password, password); - if (!isValidPassword) return null; + // check if input password match the hashed password + const isValidPassword = await verify(result.password, password); + if (!isValidPassword) return null; - return { - id: result.id, - name: result.name, - username, - }; + return { + id: result.id, + name: result.name, + username, + }; + } catch { + return null; + } }, }), /** @@ -99,6 +116,15 @@ export const authOptions: NextAuthOptions = { * @see https://next-auth.js.org/providers/github */ ], + jwt: { + maxAge: 2 * 60 * 60, // 2 hours + }, + pages: { + signIn: "/admin/login", + }, + session: { + strategy: "jwt", + }, }; /**