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