From 5753570ae784b70fe95cb63aac0f7e1ea8347e53 Mon Sep 17 00:00:00 2001 From: Darius BERTRAND Date: Thu, 19 Jan 2023 11:17:32 +0100 Subject: [PATCH] avancement image profil --- .../src/assets/img}/random-user.png | Bin .../Configurations/ConfigurationDuProfil.js | 25 ++++-------------- .../client/src/components/Log/Inscription.js | 2 +- Site Web/client/src/components/MiniProfil.js | 3 +-- Site Web/controllers/auth.controller.js | 2 -- Site Web/controllers/user.controller.js | 12 +++++---- Site Web/models/user.model.js | 3 ++- 7 files changed, 16 insertions(+), 31 deletions(-) rename Site Web/{models => client/src/assets/img}/random-user.png (100%) diff --git a/Site Web/models/random-user.png b/Site Web/client/src/assets/img/random-user.png similarity index 100% rename from Site Web/models/random-user.png rename to Site Web/client/src/assets/img/random-user.png diff --git a/Site Web/client/src/components/Configurations/ConfigurationDuProfil.js b/Site Web/client/src/components/Configurations/ConfigurationDuProfil.js index f54fd261..34d168f1 100644 --- a/Site Web/client/src/components/Configurations/ConfigurationDuProfil.js +++ b/Site Web/client/src/components/Configurations/ConfigurationDuProfil.js @@ -11,11 +11,10 @@ const ConfigurationDuProfil = ()=>{ const userData = useSelector((state) => state.user.user); const [pseudo, setPseudo] = useState(userData.pseudo); - const[userPicture, setUserPicture] = useState(null); + const [userPicture, setUserPicture] = useState(userData.picture.data); const [tmpImage, setTmpImage] =useState(null); - const [tmpAffichageImage, setTmpAffichageImage]=useState(PLUS) + const [tmpAffichageImage, setTmpAffichageImage]=useState(PLUS); const [displayAdd, setDisplayAdd] = useState(false); - const uid = useContext(UidContext); const [message, setMessage] = useState(''); const handleLoadFile=(e)=>{ @@ -31,25 +30,11 @@ const ConfigurationDuProfil = ()=>{ console.log("eeee"); }; console.log("rrrrrr"); - setTmpAffichageImage(`${tmpImage.data}`) + setTmpAffichageImage(`${tmpImage.data}`); console.log("uuuuuuuuuuuu"); + console.log(tmpImage); }; - - useEffect(() => { - const fetchImage = async () => { - try { - const { data } = await axios.get(`/api/users/${uid}/image`); - setUserPicture(`data:${data.contentType};base64,${data.data}`); - } catch (err) { - console.error(err); - } - }; - fetchImage(); - }, [uid]); - - - const handleTPM =()=> { console.log("test"); console.log(tmpImage); @@ -60,7 +45,7 @@ const ConfigurationDuProfil = ()=>{ e.preventDefault(); try { - await axios.patch( `${process.env.REACT_APP_API_URL}api/user/${uid}/image`, tmpImage); + await axios.patch( `${process.env.REACT_APP_API_URL}api/user/${userData.id}/image`, tmpImage); setMessage("Image de profil mise à jour avec succès!"); } catch (err) { setMessage("Erreur lors de la mise à jour de l'image de profil"); diff --git a/Site Web/client/src/components/Log/Inscription.js b/Site Web/client/src/components/Log/Inscription.js index 71d8bac0..ee143186 100644 --- a/Site Web/client/src/components/Log/Inscription.js +++ b/Site Web/client/src/components/Log/Inscription.js @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { NavLink, redirect} from "react-router-dom"; +import { NavLink} from "react-router-dom"; import axios from "axios"; import Connexion from "./Connexion"; diff --git a/Site Web/client/src/components/MiniProfil.js b/Site Web/client/src/components/MiniProfil.js index bd6549bd..721561b4 100644 --- a/Site Web/client/src/components/MiniProfil.js +++ b/Site Web/client/src/components/MiniProfil.js @@ -8,7 +8,6 @@ import FollowHandler from "./UserProfil/FollowHandler"; const MiniProfil = ({uid} ) => { const [userData, setUserData] = useState(null); - useEffect(() => { async function fetchData() { const response = await fetch(`${process.env.REACT_APP_API_URL}api/user/${uid}`); @@ -25,7 +24,7 @@ const MiniProfil = ({uid} ) => { return (
- + {userData.pseudo}
diff --git a/Site Web/controllers/auth.controller.js b/Site Web/controllers/auth.controller.js index 4ca43ba5..a04a6be8 100644 --- a/Site Web/controllers/auth.controller.js +++ b/Site Web/controllers/auth.controller.js @@ -91,10 +91,8 @@ module.exports.signIn = async (req, res) => { console.log("signin: ",req.body); const {email, password} = req.body try { - console.log("test user1 "); const user = await UserModel.login({email, password}); - console.log("test user2 "); //creation d'un token diff --git a/Site Web/controllers/user.controller.js b/Site Web/controllers/user.controller.js index 663f4307..468fd692 100644 --- a/Site Web/controllers/user.controller.js +++ b/Site Web/controllers/user.controller.js @@ -1,6 +1,6 @@ const UserModel = require("../models/user.model"); const ObjectID = require("mongoose").Types.ObjectId; -/*const Image = mongoose.model('Image', ImageSchema);*/ + //-password pour ne pas donner le password module.exports.getAllUsers = async (req, res) => { @@ -77,10 +77,12 @@ module.exports.compteUpdatePseudo = async (req, res) => { } module.exports.getImage = async (req, res) => { - try { - const image = await Image.findById(req.params.id); - res.status(200).json(image); + const user = await UserModel.findById(req.params.id); + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + res.status(200).json(user.picture); } catch (err) { res.status(500).json({ message: 'Error getting image' }); } @@ -89,7 +91,7 @@ module.exports.getImage = async (req, res) => { module.exports.saveImage = async (req, res) => { try { const { data, contentType } = req.body; - const image = new Image({ data, contentType }); + const image = new UserModel.picture({ data, contentType }); await image.save(); res.status(201).json(image); } catch (err) { diff --git a/Site Web/models/user.model.js b/Site Web/models/user.model.js index bdc8029f..3d7ce9cc 100644 --- a/Site Web/models/user.model.js +++ b/Site Web/models/user.model.js @@ -63,7 +63,7 @@ const userSchema = new mongoose.Schema( }, picture: { type: PictureSchema, - default: "./random-user.png" + default: { data: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAgVBMVEX///8AAACgoKDd3d38/Pza2tr4+Pjk5OT29vbv7+/Hx8eVlZXAwMCnp6f6+vrh4eFWVlbT09OKiootLS3CwsKAgIAiIiKPj4+urq5ISEjr6+tqampcXFwxMTEWFha2trZzc3M7OzuZmZlHR0cgICATExNRUVGCgoILCwt2dnY9PT16/9JNAAAGjUlEQVR4nO2dh3bqMAyGMRBGKGWWUTYdlL7/A15S4DISEkuWKrnH3wO0+g+JLGulVAoEAoFAIBAIBAKBQCAQgPFcr/TieBj3KvWmtC3kRJOXN3PDYL2NG9JmEdEpd80DFv1Y2jpnmtvBI3kn1j1pG13ozQvk/fDerkobiiTe2Oj7oeWjxsheX0Jb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wDR5gznDSwe0P8wzLTZPaL/NYJj6BpE3yFIJ083wv7/gSno5KqAWxypW/pQV/e+5cP6PHkr/mMpaLMd2FToYFvoEhorbNsY6S6pokM4h0U/61g/UWm7DIYROlTXLtnFbhXdy6wPW649f3SvIk0L6dd0vu9vy+Xt+GXWpel/J4vf6KufVBDFNsx9zi4Q+dONtI4cSJJzDE0IdJCUijkaSeggyN2o/glJfkTuuSZXnFdIKnakR5zdKfe4vTuON0XmiRgKHHeDMHapU+HYhSptvg1OBwbrDigqnOri3ItLaHBRuJQ23gqHx5SsyYmXHV4h8/4gKhwawjfStluCTtgwDRnQg25lHEpbbgs6NvUgoDmBVfgtbbg1yBexIW23PcgOBi9CtiPIJaCsQ9q0IEe+ObfnUoPbzMc3/EoPLm8qbTUE1JnvSdh9BDW/wLyYlJYuRiHbLnkOUNcLrg8e8IBxptqT3bdgnKm0zTAQLcQe5IKvQRS8vTosULkarw4L1CXYo7g7YQBX6NVxaDDXfJ9uFglwhdwbH6mBt9Y4tLKJAG/a3UibDARez/ejKHMBXJ5h3/FMDThs804hOKH4LG0xFHAew5uqzBlw6E2+b4YbsELPLk8ZU6xFMH8CiJ6/rxDcGsW40JIHsELPrvjhN8zAu/cQ7Gm8Ow/BCr2L2sBzXt5F3vCUsE/10QT4DVjvsFM28DzN388mKp8kuQfxJUimL8JzAT4sSrzr8unB9AnvpI0GgRDoU9MXtu3Lp4wprpmd+TtHlCBqawl/v/vSn0MfP/okbbkt+PVYnsQ1qJavE36USV32DtR0D3IfcVvh5kFCynVxtPpJ57WjQPWdQxSbMVT/isgxhDvqegNUqkVKVaXNtEvnfQoXhryfccRBvH1f3UVjTr5YsNqm/H6FK2uWxYmlnpLh9e6K8QsmlfZaNkPVHcf8W79r9d5qUv512qu4o+0LSYFAIBAIBAKBQODEP69jiHRqhNTMAAAAAElFTkSuQmCC", contentType: "image/png" } }, bio :{ type: String, @@ -89,6 +89,7 @@ const userSchema = new mongoose.Schema( // play function before save into display: 'block', userSchema.pre("save", async function(next) { + const salt = await bcrypt.genSalt(); this.password = await bcrypt.hash(this.password, salt); next();