2023-02-18 21:32:02 -06:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-02-06 11:59:23 -06:00
|
|
|
import NextAuth from "next-auth/next";
|
|
|
|
import CredentialsProvider from "next-auth/providers/credentials";
|
2023-07-09 13:35:22 -05:00
|
|
|
import { AuthOptions, Session } from "next-auth";
|
2023-02-06 11:59:23 -06:00
|
|
|
import bcrypt from "bcrypt";
|
2023-07-08 05:35:43 -05:00
|
|
|
import EmailProvider from "next-auth/providers/email";
|
2023-07-09 13:35:22 -05:00
|
|
|
import { JWT } from "next-auth/jwt";
|
2023-07-12 13:26:34 -05:00
|
|
|
import { PrismaAdapter } from "@auth/prisma-adapter";
|
|
|
|
import { Adapter } from "next-auth/adapters";
|
|
|
|
import sendVerificationRequest from "@/lib/api/sendVerificationRequest";
|
|
|
|
import { Provider } from "next-auth/providers";
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-12 15:34:05 -05:00
|
|
|
let email;
|
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
const providers: Provider[] = [
|
|
|
|
CredentialsProvider({
|
|
|
|
type: "credentials",
|
|
|
|
credentials: {
|
|
|
|
username: {
|
|
|
|
label: "Username",
|
|
|
|
type: "text",
|
2023-07-08 05:35:43 -05:00
|
|
|
},
|
2023-07-12 13:26:34 -05:00
|
|
|
password: {
|
|
|
|
label: "Password",
|
|
|
|
type: "password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
async authorize(credentials, req) {
|
|
|
|
if (!credentials) return null;
|
2023-07-08 05:35:43 -05:00
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
const findUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
username: credentials.username.toLowerCase(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
email: credentials.username.toLowerCase(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
emailVerified: { not: null },
|
|
|
|
},
|
|
|
|
});
|
2023-07-08 05:35:43 -05:00
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
let passwordMatches: boolean = false;
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
if (findUser?.password) {
|
|
|
|
passwordMatches = bcrypt.compareSync(
|
|
|
|
credentials.password,
|
|
|
|
findUser.password
|
|
|
|
);
|
|
|
|
}
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
if (passwordMatches) {
|
|
|
|
return findUser;
|
|
|
|
} else return null as any;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
if (process.env.EMAIL_SERVER && process.env.EMAIL_FROM)
|
|
|
|
providers.push(
|
|
|
|
EmailProvider({
|
|
|
|
server: process.env.EMAIL_SERVER,
|
|
|
|
from: process.env.EMAIL_FROM,
|
|
|
|
maxAge: 600,
|
|
|
|
sendVerificationRequest(params) {
|
2023-07-12 15:34:05 -05:00
|
|
|
email = params.identifier;
|
2023-07-12 13:26:34 -05:00
|
|
|
sendVerificationRequest(params);
|
2023-02-06 11:59:23 -06:00
|
|
|
},
|
2023-07-12 13:26:34 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
export const authOptions: AuthOptions = {
|
|
|
|
adapter: PrismaAdapter(prisma) as Adapter,
|
|
|
|
session: {
|
|
|
|
strategy: "jwt",
|
|
|
|
},
|
|
|
|
providers,
|
2023-02-06 11:59:23 -06:00
|
|
|
pages: {
|
2023-02-08 15:11:33 -06:00
|
|
|
signIn: "/login",
|
2023-07-12 15:34:05 -05:00
|
|
|
verifyRequest: "/confirmation",
|
2023-02-08 15:11:33 -06:00
|
|
|
},
|
|
|
|
callbacks: {
|
2023-07-09 13:35:22 -05:00
|
|
|
session: async ({ session, token }: { session: Session; token: JWT }) => {
|
|
|
|
session.user.id = parseInt(token.id as string);
|
|
|
|
session.user.username = token.username as string;
|
2023-02-08 15:11:33 -06:00
|
|
|
|
|
|
|
return session;
|
|
|
|
},
|
2023-05-21 04:54:42 -05:00
|
|
|
// Using the `...rest` parameter to be able to narrow down the type based on `trigger`
|
2023-07-09 13:35:22 -05:00
|
|
|
jwt({ token, trigger, session, user }) {
|
|
|
|
if (trigger === "signIn") {
|
|
|
|
token.id = user.id;
|
|
|
|
token.username = (user as any).username;
|
|
|
|
} else if (trigger === "update" && session?.name && session?.username) {
|
2023-05-21 04:54:42 -05:00
|
|
|
// Note, that `session` can be any arbitrary object, remember to validate it!
|
|
|
|
token.name = session.name;
|
2023-07-08 05:35:43 -05:00
|
|
|
token.username = session.username.toLowerCase();
|
2023-07-03 15:43:53 -05:00
|
|
|
token.email = session.email.toLowerCase();
|
2023-05-21 04:54:42 -05:00
|
|
|
}
|
|
|
|
return token;
|
|
|
|
},
|
2023-02-06 11:59:23 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NextAuth(authOptions);
|