Skip to content
Snippets Groups Projects
auth.ts 3.33 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
import { env } from "@/env.mjs"
import { db } from "@/lib/db"
import { PrismaAdapter } from "@auth/prisma-adapter"
Yusuf Akgül's avatar
Yusuf Akgül committed
import { compare } from "bcrypt"
import { NextAuthOptions } from "next-auth"
Yusuf Akgül's avatar
Yusuf Akgül committed
import { Adapter } from "next-auth/adapters"
Yusuf Akgül's avatar
Yusuf Akgül committed
import CredentialsProvider from 'next-auth/providers/credentials'
Yusuf Akgül's avatar
Yusuf Akgül committed
import GitHubProvider from "next-auth/providers/github"
Yusuf Akgül's avatar
Yusuf Akgül committed

export const authOptions: NextAuthOptions = {
Yusuf Akgül's avatar
Yusuf Akgül committed
    adapter: PrismaAdapter(db as any) as Adapter,
Yusuf Akgül's avatar
Yusuf Akgül committed
    session: {
        strategy: 'jwt'
    },
Yusuf Akgül's avatar
Yusuf Akgül committed
    pages: {
        signIn: "/login",
    },
Yusuf Akgül's avatar
Yusuf Akgül committed
    providers: [
Yusuf Akgül's avatar
Yusuf Akgül committed
        GitHubProvider({
            clientId: env.GITHUB_CLIENT_ID as string,
            clientSecret: env.GITHUB_CLIENT_SECRET as string,
        }),

Yusuf Akgül's avatar
Yusuf Akgül committed
        CredentialsProvider({
Yusuf Akgül's avatar
Yusuf Akgül committed
            name: 'Login',
Yusuf Akgül's avatar
Yusuf Akgül committed
            credentials: {
Yusuf Akgül's avatar
Yusuf Akgül committed
                username: { label: 'Username', type: 'text' },
                email: { label: 'Email', type: 'email', placeholder: 'hello@example.com' },
Yusuf Akgül's avatar
Yusuf Akgül committed
                password: { label: 'Password', type: 'password' }
            },
            async authorize(credentials) {
Yusuf Akgül's avatar
Yusuf Akgül committed
                if (!credentials?.username || !credentials.email || !credentials.password) {
Yusuf Akgül's avatar
Yusuf Akgül committed
                    return null
                }

Yusuf Akgül's avatar
Yusuf Akgül committed
                let isUnique = false;
                while (!isUnique) {
                    const existingUserName = await db.user.findUnique({
                        where: {
                            username: credentials.username
                        }
                    })

                    if (existingUserName) {
                        credentials.username = `${credentials.username}${Math.floor(Math.random() * 1000)}`
                    } else {
                        isUnique = true;
                    }
                }

                const user = await db.user.findUnique({
Yusuf Akgül's avatar
Yusuf Akgül committed
                    where: {
                        email: credentials.email
                    }
                })

                if (!user) {
                    return null
                }

                const isPasswordValid = await compare(
                    credentials.password,
                    user.password
                )

                if (!isPasswordValid) {
                    return null
                }

                return {
Yusuf Akgül's avatar
Yusuf Akgül committed
                    id: user.id,
                    username: user.username,
Yusuf Akgül's avatar
Yusuf Akgül committed
                    email: user.email,
                    name: user.name,
                }
            }
        })
    ],
Yusuf Akgül's avatar
Yusuf Akgül committed
    secret: env.NEXTAUTH_SECRET,
Yusuf Akgül's avatar
Yusuf Akgül committed
    callbacks: {
Yusuf Akgül's avatar
Yusuf Akgül committed
        async session({ token, session }) {
            if (token) {
                session.user.id = token.id + ''
                session.user.name = token.name
                session.user.email = token.email
                session.user.image = token.picture
Yusuf Akgül's avatar
Yusuf Akgül committed
            }
Yusuf Akgül's avatar
Yusuf Akgül committed

            return session
Yusuf Akgül's avatar
Yusuf Akgül committed
        },
Yusuf Akgül's avatar
Yusuf Akgül committed
        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's avatar
Yusuf Akgül committed
                }
Yusuf Akgül's avatar
Yusuf Akgül committed
                return token
            }

            return {
                id: dbUser.id,
                name: dbUser.name,
                email: dbUser.email,
                picture: dbUser.image,