feat: initial commit - 1000apps SaaS container
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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),
|
||||
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
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user