From ba2b0bcbd632578223325a883f46dd867bb4af38 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 2 Sep 2021 09:59:58 -0700 Subject: [PATCH] users and profiles working --- dbTools/profile.js | 6 ++++-- def/profile.js | 2 ++ index.js | 13 +++++++++++++ routes/profile.js | 9 +++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/dbTools/profile.js b/dbTools/profile.js index 42afdd6..2960d39 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -39,8 +39,10 @@ userDB = (DB) => { console.log(err); return false; }); - if (r[0]) userProfileCache[r[0].id] = r[0]; - return r[0]; + let index = 0; + while(r[index].isGroup) index += 1; + if (r[index]) userProfileCache[r[index].id] = r[index]; + return r[index]; } //Groups diff --git a/def/profile.js b/def/profile.js index 0bc5b13..e9950d3 100644 --- a/def/profile.js +++ b/def/profile.js @@ -17,6 +17,7 @@ class User { this.lastUpdate = info.lastUpdate || new Date(); this.newsFeedCache = info.newsFeedCache || []; this.isGroup = info.isGroup || false; + this.isCourse = info.isCourse || false; } toObj(){ @@ -30,6 +31,7 @@ class User { r.lastUpdate = this.lastUpdate; r.newsFeedCache = this.newsFeedCache; r.isGroup = this.isGroup; + r.isCourse = this.isCourse; return r; } diff --git a/index.js b/index.js index e501319..7c002a4 100644 --- a/index.js +++ b/index.js @@ -140,6 +140,19 @@ DB.getDB.then((DB)=>{ return await login(req, res); }); + app.post('/changeProfile', sessionChecker, async (req, res) => { + const user_sid = getUserId(req); + let profile = await DB.getProfile(req.body.profileid); + if(!profile || profile.userid != user_sid) return res.json({ + status: "profile does not belong to the logged user", + }); + res.cookie('profile_id', profile._id, cookiesOptions); + return res.json({ + status: "ok", + ...profile + }); + }); + // route for user logout const logout = function(req, res){ const session_id = getSessionId(req); diff --git a/routes/profile.js b/routes/profile.js index 4e7f08b..e38d6c3 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -11,6 +11,15 @@ DB.getDB.then((DB)=>{ return DB.ObjectID(user_sid); } + router.get("/mine", async (req, res) => { + let userid = req.cookies.user_sid; + let profiles = await DB.getUserProfiles(userid); + return res.json({ + status: "ok", + profiles + }); + }); + router.get("/new", async (req, res) => { let profile = { userid: getUserId(req),