users can sign in as the admin account

This commit is contained in:
Brandon Egger 2023-05-18 20:32:24 -05:00
parent 24b342a06f
commit 7188d603c2
2 changed files with 76 additions and 37 deletions

View File

@ -1,6 +1,7 @@
import { type NextPage } from "next/types"; import { type NextPage } from "next/types";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
import { useSession } from "next-auth/react";
interface QuickLink { interface QuickLink {
label: string; label: string;
@ -93,6 +94,23 @@ const ContactInfo = ({ name, title, email, phone }: ContactInfo) => {
); );
}; };
const AdminLogin = () => {
const { data: sessionData } = useSession();
if (sessionData?.user) {
return <span>{sessionData.user.name}</span>;
}
return (
<Link
className="text-sm text-neutral-300 hover:underline"
href="/admin/login"
>
Site Admin Login
</Link>
);
};
const FooterLabeledSection = ({ const FooterLabeledSection = ({
title, title,
children, children,
@ -141,12 +159,7 @@ const Footer: NextPage = () => {
<p className="text-sm">Iowa City, IA 52242</p> <p className="text-sm">Iowa City, IA 52242</p>
</section> </section>
<section> <section>
<Link <AdminLogin />
className="text-sm text-neutral-300 hover:underline"
href="/admin/login"
>
Site Admin Login
</Link>
<p className="text-sm italic text-neutral-400"> <p className="text-sm italic text-neutral-400">
Site Designed and Built by{" "} Site Designed and Built by{" "}
<a <a

View File

@ -24,18 +24,22 @@ interface SessionUser {
*/ */
declare module "next-auth" { declare module "next-auth" {
interface Session extends DefaultSession { interface Session extends DefaultSession {
user: { user: SessionUser;
id: string;
username: string;
// ...other properties
// role: UserRole;
} & DefaultSession["user"];
} }
// interface User { interface User {
// // ...other properties id: string;
// // role: UserRole; name: string;
// } username: string;
}
}
declare module "next-auth/jwt" {
interface JWT {
id: string;
name: string;
username: string;
}
} }
/** /**
@ -45,10 +49,19 @@ declare module "next-auth" {
*/ */
export const authOptions: NextAuthOptions = { export const authOptions: NextAuthOptions = {
callbacks: { callbacks: {
session({ session, user }) { jwt({ token, user }) {
if (session.user) { if (user) {
session.user.id = user.id; token.id = user.id;
// session.user.role = user.role; <-- put other properties on the session here token.username = user.username;
token.name = user.name;
}
return token;
},
session({ session, token }) {
if (token) {
session.user.id = token.id;
session.user.username = token.username;
session.user.name = token.name;
} }
return session; return session;
}, },
@ -67,26 +80,30 @@ export const authOptions: NextAuthOptions = {
password: { label: "Password", type: "password" }, password: { label: "Password", type: "password" },
}, },
async authorize(credentials): Promise<SessionUser | null> { async authorize(credentials): Promise<SessionUser | null> {
// get the username and password from the credientials try {
const { username, password } = await loginSchema.parseAsync( // get the username and password from the credientials
credentials const { username, password } = await loginSchema.parseAsync(
); credentials
);
// check if username exists in the database // check if username exists in the database
const result = await prisma.user.findFirst({ const result = await prisma.user.findFirst({
where: { username }, where: { username },
}); });
if (!result) return null; if (!result) return null;
// check if input password match the hashed password // check if input password match the hashed password
const isValidPassword = await verify(result.password, password); const isValidPassword = await verify(result.password, password);
if (!isValidPassword) return null; if (!isValidPassword) return null;
return { return {
id: result.id, id: result.id,
name: result.name, name: result.name,
username, username,
}; };
} catch {
return null;
}
}, },
}), }),
/** /**
@ -99,6 +116,15 @@ export const authOptions: NextAuthOptions = {
* @see https://next-auth.js.org/providers/github * @see https://next-auth.js.org/providers/github
*/ */
], ],
jwt: {
maxAge: 2 * 60 * 60, // 2 hours
},
pages: {
signIn: "/admin/login",
},
session: {
strategy: "jwt",
},
}; };
/** /**