From 8d055516221ee92a0a36776948a4fcb42aba982b Mon Sep 17 00:00:00 2001 From: Tamal Das Date: Thu, 7 Sep 2023 20:38:11 +0530 Subject: [PATCH] fix: minor changes to the routes --- config/passport-googleAuth-strategy.js | 53 ++++++++++++-------------- routes/club/Club.js | 19 --------- routes/display/Display.js | 11 ------ schema/club/EventSchema.js | 18 --------- schema/user/UserSchema.js | 13 ++++++- 5 files changed, 36 insertions(+), 78 deletions(-) delete mode 100644 schema/club/EventSchema.js diff --git a/config/passport-googleAuth-strategy.js b/config/passport-googleAuth-strategy.js index 40017a1..be5bc4e 100644 --- a/config/passport-googleAuth-strategy.js +++ b/config/passport-googleAuth-strategy.js @@ -11,13 +11,10 @@ passport.use( callbackURL: process.env.CALLBACK_URL, }, async function (accessToken, refreshToken, profile, done) { - User.findOne({ email: profile.emails[0].value }).exec(function ( - err, - user, - ) { - if (err) { - return; - } + try { + let user = await User.findOne({ + email: profile.emails[0].value, + }).exec(); if (user) { return done(null, user); @@ -27,22 +24,18 @@ passport.use( if (!lastname) lastname = " "; - User.create( - { - firstname: firstname, - lastname: lastname, - email: profile.emails[0].value, - password: crypto.randomBytes(20).toString("hex"), - }, - function (err, user) { - if (err) { - return; - } - return done(null, user); - }, - ); + user = await User.create({ + firstname: firstname, + lastname: lastname, + email: profile.emails[0].value, + password: crypto.randomBytes(20).toString("hex"), + }); + + return done(null, user); } - }); + } catch (err) { + return done(err); + } }, ), ); @@ -51,14 +44,18 @@ passport.serializeUser(function (user, done) { done(null, user.id); }); -// deserializing the user the key in the cookies -passport.deserializeUser(function (id, done) { - User.findById(id, function (err, user) { - if (err) { - return done(null, user); +passport.deserializeUser(async function (id, done) { + try { + const user = await User.findById(id).exec(); + + if (!user) { + return done(null, false); } + return done(null, user); - }); + } catch (err) { + return done(err); + } }); module.exports = passport; diff --git a/routes/club/Club.js b/routes/club/Club.js index 4c8efc5..9f746af 100644 --- a/routes/club/Club.js +++ b/routes/club/Club.js @@ -4,7 +4,6 @@ const express = require("express"); const Club = require("../../schema/club/ClubSchema"); const router = express.Router(); const bcrypt = require("bcryptjs"); -const Events = require("../../schema/club/EventSchema"); var jwt = require("jsonwebtoken"); router.get("/", async (req, res) => { @@ -90,22 +89,4 @@ router.post("/login", async (req, res) => { } }); -//* Route 3 - Create Event - -router.post("/createevent", async (req, res) => { - try { - const { eventname, eventlocation, eventdate, eventdescription } = req.body; - const eventData = Events({ - Eventname: eventname, - Eventdate: eventdate, - Eventlocation: eventlocation, - Eventdescription: eventdescription, - }); - await eventData.save(); - res.status(200).json(eventData); - } catch (e) { - res.status(500).json({ message: "Internal Server Error" }); - } -}); - module.exports = router; diff --git a/routes/display/Display.js b/routes/display/Display.js index 9fdff70..f66b0a6 100644 --- a/routes/display/Display.js +++ b/routes/display/Display.js @@ -3,7 +3,6 @@ const express = require("express"); const Club = require("../../schema/club/ClubSchema"); const User = require("../../schema/user/UserSchema"); -const Events = require("../../schema/club/EventSchema"); const router = express.Router(); //* Route 1 - Show all avaialble Users in the DB @@ -30,14 +29,4 @@ router.get("/clubs", async (req, res) => { } }); -//* Route 3 - Show all the other events -router.get("/events", async (req, res) => { - try { - const allEvents = await Events.find({}); - return res.status(200).json(allEvents); - } catch (error) { - res.status(500).json({ message: "Internal Server Error" }); - } -}); - module.exports = router; diff --git a/schema/club/EventSchema.js b/schema/club/EventSchema.js deleted file mode 100644 index f89b343..0000000 --- a/schema/club/EventSchema.js +++ /dev/null @@ -1,18 +0,0 @@ -const mongoose = require("mongoose"); - -const EventsSchema = mongoose.Schema({ - Eventname: { - type: String, - }, - Eventlocation: { - type: String, - }, - Eventdate: { - type: String, - }, - Eventdescription: { - type: String, - }, -}); - -module.exports = mongoose.model("Events", EventsSchema); diff --git a/schema/user/UserSchema.js b/schema/user/UserSchema.js index b6ccff8..02dc87d 100644 --- a/schema/user/UserSchema.js +++ b/schema/user/UserSchema.js @@ -1,14 +1,23 @@ const mongoose = require("mongoose"); const UserSchema = mongoose.Schema({ - firstname: { + usertype: { + type: String, + }, + slug: { type: String, required: true, }, - lastname: { + name: { type: String, required: true, }, + firstname: { + type: String, + }, + lastname: { + type: String, + }, email: { type: String, required: true,