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 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 <span>{sessionData.user.name}</span>;
}
return (
<Link
className="text-sm text-neutral-300 hover:underline"
href="/admin/login"
>
Site Admin Login
</Link>
);
};
const FooterLabeledSection = ({
title,
children,
@ -141,12 +159,7 @@ const Footer: NextPage = () => {
<p className="text-sm">Iowa City, IA 52242</p>
</section>
<section>
<Link
className="text-sm text-neutral-300 hover:underline"
href="/admin/login"
>
Site Admin Login
</Link>
<AdminLogin />
<p className="text-sm italic text-neutral-400">
Site Designed and Built by{" "}
<a

View File

@ -24,18 +24,22 @@ interface SessionUser {
*/
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
username: string;
// ...other properties
// role: UserRole;
} & DefaultSession["user"];
user: SessionUser;
}
// interface User {
// // ...other properties
// // role: UserRole;
// }
interface User {
id: string;
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 = {
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
// session.user.role = user.role; <-- put other properties on the session here
jwt({ token, user }) {
if (user) {
token.id = user.id;
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;
},
@ -67,26 +80,30 @@ export const authOptions: NextAuthOptions = {
password: { label: "Password", type: "password" },
},
async authorize(credentials): Promise<SessionUser | null> {
// 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",
},
};
/**