diff --git "a/images/2024-12-07T21-05-47.414Zblog-website-2 \342\200\224 Mozilla Firefox 11_27_2024 1_09_05 AM.png" "b/images/2024-12-07T21-05-47.414Zblog-website-2 \342\200\224 Mozilla Firefox 11_27_2024 1_09_05 AM.png" deleted file mode 100644 index 8fbf934..0000000 Binary files "a/images/2024-12-07T21-05-47.414Zblog-website-2 \342\200\224 Mozilla Firefox 11_27_2024 1_09_05 AM.png" and /dev/null differ diff --git a/images/2024-12-09T17-29-21.725Zcreate mew groups - My Workspace 12_7_2024 8_30_01 PM.png b/images/2024-12-09T17-29-21.725Zcreate mew groups - My Workspace 12_7_2024 8_30_01 PM.png deleted file mode 100644 index f6dd944..0000000 Binary files a/images/2024-12-09T17-29-21.725Zcreate mew groups - My Workspace 12_7_2024 8_30_01 PM.png and /dev/null differ diff --git a/src/app.js b/src/app.js index 7ff170b..716e5c0 100644 --- a/src/app.js +++ b/src/app.js @@ -6,10 +6,12 @@ const cors = require('cors'); const favicon = require('express-favicon'); const logger = require('morgan'); + const connectToDb = require('./db/connectToDb.js'); connectToDb(); + const mainRouter = require('./routes/mainRouter.js'); const authRouter = require('./routes/authRouter.js'); const usersRoute = require('./routes/usersRouter.js'); diff --git a/src/controllers/authController.js b/src/controllers/authController.js index 5f349d5..273d5c4 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -1,7 +1,7 @@ const asyncHandler = require("express-async-handler"); const bcrypt = require("bcryptjs"); const {User, validateRegisterUser, validateLoginUser} = require("../models/User"); -const Group = require('../models/Group'); +// const Group = require('../models/Group'); //Register @@ -26,24 +26,24 @@ const Group = require('../models/Group'); } // 3- If the user is not an admin, assign them to a group - let group = null; - if (req.body.role !== 'admin') { - // Check if the group exists by its name - group = await Group.findOne({ name: req.body.groupName }); - - if (!group) { - // If the group doesn't exist, create it - group = new Group({ - name: req.body.groupName, - }); - try { - // Save the new group - await group.save(); - } catch (err) { - return res.status(500).json({ message: "Failed to create group: " + err.message }); - } - } - } +// let group = null; +// if (req.body.role !== 'admin' && req.body.groupName) { +// // Only check for the group if the role is not admin and groupName is provided +// group = await Group.findOne({ name: req.body.groupName }); + +// if (!group) { +// // If the group doesn't exist, create it +// group = new Group({ +// name: req.body.groupName, +// }); +// try { +// // Save the new group +// await group.save(); +// } catch (err) { +// return res.status(500).json({ message: "Failed to create group: " + err.message }); +// } +// } +// } // 4- Hash the password const salt = await bcrypt.genSalt(10); @@ -55,9 +55,9 @@ const Group = require('../models/Group'); last_name: req.body.last_name, email: req.body.email, password: hashedPassword, - role: req.body.role || 'student', // Default role is student + // role: req.body.role || 'student', // Default role is student isAdmin: req.body.role === 'admin', // If role is 'admin', set isAdmin to true - groupId: group ? group._id : null, // Only assign a group if the user is not an admin + // groupId: group ? group._id : null, // Only assign a group if the user is not an admin }); try { @@ -117,8 +117,8 @@ const Group = require('../models/Group'); res.status(200).json({ _id: user._id, isAdmin: user.isAdmin, - role: user.role, // Send role to the frontend - groupId: user.groupId, // Send groupId to the frontend + // role: user.role, // Send role to the frontend + // groupId: user.groupId, // Send groupId to the frontend profilePhoto: user.profilePhoto, token, first_name: user.first_name, // Send the JWT token diff --git a/src/models/User.js b/src/models/User.js index 8122ebc..12940d0 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -50,30 +50,31 @@ const UserSchema = new mongoose.Schema({ isAccountVerified: { type:Boolean, default: false, - }, - role: { - type: String, - enum: ['student', 'mentor', 'admin'], // Roles allowed - default: 'student', // Default role is student - }, - groupId: { - type: mongoose.Schema.Types.ObjectId, - ref: 'Group', // Reference to a Group collection - required: function() { return this.role !== 'admin'; }, - }, + // }, + // role: { + // type: String, + // enum: ['student', 'mentor', 'admin'], // Roles allowed + // default: 'student', // Default role is student + // }, + // groupId: { + // type: mongoose.Schema.Types.ObjectId, + // ref: 'Group', // Reference to a Group collection + // required: function() { return this.role !== 'admin'; }, + // }, -}, { - timestamps: true, +}, +}, +{ timestamps: true } -}); +); // Generate Auth Token UserSchema.methods.generateAuthToken = function() { return jwt.sign( { id: this._id, - role: this.role, - groupId: this.groupId || null, // Admin will have null groupId + // role: this.role, + // groupId: this.groupId || null, // Admin will have null groupId isAdmin: this.isAdmin }, process.env.JWT_SECRET, @@ -91,12 +92,12 @@ function validateRegisterUser(obj) { last_name: Joi.string().trim().min(2).max(100).required(), email: Joi.string().trim().min(5).max(100).required().email(), password: Joi.string().trim().min(8).required(), - role: Joi.string().valid('student', 'mentor', 'admin').optional(), // role can be passed - groupName: Joi.string().when('role', { - is: Joi.not('admin'), // Only require groupName if the role is not 'admin' - then: Joi.required(), - otherwise: Joi.optional() - }), + // role: Joi.string().valid('student', 'mentor', 'admin').optional(), // role can be passed + // groupName: Joi.string().when('role', { + // is: Joi.not('admin'), // Only require groupName if the role is not 'admin' + // then: Joi.optional(), + // otherwise: Joi.optional() + // }), }); return schema.validate(obj); } diff --git a/src/routes/usersRouter.js b/src/routes/usersRouter.js index dec2063..c1f94c7 100644 --- a/src/routes/usersRouter.js +++ b/src/routes/usersRouter.js @@ -18,6 +18,7 @@ const photoUpload = require("../middlewares/photoUpload"); // api/users/photo-upload router.route("/photo-upload") .post(verifyToken, photoUpload.single("image"), PhotoUploadCtrl) +//router.post('/users/photo-upload', upload.single('profilePhoto'), PhotoUploadCtrl);