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-07-15 21:15:43 -05:00
|
|
|
import checkSubscription from "@/lib/api/checkSubscription";
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-15 21:15:43 -05: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",
|
2023-07-15 21:15:43 -05:00
|
|
|
credentials: {},
|
2023-07-12 13:26:34 -05:00
|
|
|
async authorize(credentials, req) {
|
|
|
|
if (!credentials) return null;
|
2023-07-08 05:35:43 -05:00
|
|
|
|
2023-07-15 21:15: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({
|
2023-07-15 21:15:43 -05:00
|
|
|
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) {
|
2023-07-15 21:15:43 -05:00
|
|
|
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) {
|
|
|
|
return findUser;
|
|
|
|
} else return null as any;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-07-15 21:15:43 -05:00
|
|
|
if (emailEnabled)
|
2023-07-12 13:26:34 -05:00
|
|
|
providers.push(
|
|
|
|
EmailProvider({
|
|
|
|
server: process.env.EMAIL_SERVER,
|
|
|
|
from: process.env.EMAIL_FROM,
|
2023-07-15 21:15:43 -05:00
|
|
|
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",
|
2023-07-15 21:15:43 -05:00
|
|
|
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: {
|
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-07-15 21:15:43 -05:00
|
|
|
session.user.isSubscriber = token.isSubscriber as boolean;
|
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-15 21:15:43 -05:00
|
|
|
async jwt({ token, trigger, session, 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;
|
2023-07-15 21:15:43 -05:00
|
|
|
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-07-15 21:15:43 -05:00
|
|
|
const subscription = await checkSubscription(
|
|
|
|
STRIPE_SECRET_KEY,
|
2023-08-21 15:11:13 -05:00
|
|
|
token.email as string
|
2023-07-15 21:15:43 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
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") {
|
|
|
|
token.id = user.id;
|
|
|
|
token.username = (user as any).username;
|
2023-07-19 15:39:59 -05:00
|
|
|
} else if (trigger === "update" && token.id) {
|
|
|
|
console.log(token);
|
|
|
|
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: token.id as number,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
token.name = user.name;
|
|
|
|
token.username = user.username?.toLowerCase();
|
|
|
|
token.email = user.email?.toLowerCase();
|
|
|
|
}
|
2023-05-21 04:54:42 -05:00
|
|
|
}
|
|
|
|
return token;
|
|
|
|
},
|
2023-02-06 11:59:23 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NextAuth(authOptions);
|