Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:WildCodeSchool-2023-09/eating-nam-na…
Browse files Browse the repository at this point in the history
…m into S06_US14
  • Loading branch information
TristanZvunka committed Jan 23, 2024
2 parents 34387ca + d4a8d38 commit a21cc95
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 37 deletions.
11 changes: 6 additions & 5 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ const app = express();

const cors = require("cors");

app.use(cors());
/*
// app.use(cors());

app.use(
cors({
origin: [
process.env.FRONTEND_URL, // keep this one, after checking the value in `backend/.env`
],
credentials: true,
})
);

Expand All @@ -54,9 +55,9 @@ app.use(
// Uncomment one or more of these options depending on the format of the data sent by your client:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.text());
app.use(express.raw());
// app.use(express.urlencoded());
// app.use(express.text());
// app.use(express.raw());

/* ************************************************************************* */

Expand Down
9 changes: 7 additions & 2 deletions backend/src/controllers/authControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ const login = async (req, res, next) => {
// Create a token for open & keep the user session as logged
const token = jwt.sign(
{ username: user.username, is_admin: user.is_admin },
process.env.APP_SECRET
process.env.APP_SECRET,
{ expiresIn: "1h" }
);
// Respond with the Token of the user, in JSON format
res.cookie("token_eating_nam_nam_usr", token).json(token);
res.cookie("token", token, {
httpOnly: true,
maxAge: 3600000, // 1h in ms
});
res.status(200).send(token);
} else {
res.sendStatus(422);
}
Expand Down
1 change: 0 additions & 1 deletion backend/src/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const edit = async (req, res, next) => {
const add = async (req, res, next) => {
// Extract the user data from the request body
const item = req.body;
console.info(item);

const existingUsername = await tables.user.readByUsername(item.username);
const existingEmail = await tables.user.readByEmail(item.email);
Expand Down
19 changes: 11 additions & 8 deletions backend/src/middleware/authMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ const hashPwd = async (req, res, next) => {
};

const verifyToken = async (req, res, next) => {
const token = req.body.cookies;
try {
const { token } = req.cookies;

console.info(req.cookies);

console.info(token);

console.info(token);
if (!token) {
res.status(401).json({ error: "No token founded" });
} else {
try {
if (!token) {
res.status(401).json({ error: "No token founded" });
} else {
const decoded = jwt.verify(token, process.env.APP_SECRET);
const user = await tables.user.read(decoded.id);
console.info({ user });
next();
} catch (err) {
res.status(401).json({ error: "The token is invalid" });
}
} catch (err) {
res.status(401).json({ error: "The token is invalid" });
}
};

Expand Down
2 changes: 0 additions & 2 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ router.post(
UserControllers.add
);

// lala ceci est un test

// Route to get specific items and block the register if they exists
router.get("/username/:username", AuthControllers.readByUsername);
router.get("/email/:email", AuthControllers.readByEmail);
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ReactDOM from "react-dom/client";

import { createBrowserRouter, RouterProvider } from "react-router-dom";

import axios from "axios";

import App from "./App";
import Accueil from "./pages/Accueil";
import Connexion from "./pages/Connexion";
Expand All @@ -24,7 +26,12 @@ const router = createBrowserRouter([
path: "/recipes/:id",
element: <RecipeDetails />,
loader: ({ params }) => {
return fetch(`http://localhost:3310/api/recipes/${params.id}`);
return axios.get(
`${import.meta.env.VITE_BACKEND_URL}/api/recipes/${params.id}`,
{
withCredentials: true,
}
);
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/pages/Connexion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ function TypeOfForm({ checkbox, setCheckbox }) {
username,
password,
};
console.info(formData);

try {
// Appel à l'API pour demander une connexion
const response = await axios
.post(`${import.meta.env.VITE_BACKEND_URL}/api/login`, formData)
.post(`${import.meta.env.VITE_BACKEND_URL}/api/login`, formData, {
withCredentials: true,
})
.catch((err) => console.error(err));

// Redirection vers la page de connexion si la création réussit
Expand Down Expand Up @@ -123,7 +124,6 @@ function TypeOfForm({ checkbox, setCheckbox }) {
// Redirection sur le login en gardant la valeur de username
document.getElementsByTagName("form")[2].email.value = "";
if (!checkbox) setCheckbox(true);
console.info("response =", res);
}
})
.catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/RecipeDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function RecipeDetails() {
return (
<div className="body-content recipe-detail">
<Detail name={detail.name} prep={detail.prep_time} />
<Link to="/keywords">
<Link to="/recipes">
<p>retour</p>
</Link>
</div>
Expand Down
22 changes: 13 additions & 9 deletions frontend/src/pages/RecipeList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ function RecipeList() {
const [filters, setFilters] = useState([]);
const [filtersRecipe, setFiltersRecipe] = useState("");

const getData = () => {
useEffect(() => {
const endpoints = [
"http://localhost:3310/api/recipes",
"http://localhost:3310/api/tags",
];
Promise.all(endpoints.map((endpoint) => axios.get(endpoint))).then(
([{ data: recipe }, { data: tag }]) => {
Promise.all(
endpoints.map((endpoint) =>
axios.get(endpoint, {
withCredentials: true,
})
)
)
.then(([{ data: recipe }, { data: tag }]) => {
setAllRecipe(recipe);
setFilters(tag);
console.info(recipe, tag);
}
);
};

useEffect(() => {
getData();
})
.catch(() => {
window.location.href = "/";
});
}, []);

return (
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/pages/style/RecipeList.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
list-style: none;
background-color: $white-color;
border-radius: 1.5rem;
height: 150px;
height: 200px;
min-width: 300px;
max-width: 350px;
max-width: 400px;
box-shadow: 0px 15px 20px rgba(0, 0, 0, 0.1);

display: flex;
Expand All @@ -42,11 +42,11 @@

.image-recipe-pos {
width: 45%;
height: 150px;
max-width: 150px;
height: 200px;
max-width: 200px;
}
.image-recipe {
height: 150px;
height: 200px;
width: 100%;
object-fit: cover;
border-radius: 1.5rem 0 0 1.5rem;
Expand Down

0 comments on commit a21cc95

Please sign in to comment.