leaky-ships/leaky-ships/pages/api/auth/[...nextauth].ts

63 lines
1.9 KiB
TypeScript

import prisma from "@lib/prisma"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { NextApiHandler } from "next"
import NextAuth, { NextAuthOptions } from "next-auth"
import AzureADProvider from "next-auth/providers/azure-ad"
import EmailProvider from "next-auth/providers/email"
import {
animals,
Config,
NumberDictionary,
uniqueNamesGenerator,
} from "unique-names-generator"
const numberDictionary = NumberDictionary.generate({ min: 0, max: 9999 })
const customConfig: Config = {
dictionaries: [animals, numberDictionary],
separator: " ",
style: "capital",
length: 2,
}
const options: NextAuthOptions = {
providers: [
EmailProvider({
server: process.env.EMAIL_SERVER,
from: process.env.EMAIL_FROM,
}),
AzureADProvider({
clientId: process.env.AZURE_AD_CLIENT_ID ?? "",
clientSecret: process.env.AZURE_AD_CLIENT_SECRET ?? "",
tenantId: process.env.AZURE_AD_TENANT_ID,
}),
],
adapter: PrismaAdapter(prisma),
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
signIn: ({ user, account }) => {
// Custom signIn callback to add username to email provider
if (account && account.provider === "email") {
user.name = uniqueNamesGenerator(customConfig) // Replace with your desired username
}
return true
},
session: ({ session, user }) => {
if (session?.user) {
session.user.id = user.id
}
return session
},
},
pages: {
signIn: "/signin",
signOut: "/signout",
// error: '/auth/error', // Error code passed in query string as ?error=
// verifyRequest: '/auth/verify-request', // (used for check email message)
// newUser: '/auth/new-user' // New users will be directed here on first sign in (leave the property out if not of interest)
},
}
export { options as authOptions }
const authHandler: NextApiHandler = (req, res) => NextAuth(req, res, options)
export default authHandler