Explorar el Código

Fix recipe page to display whether logged in or not

Lee Morgan hace 1 semana
padre
commit
fb5a221d03

+ 27 - 8
src/routes/recipe/[recipeId]/+page.server.js

@@ -1,23 +1,42 @@
 import auth from "$lib/server/auth.js";
 import auth from "$lib/server/auth.js";
 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 {JWT_SECRET} from "$env/static/private";
+import jwt from "jsonwebtoken";
 
 
 export async function load({cookies, params}){
 export async function load({cookies, params}){
-    const user = await auth(cookies);
-
     let recipe;
     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{
     try{
         recipe = await Recipe.findOne({_id: params.recipeId}, {"ingredients._id": 0}).lean();
         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;
             recipe = null;
         }else{
         }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;
         recipe = null;
     }
     }
 
 
-    return {recipe};
+    return {
+        isOwner,
+        recipe
+    };
 }
 }

+ 23 - 21
src/routes/recipe/[recipeId]/+page.svelte

@@ -83,6 +83,29 @@
 
 
             <p>{data.recipe.notes}</p>
             <p>{data.recipe.notes}</p>
         </article>
         </article>
+
+        <div class="buttonBox">
+            {#if data.isOwner}
+                <a class="button" href="/recipe/{data.recipe.id}/edit">Edit</a>
+            {/if}
+            <button onclick={toggle}>Recipe Mode</button>
+            <div class="share">
+                <button
+                    type="button"
+                    class="shareToggle"
+                    aria-expanded={shareOpen}
+                    aria-haspopup="true"
+                    onclick={()=>shareOpen = !shareOpen}
+                >Share</button>
+                {#if shareOpen}
+                    <div class="shareMenu" role="menu">
+                        <button role="menuitem" onclick={copyLink}>Copy link</button>
+                        <a href="/recipe/{data.recipe.id}/print" role="menuitem">Print</a>
+                        <button role="menuitem" onclick={download}>Download</button>
+                    </div>
+                {/if}
+            </div>
+        </div>
     {:else}
     {:else}
         <div class="missing">
         <div class="missing">
             <h1>Recipe doesn't exist or is private</h1>
             <h1>Recipe doesn't exist or is private</h1>
@@ -90,27 +113,6 @@
             <a href="/dashboard" class="button">Back to Dashboard</a>
             <a href="/dashboard" class="button">Back to Dashboard</a>
         </div>
         </div>
     {/if}
     {/if}
-
-    <div class="buttonBox">
-        <a class="button" href="/recipe/{data.recipe.id}/edit">Edit</a>
-        <button onclick={toggle}>Recipe Mode</button>
-        <div class="share">
-            <button
-                type="button"
-                class="shareToggle"
-                aria-expanded={shareOpen}
-                aria-haspopup="true"
-                onclick={()=>shareOpen = !shareOpen}
-            >Share</button>
-            {#if shareOpen}
-                <div class="shareMenu" role="menu">
-                    <button role="menuitem" onclick={copyLink}>Copy link</button>
-                    <a href="/recipe/{data.recipe.id}/print" role="menuitem">Print</a>
-                    <button role="menuitem" onclick={download}>Download</button>
-                </div>
-            {/if}
-        </div>
-    </div>
 </div>
 </div>
 
 
 <style>
 <style>