Skip to content

Commit

Permalink
Merge pull request #58 from mdsreq-fga-unb/feature/frontRefactor
Browse files Browse the repository at this point in the history
Feature/front refactor
  • Loading branch information
xGabrielCv authored Sep 5, 2024
2 parents 06b1374 + 24000a8 commit 7af8f48
Show file tree
Hide file tree
Showing 38 changed files with 1,021 additions and 866 deletions.
1 change: 1 addition & 0 deletions riso_backend/.env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MONGODB_URL=
SOFTWARE_URL=
DB_NAME=
SECRET_JWT=
94 changes: 94 additions & 0 deletions riso_backend/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 riso_backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.8.0",
"mongoose": "^8.5.2"
},
Expand Down
1 change: 1 addition & 0 deletions riso_backend/src/controllers/unit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const findByCodeUnit = async (req, res) => {

const createUnit = async (req, res) => {
try{

const {
name,
numberOfficials,
Expand Down
8 changes: 6 additions & 2 deletions riso_backend/src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ const createUser = async (req, res) => {
if(!user){
return res.status(403).send({ message: 'Error creating User' });
}

const token = userService.generateToken(user.id);

return res.status(201).send({message: 'User created sucefully', user });
return res.status(201).send({message: 'User created sucefully', token });
}catch(err){
if (err.code === 11000) { // Código de erro para violação de chave única em MongoDB/Mongoose
return res.status(400).send({ message: 'Email already in use', substatus: 2 });
Expand All @@ -81,7 +83,9 @@ const loginUser = async (req, res) => {
return res.status(400).send({ message: 'Password or email is invalid', substatus: 2});
}

return res.status(200).send({ message: 'User was found it', user });
const token = userService.generateToken(user.id);

return res.status(200).send({ message: 'User was found it', token });
}catch(err) {
return res.status(500).send({ message: err.message });
}
Expand Down
36 changes: 36 additions & 0 deletions riso_backend/src/middleware/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import dotenv from 'dotenv';
import jwt from 'jsonwebtoken';
import userService from '../services/user.service.js';

dotenv.config();

export const authMiddleware = (req, res, next) => {
try {
const { authorization } = req.headers;
console.log(authorization);

if (!authorization) return res.status(401).send({message: 'User Unauthorized'});

const parts = authorization.split(" ");

if (parts.length !== 2) return res.status(401).send({message: 'User Unauthorized'});

const [schema, token] = parts;

if (schema !== "Bearer") return res.status(401).send({message: 'User Unauthorized'});

jwt.verify(token, process.env.SECRET_JWT, async (error, decoded) => {
if(error) return res.status(401).send({message: 'Token Invalid'});

const user = await userService.findOneService({ _id: decoded._id});

if (!user || !user.id) return res.status(401).send({message: "Invalid User!"});

req.userID = decoded._id;

return next();
});
} catch(err) {
return res.status(500).send({ message: err.message });
}
}
2 changes: 1 addition & 1 deletion riso_backend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from "express";
import cors from 'cors';
import userRouter from "./user.route.js";
import unitRouter from "./unit.route.js";
import unitUserRouter from "./unitUser.js";
import unitUserRouter from "./unitUser.route.js";

const router = Router();

Expand Down
11 changes: 6 additions & 5 deletions riso_backend/src/routes/unit.route.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import express from 'express';
import unitController from '../controllers/unit.controller.js';
import { authMiddleware } from '../middleware/auth.middleware.js';

const unitRouter = express.Router()

unitRouter.get('/findAllUnit', unitController.findAllUnit);
unitRouter.get('/findByCode/:code', unitController.findByCodeUnit);
unitRouter.post('/createUnit',unitController.createUnit);
unitRouter.patch('/updateUnit/:id', unitController.updateUnit);
unitRouter.delete('/deleteUnit/:id',unitController.deleteUnit);
unitRouter.get('/findAllUnit', authMiddleware, unitController.findAllUnit);
unitRouter.get('/findByCode/:code', authMiddleware, unitController.findByCodeUnit);
unitRouter.post('/createUnit', authMiddleware,unitController.createUnit);
unitRouter.patch('/updateUnit/:id', authMiddleware, unitController.updateUnit);
unitRouter.delete('/deleteUnit/:id', authMiddleware, unitController.deleteUnit);

export default unitRouter;
File renamed without changes.
8 changes: 5 additions & 3 deletions riso_backend/src/routes/user.route.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import express from 'express';
import { authMiddleware } from '../middleware/auth.middleware.js';

const userRouter = express.Router();

import userController from '../controllers/user.controller.js';

userRouter.get('/findAllUser', userController.findAllUser);
userRouter.get('/findAllUser', authMiddleware, userController.findAllUser);
userRouter.post('/createUser', userController.createUser);
userRouter.post('/loginUser', userController.loginUser);
userRouter.patch('/updateUser/:id', userController.updateUser);
userRouter.delete("/deleteUser/:id", userController.deleteUser);
userRouter.patch('/updateUser/:id', authMiddleware, userController.updateUser);
userRouter.delete("/deleteUser/:id", authMiddleware, userController.deleteUser);

export default userRouter;
4 changes: 3 additions & 1 deletion riso_backend/src/services/user.service.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import User from '../models/User.js';

import jwt from 'jsonwebtoken';

const findAllService = () => User.find();
const createService = (body) => User.create(body);
const findOneService = (body) => User.findOne(body);
const updateService = (params, body) => User.findOneAndUpdate(params, body, {new: true});
const deleteService = (params) => User.findOneAndDelete(params)
const generateToken = (id) => jwt.sign({_id: id}, process.env.SECRET_JWT, {expiresIn: 86400});

export default {
createService,
findOneService,
findAllService,
updateService,
deleteService,
generateToken,
};
19 changes: 19 additions & 0 deletions riso_frontend/package-lock.json

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

3 changes: 3 additions & 0 deletions riso_frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.7.3",
"js-cookie": "^3.0.5",
"prop-types": "^15.8.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.26.0",
"react-scripts": "5.0.1",
"styled-components": "^6.1.12",
Expand Down
4 changes: 4 additions & 0 deletions riso_frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Merriweather+Sans:ital,wght@0,300..800;1,300..800&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<title>React App</title>
</head>
<body>
Expand Down
33 changes: 25 additions & 8 deletions riso_frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
// IMPORTAÇÕES DE PACOTES
import { BrowserRouter as Router, Route, Routes} from 'react-router-dom';
import { createGlobalStyle } from 'styled-components';

// IMPORTAÇÕES DE PÁGINAS:
import Autentication from './pages/Autentication';
import RegisterPage from './pages/RegisterPage';
import LoginPage from './pages/LoginPage';
import HomePage from './pages/HomePage';
import RegisterUnit from './pages/CadastroEmpresa';

export const GlobalStyle = createGlobalStyle`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #ecf0f1;
}
`;

function App() {
return (
<Router>
<Routes>
<Route path='/' element={<Autentication />} />
<Route path='/homePage' element={<HomePage />} />
<Route path='/registerUnit' element={<RegisterUnit/>}/>
</Routes>
</Router>
<GlobalStyle />
<Routes>
<Route path='/' element={<RegisterPage />} />
<Route path='/Login' element={<LoginPage />} />
<Route path='/HomePage' element={<HomePage />} />
</Routes>
</Router>
);
}

Expand Down
Loading

0 comments on commit 7af8f48

Please sign in to comment.