Files
1000apps/pages/api/auth/[...nextauth].js
T

54 lines
1.5 KiB
JavaScript

import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"
import bcrypt from "bcryptjs"
const prisma = new PrismaClient()
export default NextAuth({
adapter: PrismaAdapter(prisma),
trustHost: true,
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text", placeholder: "seu_usuario" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
if (!credentials?.username || !credentials?.password) return null
const user = await prisma.user.findUnique({
where: { username: credentials.username }
})
if (user && user.password) {
const isValid = await bcrypt.compare(credentials.password, user.password)
if (isValid) {
return {
id: user.id,
email: user.email,
name: user.name || user.username,
username: user.username
}
}
}
return null
}
})
],
secret: process.env.NEXTAUTH_SECRET,
session: { strategy: "jwt" },
pages: {
signIn: "/signin",
},
callbacks: {
async session({ session, token, user }) {
if (session.user) {
session.user.id = token.sub
}
return session
}
}
})