2023-04-20 04:20:00 -06:00
|
|
|
import { type GetServerSidePropsContext } from "next";
|
|
|
|
import {
|
|
|
|
getServerSession,
|
|
|
|
type NextAuthOptions,
|
|
|
|
type DefaultSession,
|
|
|
|
} from "next-auth";
|
|
|
|
import GithubProvider from "next-auth/providers/github";
|
|
|
|
import GoogleProvider from "next-auth/providers/google";
|
2023-06-12 18:53:55 -06:00
|
|
|
import { PrismaAdapter } from "@auth/prisma-adapter";
|
2023-04-20 04:20:00 -06:00
|
|
|
import { env } from "~/env.mjs";
|
|
|
|
import { prisma } from "~/server/db";
|
|
|
|
import type { Role } from "~/utils/types";
|
2023-06-07 22:21:02 -06:00
|
|
|
import { Resend } from "resend";
|
|
|
|
import { Welcome } from "../components/templates/Welcome";
|
2023-07-03 15:37:14 -06:00
|
|
|
import { invalidateCache } from "./redis";
|
2023-05-21 20:02:51 -06:00
|
|
|
|
2023-06-07 22:21:02 -06:00
|
|
|
const resend = new Resend(process.env.RESEND_API_KEY);
|
2023-04-20 04:20:00 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
|
|
|
|
* object and keep type safety.
|
|
|
|
*
|
|
|
|
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
|
|
|
|
*/
|
|
|
|
declare module "next-auth" {
|
|
|
|
interface Session extends DefaultSession {
|
|
|
|
user: {
|
|
|
|
id: string;
|
|
|
|
role: Role;
|
|
|
|
} & DefaultSession["user"];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface User {
|
|
|
|
role: Role;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
|
|
|
|
*
|
|
|
|
* @see https://next-auth.js.org/configuration/options
|
|
|
|
*/
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
|
|
callbacks: {
|
|
|
|
session({ session, user }) {
|
|
|
|
if (session.user) {
|
|
|
|
session.user.id = user.id;
|
|
|
|
session.user.role = user.role;
|
|
|
|
}
|
|
|
|
return session;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
events: {
|
|
|
|
async createUser({ user }) {
|
|
|
|
if (user && user.name && user.email) {
|
2023-06-07 22:21:02 -06:00
|
|
|
await resend.sendEmail({
|
|
|
|
from: "no-reply@sprintpadawan.dev",
|
|
|
|
to: user.email,
|
|
|
|
subject: "🎉 Welcome to Sprint Padawan! 🎉",
|
2023-06-07 22:31:43 -06:00
|
|
|
//@ts-ignore: IDK why this doesn't work...
|
|
|
|
|
2023-06-07 22:21:02 -06:00
|
|
|
react: Welcome({ name: user.name }),
|
|
|
|
});
|
2023-07-03 15:37:14 -06:00
|
|
|
await invalidateCache(`kv_userlist_admin`);
|
|
|
|
await invalidateCache(`kv_usercount_admin`);
|
2023-04-20 04:20:00 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
async signIn({}) {
|
2023-07-03 15:37:14 -06:00
|
|
|
await invalidateCache(`kv_userlist_admin`);
|
2023-04-20 04:20:00 -06:00
|
|
|
},
|
2023-05-30 22:16:33 -06:00
|
|
|
async signOut() {
|
2023-07-03 15:37:14 -06:00
|
|
|
await invalidateCache(`kv_userlist_admin`);
|
2023-04-20 04:20:00 -06:00
|
|
|
},
|
|
|
|
},
|
2023-06-12 18:53:55 -06:00
|
|
|
// @ts-ignore This adapter should work...
|
2023-04-20 04:20:00 -06:00
|
|
|
adapter: PrismaAdapter(prisma),
|
|
|
|
providers: [
|
|
|
|
GithubProvider({
|
|
|
|
clientId: env.GITHUB_CLIENT_ID,
|
|
|
|
clientSecret: env.GITHUB_CLIENT_SECRET,
|
|
|
|
}),
|
|
|
|
GoogleProvider({
|
|
|
|
clientId: env.GOOGLE_CLIENT_ID,
|
|
|
|
clientSecret: env.GOOGLE_CLIENT_SECRET,
|
|
|
|
}),
|
|
|
|
/**
|
|
|
|
* ...add more providers here.
|
|
|
|
*
|
|
|
|
* Most other providers require a bit more work than the Discord provider. For example, the
|
|
|
|
* GitHub provider requires you to add the `refresh_token_expires_in` field to the Account
|
|
|
|
* model. Refer to the NextAuth.js docs for the provider you want to use. Example:
|
|
|
|
*
|
|
|
|
* @see https://next-auth.js.org/providers/github
|
|
|
|
*/
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
|
|
|
|
*
|
|
|
|
* @see https://next-auth.js.org/configuration/nextjs
|
|
|
|
*/
|
|
|
|
export const getServerAuthSession = (ctx: {
|
|
|
|
req: GetServerSidePropsContext["req"];
|
|
|
|
res: GetServerSidePropsContext["res"];
|
|
|
|
}) => {
|
|
|
|
return getServerSession(ctx.req, ctx.res, authOptions);
|
|
|
|
};
|