UNPKG

@ibernoh/arkanjs

Version:

Backend API generator with RBAC, CLI scaffolding, and built-in structure. Just install and run.

25 lines (19 loc) 781 B
const { UserModel } = require("../../models/auth/userModel"); const jwt = require("jsonwebtoken"); const bcrypt = require("bcrypt"); const dotenv = require("dotenv"); dotenv.config(); const authController = { async login(req, res) { const { email, password } = req.body; const user = await UserModel.findOne({ where: { email } }); if (!user || !user.is_active) return res.status(404).json({ error: "Usuário inválido ou inativo" }); const valid = await bcrypt.compare(password, user.password); if (!valid) return res.status(401).json({ error: "Senha incorreta" }); const token = jwt.sign({ id: user.id, email: user.email }, process.env.JWT_SECRET, { expiresIn: "1h" }); res.json({ token }); } }; module.exports = { authController };