el.xwx.moe/pages/api/v1/auth/[...nextauth].ts

148 lines
3.9 KiB
TypeScript
Raw Normal View History

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-11-02 00:52:49 -05:00
import { AuthOptions, Session, User } 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-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";
import checkSubscription from "@/lib/api/checkSubscription";
2023-02-06 11:59:23 -06:00
const emailEnabled =
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
2023-07-12 15:34:05 -05:00
2023-07-12 13:26:34 -05:00
const providers: Provider[] = [
CredentialsProvider({
type: "credentials",
credentials: {},
2023-07-12 13:26:34 -05:00
async authorize(credentials, req) {
2023-11-02 13:59:31 -05:00
console.log("User logged in attempt...");
2023-07-12 13:26:34 -05:00
if (!credentials) return null;
2023-07-08 05:35:43 -05:00
const { username, password } = credentials as {
username: string;
password: string;
};
2023-07-12 13:26:34 -05:00
const findUser = await prisma.user.findFirst({
where: emailEnabled
? {
OR: [
{
username: username.toLowerCase(),
},
{
email: username?.toLowerCase(),
},
],
emailVerified: { not: null },
}
: {
username: username.toLowerCase(),
2023-07-12 13:26:34 -05:00
},
});
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(password, findUser.password);
2023-07-12 13:26:34 -05:00
}
2023-02-06 11:59:23 -06:00
2023-07-12 13:26:34 -05:00
if (passwordMatches) {
2023-11-02 13:59:31 -05:00
return { id: findUser?.id };
2023-07-12 13:26:34 -05:00
} else return null as any;
},
}),
];
2023-02-06 11:59:23 -06:00
if (emailEnabled)
2023-07-12 13:26:34 -05:00
providers.push(
EmailProvider({
server: process.env.EMAIL_SERVER,
from: process.env.EMAIL_FROM,
maxAge: 1200,
2023-07-12 13:26:34 -05:00
sendVerificationRequest(params) {
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",
maxAge: 30 * 24 * 60 * 60, // 30 days
2023-07-12 13:26:34 -05:00
},
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: {
async jwt({ token, trigger, user }) {
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
2023-07-19 23:46:16 -05:00
const NEXT_PUBLIC_TRIAL_PERIOD_DAYS =
process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS;
const secondsInTwoWeeks = NEXT_PUBLIC_TRIAL_PERIOD_DAYS
? Number(NEXT_PUBLIC_TRIAL_PERIOD_DAYS) * 86400
2023-07-16 10:51:24 -05:00
: 1209600;
const subscriptionIsTimesUp =
token.subscriptionCanceledAt &&
new Date() >
new Date(
((token.subscriptionCanceledAt as number) + secondsInTwoWeeks) *
1000
);
2023-07-16 10:51:24 -05:00
if (
STRIPE_SECRET_KEY &&
(trigger || subscriptionIsTimesUp || !token.isSubscriber)
) {
2023-11-02 13:59:31 -05:00
const user = await prisma.user.findUnique({
where: {
id: Number(token.sub),
},
});
const subscription = await checkSubscription(
STRIPE_SECRET_KEY,
2023-11-02 13:59:31 -05:00
user?.email as string
);
if (subscription.subscriptionCanceledAt) {
token.subscriptionCanceledAt = subscription.subscriptionCanceledAt;
} else token.subscriptionCanceledAt = undefined;
token.isSubscriber = subscription.isSubscriber;
}
2023-07-09 13:35:22 -05:00
if (trigger === "signIn") {
2023-11-02 00:52:49 -05:00
token.id = user.id as number;
2023-07-19 15:39:59 -05:00
} else if (trigger === "update" && token.id) {
const user = await prisma.user.findUnique({
where: {
id: token.id as number,
},
});
2023-11-02 13:59:31 -05:00
if (user?.name) {
2023-07-19 15:39:59 -05:00
token.name = user.name;
}
2023-05-21 04:54:42 -05:00
}
2023-11-02 13:59:31 -05:00
2023-05-21 04:54:42 -05:00
return token;
},
2023-11-02 00:52:49 -05:00
async session({ session, token }) {
session.user.id = token.id;
session.user.isSubscriber = token.isSubscriber;
return session;
},
2023-02-06 11:59:23 -06:00
},
};
export default NextAuth(authOptions);