Skip to content
6 changes: 3 additions & 3 deletions PROJETOS-LIONS/Projetos-PEA/Cadastro/Src/Functions/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ const bcrypt = require('bcrypt')
const RegisterUserMGS = require('../Schemas/SchemaRegister')
const { RetornarSucesso, RetornarErro } = require('../Utils/utils')


async function AddUser(req,res) {
try{
const Dados = req.body
const HashSenha = await bcrypt.hash(Dados.Senha, 12)
const NewUser = await RegisterUserMGS.create({

await RegisterUserMGS.create({
...Dados,
Senha: HashSenha
})

NewUser.Senha = undefined

return RetornarSucesso(res, "Ususario Criado com sucesso!!")
} catch(error){
console.error(error)
Expand Down
3 changes: 3 additions & 0 deletions PROJETOS-LIONS/Projetos-PEA/Cadastro/Src/Functions/GetUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function HelloUser (req, res) {
res.json({ message: `Bem-vindo! Seu ID é ${req.user.userId}` });
};
16 changes: 16 additions & 0 deletions PROJETOS-LIONS/Projetos-PEA/Cadastro/Src/Functions/LoginUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SchemaRegister } from "../Schemas/SchemaRegister"
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
export async function loginUser(req, res){
const { email, password } = req.body;

const user = await SchemaRegister.findOne({ email });
if (!user) return res.status(404).json({ error: 'Usuário não encontrado' });

const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) return res.status(401).json({ error: 'Senha incorreta' });

const token = jwt.sign({ userId: user._id }, 'acess token', { expiresIn: '1h' });

res.json({ message: 'Login realizado com sucesso!', token });
}
3 changes: 3 additions & 0 deletions PROJETOS-LIONS/Projetos-PEA/Cadastro/Src/Routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const express = require("express");
const { AddUser } = require("../Functions/AddUser");
const { RemoveUser } = require("../Functions/RemoveUser");
const { loginUser } = require("../Functions/LoginUser");

const route = express.Router();

route.post("/Cadastro", AddUser)
route.delete("/Cadastro/:id", RemoveUser)
route.post("/login", loginUser)
route.get("/profile",authMiddleware, HelloUser )

module.exports = route;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const jwt = require('jsonwebtoken');

export function authMiddleware(req, res, next) {
const token = req.headers.authorization;

if (!token) return res.status(401).json({ error: 'Token não fornecido' });

try {
const decoded = jwt.verify(token,'36c5e560ecb4d0cac76541eb981301959079c85277db27f5da9e1357102f7468' );
req.user = decoded;
next();
} catch {
return res.status(401).json({ error: 'Token inválido' });
}
}
113 changes: 113 additions & 0 deletions PROJETOS-LIONS/Projetos-PEA/Cadastro/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PROJETOS-LIONS/Projetos-PEA/Cadastro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"bcrypt": "^6.0.0",
"dotenv": "^17.2.3",
"express": "^5.1.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.19.1"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Projetos desenvolvidos na Startup [LionsDEV](https://www.instagram.com/lionsdev/)

> Desenvolvimento de Alguns programas usando JavaScript e [NodeJS](https://nodejs.org/en)
> Pasta Principal contendo alguns dos projetos [PROJETOS-LIONS/Projetos-NODEJS](https://github.com/Ort4vi0/LIONSDEV/tree/main/PROJETOS-LIONS/Projetos-NODEJS)
> Pasta Principal contendo alguns dos projetos [PROJETOS-LIONS](https://github.com/Ort4vi0/LIONSDEV/tree/main/PROJETOS-LIONS)


### Professor [Filipe](https://github.com/FilipePaixao) 🎓