API: /me et mise à jour du package.json

This commit is contained in:
Damien Broqua 2022-03-06 11:04:25 +01:00
parent 52ac5b73e4
commit c889208925
7 changed files with 123 additions and 19 deletions

View file

@ -27,9 +27,6 @@
}, },
"license": "GPL-3.0-or-later", "license": "GPL-3.0-or-later",
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.17.0",
"@babel/core": "^7.17.2",
"@babel/preset-env": "^7.16.11",
"eslint": "^8.9.0", "eslint": "^8.9.0",
"eslint-config-airbnb-base": "^15.0.0", "eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
@ -38,11 +35,12 @@
"husky": "^7.0.4", "husky": "^7.0.4",
"lint-staged": "^12.3.3", "lint-staged": "^12.3.3",
"nodemon": "^2.0.15", "nodemon": "^2.0.15",
"npm-run-all": "^4.1.5", "prettier": "^2.5.1"
"prettier": "^2.5.1",
"rimraf": "^3.0.2"
}, },
"dependencies": { "dependencies": {
"@babel/cli": "^7.17.0",
"@babel/core": "^7.17.2",
"@babel/preset-env": "^7.16.11",
"axios": "^0.26.0", "axios": "^0.26.0",
"connect-ensure-login": "^0.1.1", "connect-ensure-login": "^0.1.1",
"connect-flash": "^0.1.1", "connect-flash": "^0.1.1",
@ -54,14 +52,17 @@
"excel4node": "^1.7.2", "excel4node": "^1.7.2",
"express": "^4.17.2", "express": "^4.17.2",
"express-session": "^1.17.2", "express-session": "^1.17.2",
"joi": "^17.6.0",
"knacss": "^8.0.4", "knacss": "^8.0.4",
"moment": "^2.29.1", "moment": "^2.29.1",
"moment-timezone": "^0.5.34", "moment-timezone": "^0.5.34",
"mongoose": "^6.2.1", "mongoose": "^6.2.1",
"mongoose-unique-validator": "^3.0.0", "mongoose-unique-validator": "^3.0.0",
"npm-run-all": "^4.1.5",
"passport": "^0.5.2", "passport": "^0.5.2",
"passport-http": "^0.3.0", "passport-http": "^0.3.0",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"rimraf": "^3.0.2",
"sass": "^1.49.7", "sass": "^1.49.7",
"vue": "^3.2.31" "vue": "^3.2.31"
}, },

View file

