|
@@ -1,38 +1,47 @@
|
|
|
-import auth from "$lib/server/auth.js";
|
|
|
|
|
|
|
+import mongoose from "mongoose";
|
|
|
import Recipe from "$lib/server/models/Recipe.js";
|
|
import Recipe from "$lib/server/models/Recipe.js";
|
|
|
import User from "$lib/server/models/User.js";
|
|
import User from "$lib/server/models/User.js";
|
|
|
import {JWT_SECRET} from "$env/static/private";
|
|
import {JWT_SECRET} from "$env/static/private";
|
|
|
import jwt from "jsonwebtoken";
|
|
import jwt from "jsonwebtoken";
|
|
|
|
|
|
|
|
export async function load({cookies, params}){
|
|
export async function load({cookies, params}){
|
|
|
- let recipe;
|
|
|
|
|
|
|
+ let recipe = null;
|
|
|
let user = null;
|
|
let user = null;
|
|
|
let isOwner = false;
|
|
let isOwner = false;
|
|
|
try{
|
|
try{
|
|
|
const userData = jwt.verify(cookies.get("user"), JWT_SECRET);
|
|
const userData = jwt.verify(cookies.get("user"), JWT_SECRET);
|
|
|
user = await User.findOne({_id: userData.id, token: userData.token});
|
|
user = await User.findOne({_id: userData.id, token: userData.token});
|
|
|
}catch{}
|
|
}catch{}
|
|
|
-
|
|
|
|
|
|
|
|
|
|
try{
|
|
try{
|
|
|
- recipe = await Recipe.findOne({_id: params.recipeId}, {"ingredients._id": 0}).lean();
|
|
|
|
|
- isOwner = user && user._id.toString() === recipe.user.toString();
|
|
|
|
|
|
|
+ if(!mongoose.isValidObjectId(params.recipeId)){
|
|
|
|
|
+ return {isOwner, recipe};
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if(recipe.private && !isOwner){
|
|
|
|
|
- recipe = null;
|
|
|
|
|
- }else{
|
|
|
|
|
- recipe = {
|
|
|
|
|
- id: recipe._id.toString(),
|
|
|
|
|
- name: recipe.name,
|
|
|
|
|
- ingredients: recipe.ingredients,
|
|
|
|
|
- steps: recipe.steps,
|
|
|
|
|
- notes: recipe.notes,
|
|
|
|
|
- createdAt: recipe.createdAt,
|
|
|
|
|
- updatedAt: recipe.updatedAt
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ const doc = await Recipe.findById(params.recipeId, {"ingredients._id": 0}).lean();
|
|
|
|
|
+ if(!doc){
|
|
|
|
|
+ return {isOwner, recipe};
|
|
|
}
|
|
}
|
|
|
- }catch(e){
|
|
|
|
|
|
|
+
|
|
|
|
|
+ isOwner = !!(user && user._id.toString() === doc.user.toString());
|
|
|
|
|
+
|
|
|
|
|
+ if(doc.private && !isOwner){
|
|
|
|
|
+ return {isOwner: false, recipe: null};
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ recipe = {
|
|
|
|
|
+ id: doc._id.toString(),
|
|
|
|
|
+ name: doc.name,
|
|
|
|
|
+ ingredients: doc.ingredients ?? [],
|
|
|
|
|
+ steps: doc.steps ?? [],
|
|
|
|
|
+ notes: doc.notes ?? "",
|
|
|
|
|
+ private: doc.private,
|
|
|
|
|
+ createdAt: doc.createdAt,
|
|
|
|
|
+ updatedAt: doc.updatedAt
|
|
|
|
|
+ };
|
|
|
|
|
+ }catch{
|
|
|
recipe = null;
|
|
recipe = null;
|
|
|
|
|
+ isOwner = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
return {
|