import NextAuth from "next-auth"
import Discord from "next-auth/providers/discord"

export const { handlers, signIn, signOut, auth } = NextAuth({
    providers: [
        Discord({
            authorization: "https://discord.com/api/oauth2/authorize?scope=identify+guilds",
        }),
    ],
    callbacks: {
        async jwt({ token, account, profile }) {
            if (account) {
                token.accessToken = account.access_token
                token.discordId = account.providerAccountId
            }
            if (profile) {
                token.discordId = profile.id
            }
            return token
        },
        async session({ session, token }) {
            // @ts-expect-error - accessToken is not strictly typed in session yet
            session.accessToken = token.accessToken
            if (session.user && token.discordId) {
                session.user.id = token.discordId as string
            }
            return session
        },
    },
})
