|
|
@@ -1,14 +1,21 @@
|
|
|
-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 user = null;
|
|
|
+ try{
|
|
|
+ const userData = jwt.verify(cookies.get("user"), JWT_SECRET);
|
|
|
+ user = await User.findOne({_id: userData.id, token: userData.token});
|
|
|
+ }catch{}
|
|
|
|
|
|
let recipe;
|
|
|
try{
|
|
|
recipe = await Recipe.findOne({_id: params.recipeId}, {"ingredients._id": 0}).lean();
|
|
|
+ const 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();
|