Browse Source

Bug fix: print page was protected even for public recipes

Lee Morgan 1 tháng trước cách đây
mục cha
commit
b7bea1a012
1 tập tin đã thay đổi với 10 bổ sung và 3 xóa
  1. 10 3
      src/routes/recipe/[recipeId]/print/+page.server.js

+ 10 - 3
src/routes/recipe/[recipeId]/print/+page.server.js

@@ -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();