2022-11-11 22:17:44 -05:00
|
|
|
import { PrismaAdapter } from "@next-auth/prisma-adapter"
|
|
|
|
import { NextAuthOptions } from "next-auth"
|
|
|
|
import GitHubProvider from "next-auth/providers/github"
|
2022-11-12 19:06:23 -05:00
|
|
|
import { prisma } from "@lib/server/prisma"
|
2022-11-11 22:17:44 -05:00
|
|
|
import config from "@lib/config"
|
|
|
|
|
|
|
|
const providers: NextAuthOptions["providers"] = [
|
|
|
|
GitHubProvider({
|
|
|
|
clientId: config.GITHUB_CLIENT_ID,
|
|
|
|
clientSecret: config.GITHUB_CLIENT_SECRET
|
2022-11-12 03:58:21 -05:00
|
|
|
})
|
2022-11-11 22:17:44 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
|
|
// see https://github.com/prisma/prisma/issues/16117 / https://github.com/shadcn/taxonomy
|
|
|
|
adapter: PrismaAdapter(prisma as any),
|
|
|
|
session: {
|
|
|
|
strategy: "jwt"
|
|
|
|
},
|
|
|
|
pages: {
|
|
|
|
signIn: "/signin"
|
|
|
|
},
|
|
|
|
providers,
|
|
|
|
callbacks: {
|
|
|
|
async session({ token, session }) {
|
|
|
|
if (token) {
|
|
|
|
session.user.id = token.id
|
|
|
|
session.user.name = token.name
|
|
|
|
session.user.email = token.email
|
|
|
|
session.user.image = token.picture
|
|
|
|
session.user.role = token.role
|
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
},
|
|
|
|
async jwt({ token, user }) {
|
|
|
|
const dbUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
email: token.email
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!dbUser) {
|
2022-11-12 03:58:21 -05:00
|
|
|
// TODO: user should be defined? should we invalidate/signout?
|
2022-11-11 22:17:44 -05:00
|
|
|
if (user) {
|
|
|
|
token.id = user.id
|
2022-11-12 02:59:33 -05:00
|
|
|
token.role = "user"
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: dbUser.id,
|
2022-11-14 21:39:42 -05:00
|
|
|
name: dbUser.displayName,
|
2022-11-11 22:17:44 -05:00
|
|
|
email: dbUser.email,
|
|
|
|
picture: dbUser.image,
|
2022-11-12 04:28:06 -05:00
|
|
|
role: dbUser.role || "user",
|
2022-11-11 22:17:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} as const
|