« get me outta code hell

Fix error when viewing homepage while logged out - scratchrlol - Simple HTML-based Scratch client
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-11-19 22:54:45 -0400
committerFlorrie <towerofnix@gmail.com>2018-11-19 22:54:45 -0400
commit89a6005e3cf0e1b50cbd953b0884f63ed629a37d (patch)
tree49080d6fac7bb4257c32816d93aa33d1b802f56e
parent2bcafc451ff8c930909ec8bf6ccef2dc706b627b (diff)
Fix error when viewing homepage while logged out
-rwxr-xr-xindex.js19
1 files changed, 13 insertions, 6 deletions
diff --git a/index.js b/index.js
index 1619d6a..361803b 100755
--- a/index.js
+++ b/index.js
@@ -569,15 +569,22 @@ const handleRequest = async (request, response) => {
   }
 
   if (urlParts.length === 0) {
-    const activity = await fetch(`https://api.scratch.mit.edu/users/${cookie.username}/following/users/activity?limit=8&x-token=${cookie.token}`).then(res => res.json())
-    const activityText = activity.map(templates.activity).join('\n')
+    let activityText = ''
+
+    if (cookie.username) {
+      const activity = await fetch(`https://api.scratch.mit.edu/users/${cookie.username}/following/users/activity?limit=8&x-token=${cookie.token}`).then(res => res.json())
+      activityText = activity.map(templates.activity).join('\n')
+    }
 
     return page(request, response, fixWS`
       <h1>Scratch Unofficial HTML Client</h1>
-      <h2>What's Happening?</h2>
-      <ul>
-        ${activityText}
-      </ul>
+      <p>Welcome!</p>
+      ${activityText ? fixWS`
+        <h2>What's Happening?</h2>
+        <ul>
+          ${activityText}
+        </ul>
+      ` : ''}
     `, 'Scratch Homepage')
   }