F1Manager-Calc/pages/api/auth/[...nextauth].js

49 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-04-17 20:50:24 +02:00
import NextAuth from "next-auth"
import DiscordProvider from "next-auth/providers/discord"
import { MongoDBAdapter } from "@next-auth/mongodb-adapter"
2023-04-18 07:40:11 +02:00
import clientPromise from "../../../libs/cloud/mongodb"
2023-04-17 20:50:24 +02:00
export const authOptions = {
adapter: MongoDBAdapter(clientPromise, {
databaseName: process.env.AUTH_DATABASE_NAME,
}),
// Configure one or more authentication providers
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
profile(profile) {
if (profile.avatar === null) {
const defaultAvatarNumber = parseInt(profile.discriminator) % 5
profile.image_url = `https://cdn.discordapp.com/embed/avatars/${defaultAvatarNumber}.png`
} else {
const format = profile.avatar.startsWith("a_") ? "gif" : "png"
profile.image_url = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`
}
return {
id: profile.id,
name: profile.username,
email: profile.email,
image: profile.image_url,
discord_profile: profile,
}
},
}),
// ...add more providers here
],
callbacks: {
2023-04-18 05:21:16 +02:00
async session({ session, token, user }) {
// Send properties to the client, like an access_token and user id from a provider.
session.userId = user.id
session.user = user
return session
},
2023-04-17 20:50:24 +02:00
async signIn({ user, account, profile, email, credentials }) {
if(user) {
account.discord_profile = profile;
}
return true;
},
}
}
export default NextAuth(authOptions)