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
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,54 @@
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
import bcrypt from "bcryptjs"
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
return res.status(405).json({ error: "Método não permitido" })
|
||||
}
|
||||
|
||||
const { username, password } = req.body
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({ error: "Username e senha são obrigatórios" })
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { username }
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return res.status(401).json({ error: "Usuário não encontrado" })
|
||||
}
|
||||
|
||||
if (!user.password) {
|
||||
return res.status(401).json({ error: "Senha não configurada" })
|
||||
}
|
||||
|
||||
const isValid = await bcrypt.compare(password, user.password)
|
||||
|
||||
if (!isValid) {
|
||||
return res.status(401).json({ error: "Senha incorreta" })
|
||||
}
|
||||
|
||||
// Create a JWT token manually for the session
|
||||
const token = require("jsonwebtoken").sign(
|
||||
{ sub: user.id, email: user.email, name: user.name || user.username },
|
||||
process.env.NEXTAUTH_SECRET || "test-secret-2x4=",
|
||||
{ expiresIn: "30d" }
|
||||
)
|
||||
|
||||
return res.status(200).json({
|
||||
ok: true,
|
||||
user: { id: user.id, email: user.email, name: user.name || user.username },
|
||||
token
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Signin error:", error)
|
||||
return res.status(500).json({ error: "Erro interno do servidor" })
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
import bcrypt from "bcryptjs"
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
return res.status(405).json({ error: "Método não permitido" })
|
||||
}
|
||||
|
||||
const { username, password } = req.body
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({ error: "Username e senha são obrigatórios" })
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
return res.status(400).json({ error: "Senha deve ter pelo menos 6 caracteres" })
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if username exists
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { username }
|
||||
})
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(400).json({ error: "Nome de usuário já existe" })
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 12)
|
||||
|
||||
// Create user
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
password: hashedPassword,
|
||||
email: `${username}@1000apps.local`
|
||||
}
|
||||
})
|
||||
|
||||
return res.status(201).json({
|
||||
message: "Usuário criado com sucesso",
|
||||
user: { id: user.id, username: user.username }
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Signup error:", error)
|
||||
return res.status(500).json({ error: "Erro interno do servidor" })
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user