100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import { NextAuthOptions } from 'next-auth'
|
|
import DiscordProvider from 'next-auth/providers/discord'
|
|
import { connectToDatabase } from './mongodb'
|
|
import { isReservedUsername } from './validations'
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
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,
|
|
}
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async signIn({ user, account }) {
|
|
if (account?.provider === 'discord') {
|
|
const { db } = await connectToDatabase()
|
|
|
|
const existingUser = await db.collection('users').findOne({ discordId: user.id })
|
|
|
|
if (!existingUser) {
|
|
let initialSolBalance = 5 // Give new users 5 SOL
|
|
|
|
let cleanName = user.name?.replace(/\.$/, '') || `User-${user.id.slice(0, 4)}`;
|
|
|
|
if (isReservedUsername(cleanName)) {
|
|
cleanName += Math.floor(Math.random() * 1000).toString();
|
|
}
|
|
|
|
await db.collection('users').insertOne({
|
|
discordId: user.id,
|
|
name: cleanName,
|
|
email: user.email,
|
|
image: user.image,
|
|
balance: initialSolBalance,
|
|
createdAt: new Date(),
|
|
portfolio: [],
|
|
isOnboarding: true, // flag to force username selection
|
|
})
|
|
} else {
|
|
if (user.image) { //? upd avatar
|
|
await db.collection('users').updateOne(
|
|
{ _id: existingUser._id },
|
|
{ $set: { image: user.image } }
|
|
)
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
},
|
|
async session({ session, token }) {
|
|
if (session.user && token.sub) {
|
|
const { db } = await connectToDatabase()
|
|
const dbUser = await db.collection('users').findOne({ discordId: token.sub })
|
|
|
|
if (dbUser) {
|
|
session.user.id = dbUser._id.toString()
|
|
session.user.discordId = dbUser.discordId
|
|
session.user.balance = dbUser.balance
|
|
session.user.verified = dbUser.verified || false
|
|
session.user.isAdmin = dbUser.isAdmin || false
|
|
session.user.isBetaTester = dbUser.isBetaTester || false
|
|
session.user.isBugHunter = dbUser.isBugHunter || false
|
|
session.user.isOnboarding = dbUser.isOnboarding || false
|
|
session.user.name = dbUser.name
|
|
session.user.chatBannedUntil = dbUser.chatBannedUntil
|
|
}
|
|
}
|
|
return session
|
|
},
|
|
async jwt({ token, user, account }) {
|
|
if (account && user) {
|
|
token.sub = user.id
|
|
}
|
|
return token
|
|
},
|
|
},
|
|
pages: {
|
|
signIn: '/login',
|
|
},
|
|
session: {
|
|
strategy: 'jwt',
|
|
},
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
}
|