Skip to content

Commit

Permalink
Merge pull request #40 from Naresh-chandanbatve/main
Browse files Browse the repository at this point in the history
added sessions integration and remaining schemas
  • Loading branch information
Siddhant-Patil0203 authored Aug 28, 2023
2 parents 7b8c693 + 9f88823 commit 901c443
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ sysinfo.txt
*.aab
*.unitypackage
*.app
dist/

#server files
node_modules/
Expand Down
3 changes: 2 additions & 1 deletion server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
*.env
.env
.env
dist/
67 changes: 67 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"type": "module",
"scripts": {
"start": "ncc build index.js && node index.js",
"server": "nodemon index.js",
"build": "mkdir -p dist && cp index.js src/api dist/"
"server": "nodemon index.js"
},
"author": "Siddhant Patil, Naresh Chandanbatve, Vishal Kesharwani, Harshal Lade and Saurabh Yadav",
"license": "ISC",
Expand All @@ -17,6 +16,7 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-session": "^1.17.3",
"jsonwebtoken": "^9.0.0",
"mongoose": "^7.4.2",
"nodemailer": "^6.9.2",
Expand Down
28 changes: 27 additions & 1 deletion server/src/api/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import jwt from 'jsonwebtoken'

const indexController = async (req, res) => {
res.status(200).json({message:"Arrow Server is running..."})

/**
* check if there is already any session
* if yes get the session data and login
* else redirect to landing page
*/
if(req.session.user){

let isTokenValid = jwt.decode(req.session.user.token)
if(isTokenValid){
res.status(200).json({
success: true,
user: req.session.user,
msg: req.session.user.user.name +" is logged in successfully"
});
}

}
else{
//show landing page
res.status(200).json({message:"Arrow Server is running..."})
}



}

export default indexController;
7 changes: 6 additions & 1 deletion server/src/api/controllers/userAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ export const signin = async (req, res) => {

const token = generateToken(oldUser, SECRET);

req.session.user = {
token: token,
user: oldUser
}

res.status(200).json({
success: true,
result: oldUser,
Expand All @@ -137,6 +142,6 @@ export const signin = async (req, res) => {
}
}
else{
res.json({msg:"User Admin does not exist"})
res.json({ msg:"User Admin does not exist" })
}
}
6 changes: 6 additions & 0 deletions server/src/api/controllers/userStudent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import userStudentModel from "../models/userStudentModel.js"
import bcrypt from 'bcrypt'
import generateToken from "../middlewares/generateToken.js"


/**
* Route: /userStudent
* Desc: to show or Access user Student
Expand Down Expand Up @@ -123,6 +124,11 @@ export const signin = async (req, res) => {

const token = generateToken(oldUser, SECRET);

req.session.user = {
token: token,
user: oldUser
}

res.status(200).json({
success: true,
result: oldUser,
Expand Down
29 changes: 29 additions & 0 deletions server/src/api/middlewares/authAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import jwt from "jsonwebtoken"

const SECRET = process.env.ADMIN_SECRET

const authAdmin = (req, next) => {

try {
const token = req.headers.authprization.split(" ")[1]
const isCustomAuth = token.lenght < 500

let decodedData
if(token && isCustomAuth){
decodedData = jwt.verify(token, SECRET)
req.userId = decodedData?.id
}
else{
decodedData = jwt.decode(token)
req.userId = decodedData?.sub
}

next()
} catch (error) {
console.log(error)
}

}


export default authAdmin
19 changes: 19 additions & 0 deletions server/src/api/middlewares/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import express from "express"
import session from 'express-session'
import * as dotenv from "dotenv";
dotenv.config();


const app = express();
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
},
(next)=>{
next()
}
))

export default app

19 changes: 19 additions & 0 deletions server/src/api/models/userFacultyModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from "mongoose";


//estimated user schema
const userFacultyModel = mongoose.Schema({
id: { type: String },
name: { type: String, required: true },
branch: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
subjects: { type: String, required: true },
designation: { type: String, required: true },
education: { type: String, required: true },
bio: { type: String },
intrest: { type: String }

})

export default mongoose.model("userFaculties",userFacultyModel);
14 changes: 14 additions & 0 deletions server/src/api/models/userGuestModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import mongoose from "mongoose";


//estimated user schema
const userGuestModel = mongoose.Schema({
id: { type: String },
name: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
bio: { type: String, required: true },

})

export default mongoose.model("userGuests", userGuestModel);
17 changes: 17 additions & 0 deletions server/src/api/models/userStaffModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mongoose from "mongoose";


//estimated user schema
const userStaffModel = mongoose.Schema({
id: { type: String },
name: { type: String, required: true },
branch: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
designation: { type: String, required: true },
bio: { type: String },
mobile: { type: Number }

})

export default mongoose.model("userStaffs",userStaffModel);
4 changes: 2 additions & 2 deletions server/src/api/models/userStudentModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import mongoose from "mongoose";
const userStudentModel = mongoose.Schema({
id: { type: String },
name: { type: String, required: true },
enrollNo: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
year: { type: String, required: true },
branch: { type: String },
year: { type: String },
intrest: {type: String},
roll: { type: Number },
mobile: { type: Number }

Expand Down
3 changes: 2 additions & 1 deletion server/src/api/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from 'express';
import session from '../middlewares/session.js';
const router = express.Router();

import indexController from "../controllers/index.js";


router.get("/", indexController)
router.get("/", session, indexController)

export default router;
4 changes: 3 additions & 1 deletion server/src/api/routes/userAdmin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import express from 'express';
import session from '../middlewares/session.js'

const router = express.Router();

import {userAdmin, signup, signin} from "../controllers/userAdmin.js";


router.get("/", userAdmin)
router.post("/signup", signup)
router.post("/signin", signin)
router.post("/signin", session, signin)

export default router;
3 changes: 2 additions & 1 deletion server/src/api/routes/userStudent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from 'express';
const router = express.Router();
import session from '../middlewares/session.js';

import {userStudent, signup, signin} from "../controllers/userStudent.js";


router.get("/", userStudent)
router.post("/signup", signup)
router.post("/signin", signin)
router.post("/signin",session, signin)

export default router;

0 comments on commit 901c443

Please sign in to comment.