Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Users Able to Register with the Same Username[Feat]: #71 #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions server/Controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ const bcrypt = require("bcryptjs");

module.exports.Signup = async (req, res, next) => {
try {
const { email, password, username, createdAt,role } = req.body;
const existingUser = await User.findOne({ email });
const { email, password, username, createdAt, role } = req.body;

const existingUser = await User.findOne({ username, email });
if (existingUser) {
return res.json({ message: "User already exists" });
}
const user = await User.create({ email, password, username, createdAt ,role});
const user = await User.create({ email, password, username, createdAt, role });
const token = createSecretToken(user._id);
res.cookie("token", token, {
withCredentials: true,
httpOnly: false,
});
res
.status(201)
.json({ message: "User signed in successfully", success: true, token });
.json({ message: "User signed in successfully", success: true, token });
next();
} catch (error) {
console.error(error);
Expand All @@ -28,27 +28,27 @@ module.exports.Signup = async (req, res, next) => {
module.exports.Login = async (req, res, next) => {
try {
const { email, password } = req.body;
if(!email || !password ){
return res.json({message:'All fields are required'})
if (!email || !password) {
return res.json({ message: 'All fields are required' })
}
const user = await User.findOne({ email });
if(!user){
return res.json({message:'Incorrect password or email' })
if (!user) {
return res.json({ message: 'Incorrect password or email' })
}
const auth = await bcrypt.compare(password,user.password)
const auth = await bcrypt.compare(password, user.password)
if (!auth) {
return res.json({message:'Incorrect password or email' })
return res.json({ message: 'Incorrect password or email' })
}
const token = createSecretToken(user._id);
res.cookie("token", token, {
withCredentials: true,
httpOnly: false,
});
res.status(201).json({ message: "User logged in successfully", success: true ,token });
next()
const token = createSecretToken(user._id);

res.cookie("token", token, {
withCredentials: true,
httpOnly: false,
});


res.status(201).json({ message: "User logged in successfully", success: true, token });
next()
} catch (error) {
console.error(error);
}
Expand Down
1 change: 1 addition & 0 deletions server/Controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const User = require("../Models/UserModel");

module.exports.getAllUsers = async (req, res) => {

try {
console.log("hello")
const users = await User.find({});
Expand Down
3 changes: 2 additions & 1 deletion server/Models/UserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ const userSchema = new mongoose.Schema({
username: {
type: String,
required: [true, "Your username is required"],
unique: true
},
password: {
type: String,
required: [true, "Your password is required"],
},
role: {
type: String,
enum: ["admin", "user","student"], // Example roles
enum: ["admin", "user", "student"], // Example roles
default: "user",
},
createdAt: {
Expand Down
10 changes: 5 additions & 5 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const StudentRoute = require("./Routes/StudentRoutes");
const SubjectRoute = require('./Routes/SubjectRoutes');
const StudentElectiveSubjectRoute = require('./Routes/StudentElectiveSubjectRoutes');
const { authMiddleware } = require("./Middleware/AuthMiddleware");
const axios =require('axios')
const axios = require('axios')

require("dotenv").config();
const { MONGO_URL, PORT } = process.env;
Expand Down Expand Up @@ -52,9 +52,9 @@ app.use(
authMiddleware(["admin", "user", "student"]),
StudentElectiveSubjectRoute
);
app.use("/student", authMiddleware(["admin", "user","student"]), StudentRoute);
app.use("/subject", authMiddleware(["admin", "user","student"]), SubjectRoute);
app.get("/yaae", authMiddleware(["admin", "user","student"]), (req, res) => {
app.use("/student", authMiddleware(["admin", "user", "student"]), StudentRoute);
app.use("/subject", authMiddleware(["admin", "user", "student"]), SubjectRoute);
app.get("/yaae", authMiddleware(["admin", "user", "student"]), (req, res) => {
res.json({ status: true, user: req.user });
});

Expand All @@ -72,7 +72,7 @@ app.post("/subscribe-newsletter", async (req, res) => {
publicationId,
}
);
console.log(response);
console.log(response);
// Handle the response from the API call
if (response.status === 200) {
res.status(200).json({ message: "Subscribed to the newsletter successfully!" });
Expand Down