Newer
Older
import { env } from "@/env.mjs"
import { db } from "@/lib/db"
import { PrismaAdapter } from "@auth/prisma-adapter"
import { compare } from "bcrypt"
import { NextAuthOptions } from "next-auth"
import CredentialsProvider from 'next-auth/providers/credentials'
import { normalize } from "normalize-diacritics"
import { sendVerificationEmail } from "./validations/sendVerificationEmail"
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
usernameOrEmail: { label: 'Username or Email', type: 'text' },
password: { label: 'Password', type: 'password' }
},
async authorize(credentials) {
if (!credentials?.usernameOrEmail || !credentials?.password) {
OR: [{
username: credentials.usernameOrEmail.toLowerCase()
},
{
email: credentials.usernameOrEmail.toLowerCase()
}],

Yusuf Akgül
committed
})
throw new Error('user not found')
}
const isPasswordValid = await compare(
credentials.password,
user.password
)
throw new Error('invalid password')
session.user.username = token.username
session.user.email = token.email
session.user.image = token.picture
async jwt({ token, user }) {
const dbUser = await db.user.findFirst({
where: {
email: token.email,
},
})
if (!dbUser) {
if (user) {
token.id = user?.id

Yusuf Akgül
committed
let username = await normalize(dbUser.name?.toLowerCase().replace(/\s/g, ''))
const email = dbUser.email?.toLowerCase()

Yusuf Akgül
committed
let isUnique = false
while (!isUnique) {
const existingUserName = await db.user.findFirst({
where: {
username,
NOT: { email },
},

Yusuf Akgül
committed
})

Yusuf Akgül
committed
username = `${username}${Math.floor(Math.random() * 1000)}`

Yusuf Akgül
committed
isUnique = true

Yusuf Akgül
committed
dbUser.username = username
await db.user.update({
where: { email },
data: { username },

Yusuf Akgül
committed
})