@ -16,6 +16,7 @@ import maCollectionRouter from "./routes/ma-collection";
import importAlbumRouterApiV1 from "./routes/api/v1/albums"; import importAlbumRouterApiV1 from "./routes/api/v1/albums";
import importSearchRouterApiV1 from "./routes/api/v1/search"; import importSearchRouterApiV1 from "./routes/api/v1/search";
import importMeRouterApiV1 from "./routes/api/v1/me";
// Mongoose schema init // Mongoose schema init
require("./models/users"); require("./models/users");
@ -84,6 +85,7 @@ app.use("/", indexRouter);
app.use("/ma-collection", maCollectionRouter); app.use("/ma-collection", maCollectionRouter);
app.use("/api/v1/albums", importAlbumRouterApiV1); app.use("/api/v1/albums", importAlbumRouterApiV1);
app.use("/api/v1/search", importSearchRouterApiV1); app.use("/api/v1/search", importSearchRouterApiV1);
app.use("/api/v1/me", importMeRouterApiV1);
// Handle 404 // Handle 404
app.use((req, res) => { app.use((req, res) => {

View file

@ -11,6 +11,12 @@ import ErrorEvent from "../libs/error";
* Classe permettant la gestion des albums d'un utilisateur * Classe permettant la gestion des albums d'un utilisateur
*/ */
class Albums extends Pages { class Albums extends Pages {
/**
* Méthode permettant de remplacer certains cartactères par leur équivalents html
* @param {String} str
*
* @return {String}
*/
static replaceSpecialChars(str) { static replaceSpecialChars(str) {
if (!str) { if (!str) {
return ""; return "";

46
src/middleware/Me.js Normal file
View file

@ -0,0 +1,46 @@
import Joi from "joi";
import mongoose from "mongoose";
const Users = mongoose.model("Users");
/**
* Classe permettant la gestion de l'utilisateur connecté
*/
class Me {
constructor(req) {
this.req = req;
}
/**
* Méthode permettant de modifier le profil d'un utilisateur
* @return {Object}
*/
async patchMe() {
const { body, user } = this.req;
const schema = Joi.object({
isPublicCollection: Joi.boolean(),
});
const value = await schema.validateAsync(body);
const update = await Users.findByIdAndUpdate(
user._id,
{ $set: value },
{ new: true }
);
await new Promise((resolve, reject) => {
this.req.login(update, (err) => {
if (err) {
return reject(err);
}
return resolve(null);
});
});
return update;
}
}
export default Me;

View file

@ -1,5 +1,7 @@
/* eslint-disable func-names */ /* eslint-disable func-names */
/* eslint-disable no-invalid-this */ /* eslint-disable no-invalid-this */
/* eslint-disable no-param-reassign */
import mongoose from "mongoose"; import mongoose from "mongoose";
import uniqueValidator from "mongoose-unique-validator"; import uniqueValidator from "mongoose-unique-validator";
import crypto from "crypto"; import crypto from "crypto";
@ -28,7 +30,15 @@ const UserSchema = new mongoose.Schema(
default: false, default: false,
}, },
}, },
{ timestamps: true } {
timestamps: true,
toJSON: {
transform(doc, ret) {
delete ret.hash;
delete ret.salt;
},
},
}
); );
UserSchema.plugin(uniqueValidator, { message: "est déjà utilisé" }); UserSchema.plugin(uniqueValidator, { message: "est déjà utilisé" });

24
src/routes/api/v1/me.js Normal file
View file

@ -0,0 +1,24 @@
import express from "express";
import { ensureLoggedIn } from "connect-ensure-login";
import { sendResponse } from "../../../libs/format";
import Me from "../../../middleware/Me";
// eslint-disable-next-line new-cap
const router = express.Router();
router
.route("/")
.patch(ensureLoggedIn("/connexion"), async (req, res, next) => {
try {
const me = new Me(req);
const data = await me.patchMe();
return sendResponse(req, res, data);
} catch (err) {
return next(err);
}
});
export default router;

View file

@ -115,16 +115,26 @@
Partager ma collection Partager ma collection
</header> </header>
<section> <section>
Votre collection sera visible de toute personne disposant du lien suivant : <template v-if="!isPublicCollection">
<br /> Votre collection sera visible de toute personne disposant du lien suivant :
<a :href="shareLink" target="_blank">{{shareLink}}</a> <br />
<br /> <a :href="shareLink" target="_blank">{{shareLink}}</a>
Ce lien permet uniquement de visualiser l'ensemble de votre collection mais ne perment <strong class="is-danger">en aucun cas</strong> de la modifier. <br />
<br /> Ce lien permet uniquement de visualiser l'ensemble de votre collection mais ne perment <strong class="is-danger">en aucun cas</strong> de la modifier.
Vous pourrez à tout moment supprimer le lien de partage en cliquant à nouveau sur l'icône <i class="icon-share"></i> sur votre collection. <br />
Vous pourrez à tout moment supprimer le lien de partage en cliquant à nouveau sur l'icône <i class="icon-share"></i> sur votre collection.
</template>
<template v-if="isPublicCollection">
Vous êtes sur le point de rendre votre collection privée.
<br />
Toute les personnes ayant le lien partagé ne pourront plus accéder à votre collection.
<br />
Vous pourrez à tout moment rendre à nouveau votre collection publique en cliquant sur l'icône <i class="icon-share"></i>.
</template>
</section> </section>
<footer> <footer>
<button class="button is-primary" @click="shareCollection">Partager</button> <button v-if="!isPublicCollection" class="button is-primary" @click="shareCollection">Partager</button>
<button v-if="isPublicCollection" class="button is-danger" @click="shareCollection">Supprimer</button>
<button class="button" @click="toggleModalShare">Annuler</button> <button class="button" @click="toggleModalShare">Annuler</button>
</footer> </footer>
</div> </div>
@ -155,6 +165,7 @@
showModalDelete: false, showModalDelete: false,
showModalShare: false, showModalShare: false,
shareLink: `${protocol}//${host}/collection/<%= user._id %>`, shareLink: `${protocol}//${host}/collection/<%= user._id %>`,
isPublicCollection: <%= user.isPublicCollection ? 'true' : 'false' %>,
} }
}, },
created() { created() {
@ -241,13 +252,17 @@
}); });
}, },
shareCollection() { shareCollection() {
axios.post(`/api/v1/me`, { axios.patch(`/api/v1/me`, {
isPublicCollection: true, isPublicCollection: !this.isPublicCollection,
}) })
.then( () => { .then( (res) => {
this.isPublicCollection = res.data.isPublicCollection;
showToastr("Collection partagée", true); showToastr("Collection partagée", true);
window.open(this.shareLink, '_blank'); if ( this.isPublicCollection ) {
console.log('ici', this.shareLink)
window.open(this.shareLink, '_blank');
}
}) })
.catch((err) => { .catch((err) => {
showToastr(err.response?.data?.message || "Impossible de supprimer cet album"); showToastr(err.response?.data?.message || "Impossible de supprimer cet album");