import { prisma } from '../lib/prisma'; import bcrypt from 'bcrypt'; import { LoginInput } from '../schemas/auth.schema'; export async function loginService({ email, senha }: LoginInput) { const user = await prisma.usuario.findUnique({ where: { email }, }); if (!user || !user.ativo) { throw new Error('Credenciais inválidas ou usuário inativo'); } const isPasswordValid = await bcrypt.compare(senha, user.senha); if (!isPasswordValid) { throw new Error('Credenciais inválidas ou usuário inativo'); } return { id: user.id, nome: user.nome, email: user.email, role: user.role, }; } export async function changePassword(userId: string, senhaAtual: string, novaSenha: string) { const user = await prisma.usuario.findUnique({ where: { id: userId } }); if (!user) throw new Error('Usuário não encontrado'); const isPasswordValid = await bcrypt.compare(senhaAtual, user.senha); if (!isPasswordValid) throw new Error('Senha atual incorreta'); const passwordHash = await bcrypt.hash(novaSenha, 10); await prisma.usuario.update({ where: { id: userId }, data: { senha: passwordHash }, }); }