From 8e91427c1ca9568654f6d134a5cf211f0c1ed813 Mon Sep 17 00:00:00 2001 From: Moon Patel Date: Tue, 27 Jun 2023 20:31:47 +0530 Subject: [PATCH] new api /api/user/:username/friends - get friends list --- backend/app.js | 4 +++- backend/models/user.js | 20 +++++++++++++++----- backend/routes/user.js | 14 +++++++++++++- backend/util/auth.js | 3 ++- backend/util/user.js | 0 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 backend/util/user.js diff --git a/backend/app.js b/backend/app.js index 850cbbc..3343cfe 100644 --- a/backend/app.js +++ b/backend/app.js @@ -2,6 +2,7 @@ const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); const authRoutes = require("./routes/auth"); +const userRoutes = require("./routes/user"); const mongoose = require("mongoose"); mongoose @@ -26,7 +27,7 @@ io.on("connection", (socket) => { }); socket.on("play", (data) => { - console.log(data) + console.log(data); let { fromRow, fromCol, toRow, toCol, room } = data; fromRow = 7 - fromRow; fromCol = 7 - fromCol; @@ -53,6 +54,7 @@ app.use((req, res, next) => { // app.use("/", (req, res, next) => res.send('Hello')); app.use("/api/auth", authRoutes); +app.use("/api/user", userRoutes); app.use((error, req, res, next) => { const status = error.status || 500; diff --git a/backend/models/user.js b/backend/models/user.js index bb2097c..26f7e91 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -21,10 +21,12 @@ const userSchema = new Schema( lname: String, location: String, country: String, - friends: { - type: [ObjectId], - ref: "User", - }, + friends: [ + { + type: ObjectId, + ref: "User", + }, + ], }, { virtuals: { @@ -34,7 +36,15 @@ const userSchema = new Schema( }, }, }, + methods: { + async getFriends() { + await this.populate("friends", "username"); + console.log(this.friends); + return this.friends.map(friend => friend.username); + }, + }, } ); -module.exports.User = mongoose.model("User", userSchema); +const User = mongoose.model("User", userSchema); +module.exports = { User }; diff --git a/backend/routes/user.js b/backend/routes/user.js index 32d9dd3..452eee5 100644 --- a/backend/routes/user.js +++ b/backend/routes/user.js @@ -1 +1,13 @@ -const express = require("express").Router(); \ No newline at end of file +const router = require("express").Router(); + +const { checkAuth } = require("../util/auth"); +const { User } = require("../models/user"); + +router.get("/:username/friends", async (req, res, next) => { + const username = req.params.username; + const user = await User.findOne({username}); + const friends = await user.getFriends(); + return res.json({ success: true, friends }); +}); + +module.exports = router; diff --git a/backend/util/auth.js b/backend/util/auth.js index e8fa939..611c7a9 100644 --- a/backend/util/auth.js +++ b/backend/util/auth.js @@ -30,6 +30,7 @@ function checkAuthMiddleware(req, res, next) { return next(new NotAuthError("Not authenticated.")); } const authFragments = req.headers.authorization.split(" "); + console.log(authFragments); if (authFragments.length !== 2) { console.log("NOT AUTH. AUTH HEADER INVALID."); @@ -38,7 +39,7 @@ function checkAuthMiddleware(req, res, next) { const authToken = authFragments[1]; try { const validatedToken = validateJSONToken(authToken); - req.token = validatedToken; + req.userid = validatedToken; } catch (error) { console.log("NOT AUTH. TOKEN INVALID."); return next(new NotAuthError("Not authenticated.")); diff --git a/backend/util/user.js b/backend/util/user.js new file mode 100644 index 0000000..e69de29