Skip to content

Commit

Permalink
Merge pull request #15 from ArinNigam/userModels
Browse files Browse the repository at this point in the history
Updated Models and Schemas
  • Loading branch information
henilp105 authored Feb 4, 2024
2 parents f97f117 + a55d579 commit 85cd19b
Show file tree
Hide file tree
Showing 37 changed files with 1,797 additions and 567 deletions.
6 changes: 5 additions & 1 deletion backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import otpResource from "./resources/otpResource.js";
import Connection from "./database/db.js";
import bodyParser from "body-parser";
import cors from "cors";
import auth from "./middlewares/auth.js";
const PORT =`${process.env.PORT || 3000}`;
const app = express();

Expand All @@ -19,8 +20,11 @@ app.use(cors());

// Get Database connection
Connection();

app.use(authResource);
app.use(otpResource);
app.use("/", testResource);

app.get('/protected', auth, (req, res) => {
res.json({ message: 'Access granted' });
});
export default app;
6 changes: 5 additions & 1 deletion backend/constants/errorMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const userAlreadyExists = "User with this email already exists!";
export const userNotFound = "User with this email does not exist!";
export const incorrectPassword = "Incorrect password.";
export const internalServerError = "Internal Server Error. Please try again later.";
export const userCreated = "User created successfully";

//Database Connection
export const databaseConnected = "Database connected successfully";
Expand All @@ -20,4 +21,7 @@ export const otpSent = 'OTP sent successfully';

// Auth Middleware
export const noAuthToken = 'No auth token, access denied';
export const tokenVerificationFailed = 'Token verification failed, authorization denied.';
export const invalidAuthToken = 'Invalid token';
export const tokenVerificationFailed = 'Token verification failed, authorization denied.';
export const invalidUserType = 'Invalid user type';
export const tokenUpdateError= 'Error updating token';
37 changes: 19 additions & 18 deletions backend/middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import jwt from "jsonwebtoken";
import * as errorMessages from "../constants/errorMessages.js";
const passwordKey = process.env.PASSWORD_KEY;
const secretKey = process.env.ACCESS_TOKEN_SECRET;

const auth = async (req, res, next) => {
try {
const token = req.header("x-auth-token");
if (!token)
return res.status(401).json({ msg: errorMessages.noAuthToken });

const verified = jwt.verify(token, passwordKey);
if (!verified)
return res
.status(401)
.json({ msg: errorMessages.tokenVerificationFailed });

req.user = verified.id;
req.token = token;
next();
} catch (err) {
res.status(500).json({ error: err.message });
}
try {
const bearerHeader = req.headers['authorization'];
if(typeof bearerHeader !== 'undefined') {
const bearer = bearerHeader.split(' ');
const bearerToken = bearer[1];
// Verify the token
jwt.verify(bearerToken, secretKey, (err, user) => {
if(err) {
res.sendStatus(403);
} else {
req.user = user;
next();
}
});
}
} catch (error) {
res.status(500).json({ error: err.message });
}
};

export default auth;
10 changes: 10 additions & 0 deletions backend/models/achievement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import mongoose from 'mongoose';

const achievementSchema = new mongoose.Schema({
name: { type: String, required: true },
date: { type: Date, required: true },
description: { type: String, required: true },
});

const Achievement = mongoose.model('Achievement', achievementSchema);
export default Achievement;
10 changes: 3 additions & 7 deletions backend/models/user.js → backend/models/admin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
name:{
required: true,
type:String,
},
const adminSchema = new mongoose.Schema({
email:{
required: true,
type:String,
Expand All @@ -23,5 +19,5 @@ const userSchema = new mongoose.Schema({
},
})

const User = mongoose.model("User", userSchema);
export default User;
const Admin = mongoose.model("Admin", adminSchema);
export default Admin;
33 changes: 33 additions & 0 deletions backend/models/course.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mongoose from 'mongoose';

const courseSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
courseCode: {
type: String,
required: true,
unique: true
},
primaryRoom: {
type: String,
},
credits: {
type: Number,
required: true,
},
professorId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Faculty',
required: true,
},
branches: [{
type: String,
required: true
}]
});

const Course = mongoose.model('Course', courseSchema);

export default Course;
27 changes: 27 additions & 0 deletions backend/models/faculty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mongoose from 'mongoose';

const facultySchema = new mongoose.Schema({
name: {
type: String,
default: 'Smart Insti User'
},
email: {
type: String,
required: true,
unique: true
},
cabinNumber: {
type: String,
},
department: {
type: String,
},
courses: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Course'
}],

});

const Faculty = mongoose.model('Faculty', facultySchema);
export default Faculty;
30 changes: 30 additions & 0 deletions backend/models/lost_and_found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import mongoose from 'mongoose';

const lostAndFoundItemSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
lastSeenLocation: {
type: String,
},
imagePath: {
type: String,
},
description: {
type: String,
required: true
},
contactNumber: {
type: String,
required: true
},
isLost: {
type: Boolean,
required: true
}
});

const LostAndFoundItem = mongoose.model('LostAndFoundItem', lostAndFoundItemSchema);

export default LostAndFoundItem;
24 changes: 24 additions & 0 deletions backend/models/mess_menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require('mongoose');

const messMenuSchema = new mongoose.Schema({
id: {
type: String,
required: true
},
kitchenName: {
type: String,
required: true
},
messMenu: {
type: Map,
of: {
type: Map,
of: [String]
},
required: true
}
});

const MessMenu = mongoose.model('MessMenu', messMenuSchema);

module.exports = MessMenu;
26 changes: 26 additions & 0 deletions backend/models/room.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

const mongoose = require('mongoose');

const roomSchema = new mongoose.Schema({
id: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
vacant: {
type: Boolean,
default: true,
},
occupantId: {
type: String,
default: null,
},
});

const Room = mongoose.model('Room', roomSchema);

module.exports = Room;

9 changes: 9 additions & 0 deletions backend/models/skill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import mongoose from 'mongoose';

const skillSchema = new mongoose.Schema({
name: { type: String, required: true },
level: { type: Number, required: true },
});

const Skill = mongoose.model('Skill', skillSchema);
export default Skill;
41 changes: 41 additions & 0 deletions backend/models/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import mongoose from 'mongoose';
const studentSchema = new mongoose.Schema({
name: {
type: String,
default: 'Smart Insti User'
},
email: {
type: String,
required: true
},
rollNumber: {
type: String,
},
about: {
type: String,
},
profilePicURI: {
type: String,
},
branch: {
type: String,
},
graduationYear: {
type: Number,
},
skills: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Skill'
}],
achievements: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Achievement'
}],
roles: {
type: [String],
}
});

const Student = mongoose.model('Student', studentSchema);

export default Student;
Loading

0 comments on commit 85cd19b

Please sign in to comment.