Skip to content

Commit

Permalink
Merge pull request #56 from WildCodeSchool/making-better-database
Browse files Browse the repository at this point in the history
Making better database
  • Loading branch information
mahdimcheik authored Mar 6, 2024
2 parents d89f823 + 20ebe1c commit 707cd92
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const corsOptions = {
app.use(cors(corsOptions));
/*
const cors = require("cors");
app.use(
cors({
origin: [
Expand Down
7 changes: 5 additions & 2 deletions backend/src/controllers/uploadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { google } = require("googleapis");
const { OAuth2Client } = require("google-auth-library");
const fs = require("fs");
const VideoManager = require("../models/videoManager");
const youtubeManager = require("../models/youtubeManager");
// const path = require("path");
require("dotenv").config();

Expand Down Expand Up @@ -72,15 +73,17 @@ async function uploadVideo(req, res) {
media: {
body: fs.createReadStream(
`public/${req.relativePath ?? "uploads/video.mp4"}`
), // Adjust the video file path
),
},
});
const video = {
title: resUpload.data.snippet.title,
ytId: resUpload.data.id,
playlistId: details.playlistId,
playlistTitle: details.playlistTitle ?? "playlist",
duration: "10:00",
duration: youtubeManager.convertToTimeFormat(
resUpload.data.contentDetails.duration
), // "10:00"
publishDate:
resUpload.data.snippet.publishedAt.split("T")[0] ?? "1986-04-21",
description:
Expand Down
2 changes: 2 additions & 0 deletions backend/src/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ async function add(req, res) {
async function check(req, res) {
try {
const user = req.body;
// const { email, password } = req.query;
const userdb = await userManager.read(user.email, user.password);
// const userdb = await userManager.read(email, password);
if (!userdb) {
res.status(404).json({
user: null,
Expand Down
7 changes: 4 additions & 3 deletions backend/src/middlewares/userMiddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ async function verifyAdminToken(req, res, next) {
if (userInfo.isAdmin) {
return next();
}
return res
.status(401)
.json({ message: "Tu n'es pas Admin mon pote", success: false });
return res.status(401).json({
message: "vous n'avez pas les droits nécessaires",
success: false,
});
} catch (err) {
return res.status(401).json({ message: err.message, success: false });
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/userManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const database = require("../../database/client");
const activationManager = require("./activationManager");

class UserManager {
static async Hashing(password) {
static async hashing(password) {
const res = await bcrypt.hash(password, 3);
return res;
}
Expand All @@ -15,7 +15,7 @@ class UserManager {
}

static async create(user) {
const hashedPassword = await this.Hashing(user.password);
const hashedPassword = await this.hashing(user.password);
try {
const randomCode = uuid();
const [result] = await database.query(
Expand Down
10 changes: 7 additions & 3 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ const router = express.Router();
const userControllers = require("./controllers/userControllers");

// Route to get a list of items
router.get("/users", userControllers.browse);
router.get("/users", userMiddle.verifyAdminToken, userControllers.browse);

router.get("/users/:email", userControllers.browseByEmail);
router.get(
"/users/:email",
userMiddle.verifyAdminToken,
userControllers.browseByEmail
);

// Route to get a specific item by ID
// router.get("/users", userControllers.read);
Expand All @@ -62,7 +66,7 @@ router.post("/user", userControllers.check);
// router.put("/user", userControllers.edit);

// Route to delete existed users
router.delete("/user", userControllers.destroy);
router.delete("/user", userMiddle.verifyAdminToken, userControllers.destroy);

router.get("/getprofile", verifyToken, userControllers.getProfile);

Expand Down
15 changes: 13 additions & 2 deletions backend/src/routerAdmin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
const express = require("express");
const adminController = require("./controllers/adminController");
const middleware = require("./middlewares/userMiddle");

const routerAdmin = express.Router();

routerAdmin.get("/users", adminController.browse);
routerAdmin.delete("/users/:email", adminController.destroy);
routerAdmin.get(
"/users",
middleware.verifyToken,
middleware.verifyAdminToken,
adminController.browse
);
routerAdmin.delete(
"/users/:email",
middleware.verifyToken,
middleware.verifyAdminToken,
adminController.destroy
);

module.exports = routerAdmin;
5 changes: 3 additions & 2 deletions frontend/src/context/AdminContext.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useContext, useMemo, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Navigate } from "react-router-dom";
// import { Navigate } from "react-router-dom";
import axios from "axios";
import { useUserContext } from "./UserContext";
import AdminService from "../services/AdminService";
Expand Down Expand Up @@ -116,7 +116,8 @@ export default function AdminContextProvider({ children }) {
return user.isAdmin ? (
<adminContext.Provider value={adminData}>{children}</adminContext.Provider>
) : (
<Navigate to="/" />
<adminContext.Provider value={adminData}>{children}</adminContext.Provider>
// <Navigate to="/" />
);
}
AdminContextProvider.propTypes = {
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/context/UserContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export default function UserContextProvider({ children }) {
`${import.meta.env.VITE_BACKEND_URL}/api/user`,
credentials
);
// const { headers, data } = await axios.get(
// `${import.meta.env.VITE_BACKEND_URL}/api/user?email=${
// credentials.email
// }&password=${credentials.password}`
// );
return {
headers,
userdb: data.user,
Expand Down Expand Up @@ -137,7 +142,11 @@ export default function UserContextProvider({ children }) {
try {
const { message, insertId } = await axios.post(
`${import.meta.env.VITE_BACKEND_URL}/api/users`,
newUser
{
...newUser,
imgUrl:
"https://cdn.pixabay.com/photo/2020/07/01/12/58/icon-5359553_1280.png",
}
);
if (+insertId === 0) {
setMessageUser(message);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/ForgotPassword.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default function ForgotPassword() {
<div className="inscription_container">
<Container size="xs">
<Banner imgUrl={bannerImage} />
<h2>Nom et prénom</h2>
<Fieldset legend="Coordonnées" radius="sm" className="transparent">
<h2>Mot de passe oublié</h2>
<Fieldset legend="Email" radius="sm" className="transparent">
<Input
placeholder="Votre email"
value={email}
Expand Down

0 comments on commit 707cd92

Please sign in to comment.