├── .gitignore ├── vercel.json ├── db.js ├── controllers ├── note │ ├── category.js │ ├── sub_category.js │ ├── all.js │ └── new.js ├── quiz │ ├── category.js │ ├── sub_category.js │ ├── all.js │ └── new.js └── auth │ ├── id.js │ ├── payment.js │ ├── n-password.js │ ├── login.js │ ├── signin.js │ └── forgot.js ├── router ├── Note.js ├── quiz.js └── auth.js ├── index.js ├── package.json ├── models ├── Quiz.js ├── note.js └── User.js └── middleware └── fetchUser.js /.gitignore: -------------------------------------------------------------------------------- 1 | .vercel 2 | /node_modules -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { "version": 2, "rewrites": [{ "source": "/(.*)", "destination": "/" }] } -------------------------------------------------------------------------------- /db.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | 3 | const MONGO_URI = `mongodb+srv://Aditya:adpandey@cluster0.h40tx.mongodb.net/Note_rn?retryWrites=true&w=majority`; 4 | 5 | 6 | mongoose.connect(MONGO_URI, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | }) 10 | .then(() => { 11 | console.log('Connected to MongoDB'); 12 | }) 13 | .catch((error) => { 14 | console.error('Error connecting to MongoDB:', error); 15 | }); -------------------------------------------------------------------------------- /controllers/note/category.js: -------------------------------------------------------------------------------- 1 | const Note = require('../../models/note'); 2 | 3 | const category = async (req, res) => { 4 | try { 5 | const categories = await Note.distinct('category'); 6 | res.json({ categories }); 7 | } catch (error) { 8 | console.error('Error fetching unique categories:', error); 9 | res.status(500).json({ error: 'Failed to fetch unique categories' }); 10 | } 11 | } 12 | 13 | module.exports = category; -------------------------------------------------------------------------------- /controllers/quiz/category.js: -------------------------------------------------------------------------------- 1 | const Quiz = require('../../models/Quiz'); 2 | 3 | const category = async (req, res) => { 4 | try { 5 | const categories = await Quiz.distinct('category'); 6 | res.json({ categories }); 7 | } catch (error) { 8 | console.error('Error fetching unique categories:', error); 9 | res.status(500).json({ error: 'Failed to fetch unique categories' }); 10 | } 11 | } 12 | 13 | module.exports = category; -------------------------------------------------------------------------------- /controllers/auth/id.js: -------------------------------------------------------------------------------- 1 | const User = require("../../models/User"); 2 | 3 | 4 | const id = async(req, res) => { 5 | try { 6 | let userId = req.user.id; 7 | const user = await User.findById(userId).select("-password"); 8 | // const user1 = user.("-resetToken") 9 | // const user2 = user1.select("-expireToken") 10 | res.send(user); 11 | } catch (error) { 12 | console.log(error.message); 13 | res.status(500).send("Enternal sever error"); 14 | } 15 | } 16 | 17 | module.exports = id; -------------------------------------------------------------------------------- /controllers/auth/payment.js: -------------------------------------------------------------------------------- 1 | const User = require('../../models/User'); 2 | 3 | const paid = async(req, res) => { 4 | try { 5 | 6 | let userId = req.user.id; 7 | const user = await User.findByIdAndUpdate(userId, { last_payment: new Date(), next_payment: new Date(Date.now() + 3600000*24*30) }); 8 | res.json({ message: "Payment Successful" }); 9 | 10 | 11 | 12 | 13 | } catch (error) { 14 | console.log(error.message); 15 | res.status(500).send("Enternal sever error"); 16 | } 17 | } 18 | 19 | module.exports = paid; -------------------------------------------------------------------------------- /router/Note.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const router = express.Router(); 3 | const { body, validationResult } = require("express-validator"); 4 | const get = require("../controllers/note/all"); 5 | const add = require("../controllers/note/new"); 6 | const category = require("../controllers/note/category"); 7 | const sub_category = require("../controllers/note/sub_category"); 8 | 9 | 10 | 11 | router.get("/fetchallnotes",get ); 12 | 13 | 14 | router.post("/add", add); 15 | 16 | router.get('/categories', category); 17 | router.get('/subcategories', sub_category); 18 | 19 | 20 | module.exports = router; -------------------------------------------------------------------------------- /router/quiz.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const router = express.Router(); 3 | const { body, validationResult } = require("express-validator"); 4 | const get = require("../controllers/quiz/all"); 5 | const add = require("../controllers/quiz/new"); 6 | const category = require("../controllers/quiz/category"); 7 | const sub_category = require("../controllers/quiz/sub_category"); 8 | 9 | 10 | 11 | router.get("/fetchallnotes",get ); 12 | 13 | 14 | router.post("/add", add); 15 | 16 | router.get('/categories', category); 17 | router.get('/subcategories', sub_category); 18 | 19 | 20 | module.exports = router; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | var cors = require('cors'); 4 | 5 | require('./db') 6 | 7 | const PORT = process.env.PORT || 5000; 8 | 9 | app.use(express.json()); 10 | app.use(cors()) 11 | 12 | // Avialable Route 13 | app.get('/', (req, res) => { 14 | res.send('Hello Owner ') 15 | }) 16 | // app.use('/api/auth',require('./routes/auth')) 17 | 18 | app.use('/api/note', require('./router/Note')) 19 | app.use('/api/user', require('./router/auth')) 20 | app.use('/api/quiz', require('./router/quiz')) 21 | 22 | 23 | app.listen(PORT, () => { 24 | console.log(`Server is listening on port ${PORT}`); 25 | }); -------------------------------------------------------------------------------- /controllers/note/sub_category.js: -------------------------------------------------------------------------------- 1 | const Note = require('../../models/note'); 2 | 3 | const distinctSubcategories = async (req, res) => { 4 | try { 5 | const { category } = req.query; 6 | 7 | const query = {}; 8 | if (category) { 9 | query.category = category; 10 | } 11 | 12 | const subcategories = await Note.distinct('sub_category', query); 13 | res.json({ subcategories }); 14 | } catch (error) { 15 | console.error('Error fetching unique subcategories:', error); 16 | res.status(500).json({ error: 'Failed to fetch unique subcategories' }); 17 | } 18 | }; 19 | 20 | module.exports = distinctSubcategories; -------------------------------------------------------------------------------- /controllers/quiz/sub_category.js: -------------------------------------------------------------------------------- 1 | const Quiz = require('../../models/Quiz'); 2 | 3 | const distinctSubcategories = async (req, res) => { 4 | try { 5 | const { category } = req.query; 6 | 7 | const query = {}; 8 | if (category) { 9 | query.category = category; 10 | } 11 | 12 | const subcategories = await Quiz.distinct('sub_category', query); 13 | res.json({ subcategories }); 14 | } catch (error) { 15 | console.error('Error fetching unique subcategories:', error); 16 | res.status(500).json({ error: 'Failed to fetch unique subcategories' }); 17 | } 18 | }; 19 | 20 | module.exports = distinctSubcategories; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "backend-agh", 3 | "version": "1.0.0", 4 | "description": "backend for the website and also for the booking purpose", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "nodemon index.js" 8 | }, 9 | "author": "Aditya Pandey", 10 | "license": "ISC", 11 | "dependencies": { 12 | "bcryptjs": "^2.4.3", 13 | "cors": "^2.8.5", 14 | "express": "^4.19.2", 15 | "express-validator": "^7.2.0", 16 | "jsonwebtoken": "^9.0.2", 17 | "mongoose": "^8.5.3", 18 | "multer": "^1.4.5-lts.1", 19 | "nodemailer": "^6.9.14", 20 | "react-native-document-picker": "^9.3.1" 21 | }, 22 | "devDependencies": { 23 | "nodemon": "^3.1.4" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /models/Quiz.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const { Schema } = mongoose; 3 | 4 | const UserSchema = new Schema({ 5 | question: { 6 | type: String, 7 | required: true, 8 | unique: true 9 | }, 10 | options: { 11 | type: Array, 12 | required: true, 13 | }, 14 | correctAnswer: { 15 | type: String, 16 | required: true, 17 | unique: true, 18 | }, 19 | category: { 20 | type: String, 21 | required: true, 22 | }, 23 | 24 | sub_category: { 25 | type: String, 26 | required: true, 27 | } 28 | 29 | 30 | }) 31 | const Quiz = mongoose.model('Quiz', UserSchema); 32 | module.exports = Quiz; -------------------------------------------------------------------------------- /middleware/fetchUser.js: -------------------------------------------------------------------------------- 1 | const jwt = require("jsonwebtoken"); 2 | const JWT_SECRECT = "Aditya"; 3 | 4 | const fetchUser =(req, res, next) =>{ 5 | // get the user from jwt server and id to req object 6 | const token = req.header('auth-token'); 7 | if (!token){ 8 | res.status(401).send({error:"Please authenticate using a valid token"}) 9 | } 10 | try { 11 | const data = jwt.verify(token, JWT_SECRECT); 12 | req.user = data.user; 13 | // res.json(data); 14 | // req.send(req.user); 15 | next() 16 | } catch (error) { 17 | res.status(401).send({error:"Please authenticate using a valid token"}) 18 | } 19 | 20 | } 21 | 22 | module.exports = fetchUser; -------------------------------------------------------------------------------- /controllers/note/all.js: -------------------------------------------------------------------------------- 1 | const Note = require('../../models/note'); 2 | 3 | const get = async (req, res) => { 4 | try { 5 | const { category, sub_category } = req.query; 6 | 7 | const query = {}; 8 | 9 | // Filter by category first 10 | if (category) { 11 | query.category = category; 12 | } 13 | 14 | // Filter by subcategory if both category and subcategory are provided 15 | if (category && sub_category) { 16 | query.sub_category = sub_category; 17 | } 18 | 19 | const notes = await Note.find(query); 20 | res.json(notes); 21 | } catch (error) { 22 | console.error(error.message); 23 | res.status(500).send("Internal Server Error"); 24 | } 25 | }; 26 | 27 | module.exports = get; -------------------------------------------------------------------------------- /controllers/quiz/all.js: -------------------------------------------------------------------------------- 1 | const Quiz = require('../../models/Quiz'); 2 | 3 | const get = async (req, res) => { 4 | try { 5 | const { category, sub_category } = req.query; 6 | 7 | const query = {}; 8 | 9 | // Filter by category first 10 | if (category) { 11 | query.category = category; 12 | } 13 | 14 | // Filter by subcategory if both category and subcategory are provided 15 | if (category && sub_category) { 16 | query.sub_category = sub_category; 17 | } 18 | 19 | const notes = await Quiz.find(query); 20 | res.json(notes); 21 | } catch (error) { 22 | console.error(error.message); 23 | res.status(500).send("Internal Server Error"); 24 | } 25 | }; 26 | 27 | module.exports = get; -------------------------------------------------------------------------------- /models/note.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const { Schema } = mongoose; 3 | 4 | const NoteSchema = new Schema({ 5 | id: { 6 | type: Schema.Types.ObjectId, 7 | default: () => new mongoose.Types.ObjectId(), 8 | unique: true, 9 | }, 10 | category: { 11 | type: String, 12 | required: true, 13 | }, 14 | sub_category: { 15 | type: String, 16 | required: true, 17 | }, 18 | title: { 19 | type: String, 20 | required: true, 21 | 22 | }, 23 | content: { 24 | type: String, 25 | required: true, 26 | 27 | } 28 | 29 | 30 | 31 | }) 32 | const Note = mongoose.model('Note', NoteSchema); 33 | module.exports = Note; -------------------------------------------------------------------------------- /controllers/auth/n-password.js: -------------------------------------------------------------------------------- 1 | const bcrypt = require('bcryptjs') 2 | const User = require("../../models/User"); 3 | 4 | 5 | const newpass = async (req, res) => { 6 | const newPassword = req.body.password 7 | const sentToken = req.params.token 8 | await User.findOne({resetToken:sentToken, expireToken:{ $gt:Date.now()}}) 9 | .then(user=>{ 10 | if(!user){ 11 | return res.status(422).json({error:"Try again session expired"}) 12 | } 13 | bcrypt.hash(newPassword,12).then(hashedpassword=>{ 14 | user.password = hashedpassword 15 | user.resetToken = undefined 16 | user.expireToken = undefined 17 | user.save().then((saveduser)=>{ 18 | res.json({message:"password updated success"}) 19 | }) 20 | }) 21 | }).catch(err=>{ 22 | console.log(err) 23 | }) 24 | } 25 | 26 | module.exports = newpass; -------------------------------------------------------------------------------- /controllers/note/new.js: -------------------------------------------------------------------------------- 1 | const Note = require('../../models/note'); 2 | const { validationResult } = require('express-validator') 3 | 4 | 5 | const add = async (req, res) => { 6 | try { 7 | const { 8 | 9 | title, 10 | content, 11 | category, 12 | sub_category, 13 | // user 14 | 15 | } = req.body; 16 | 17 | // If there are errors, return Bad request and the errors 18 | const errors = validationResult(req); 19 | if (!errors.isEmpty()) { 20 | return res.status(400).json({ errors: errors.array() }); 21 | } 22 | const note = new Note({ 23 | 24 | title, 25 | content, 26 | category, 27 | sub_category 28 | // user: req.user.id, 29 | // user: req.user._id, 30 | }); 31 | const savedNote = await note.save(); 32 | 33 | // res.json(savedNote); 34 | var json = JSON.stringify(savedNote); 35 | 36 | res.send(json); 37 | 38 | } catch (error) { 39 | console.error(error.message); 40 | res.status(500).send("Internal Server Error"); 41 | } 42 | } 43 | 44 | module.exports = add; -------------------------------------------------------------------------------- /models/User.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const { Schema } = mongoose; 3 | 4 | const UserSchema = new Schema({ 5 | name: { 6 | type: String, 7 | required: true, 8 | unique: true 9 | }, 10 | img: { 11 | type: String, 12 | required: true, 13 | }, 14 | email: { 15 | type: String, 16 | required: true, 17 | unique: true, 18 | }, 19 | password: { 20 | type: String, 21 | required: true, 22 | }, 23 | resetToken: { 24 | type: String, 25 | default: null 26 | }, 27 | expireToken: { 28 | type: Date, 29 | default: null 30 | }, 31 | last_payment: { 32 | type: String, 33 | default: null, 34 | }, 35 | next_payment: { 36 | type: String, 37 | default: null, 38 | }, 39 | google_token: { 40 | type: String, 41 | default: null, 42 | }, 43 | date: { 44 | type: Date, 45 | default: Date.now, 46 | } 47 | 48 | }) 49 | const User = mongoose.model('User', UserSchema); 50 | module.exports = User; -------------------------------------------------------------------------------- /controllers/quiz/new.js: -------------------------------------------------------------------------------- 1 | const Quiz = require('../../models/Quiz'); 2 | const { validationResult } = require('express-validator') 3 | 4 | 5 | const add = async (req, res) => { 6 | try { 7 | const { 8 | 9 | question, 10 | options, 11 | correctAnswer, 12 | category, 13 | sub_category 14 | // user 15 | 16 | } = req.body; 17 | 18 | // If there are errors, return Bad request and the errors 19 | const errors = validationResult(req); 20 | if (!errors.isEmpty()) { 21 | return res.status(400).json({ errors: errors.array() }); 22 | } 23 | const note = new Quiz({ 24 | 25 | question, 26 | options, 27 | correctAnswer, 28 | category, 29 | sub_category 30 | // user: req.user.id, 31 | // user: req.user._id, 32 | }); 33 | const savedNote = await note.save(); 34 | 35 | // res.json(savedNote); 36 | var json = JSON.stringify(savedNote); 37 | 38 | res.send(json); 39 | 40 | } catch (error) { 41 | console.error(error.message); 42 | res.status(500).send("Internal Server Error"); 43 | } 44 | } 45 | 46 | module.exports = add; -------------------------------------------------------------------------------- /controllers/auth/login.js: -------------------------------------------------------------------------------- 1 | const User = require("../../models/User"); 2 | const bcrypt = require("bcryptjs"); 3 | const jwt = require("jsonwebtoken"); 4 | const JWT_SECRECT = "Aditya"; 5 | 6 | const login = async (req, res) => { 7 | const { email, password } = req.body; 8 | try { 9 | let user = await User.findOne({ email }); 10 | if (!user) { 11 | return res 12 | .status(404) 13 | .json({ errors: "Please try to login with correct Credential" }); 14 | } 15 | 16 | const passwordCompare = await bcrypt.compare(password, user.password); 17 | if (!passwordCompare) { 18 | return res 19 | .status(404) 20 | .json({ errors: "Please try to login with correct Credential" }); 21 | } 22 | const data = { 23 | user: { 24 | id: user.id, 25 | }, 26 | }; 27 | let userId = data.user.id; 28 | const user1 = await User.findById(userId).select("-password"); 29 | const authtoken = jwt.sign(data, JWT_SECRECT); 30 | // console.log(jwtData) 31 | var success = true; 32 | res.json({ success,authtoken,img:user1.img,name:user1.name,id:data.user.id }); 33 | // res.json(data.user); 34 | } catch (error) { 35 | console.log(error.message); 36 | res.status(500).send("Enternal sever error"); 37 | } 38 | } 39 | 40 | module.exports = login; 41 | -------------------------------------------------------------------------------- /router/auth.js: -------------------------------------------------------------------------------- 1 | // importing the modules for the routes 2 | const express = require("express"); 3 | const router = express.Router(); 4 | // const crypto = require('crypto') 5 | const {body} = require("express-validator"); 6 | var fetchUser = require("../middleware/fetchUser"); 7 | // const User = require("../models/User"); 8 | 9 | // const jwt = require('jsonwebtoken') 10 | 11 | // requirement of the controllers 12 | const loged = require("../controllers/auth/signin"); 13 | const login = require("../controllers/auth/login"); 14 | const id = require("../controllers/auth/id"); 15 | const forget = require("../controllers/auth/forgot"); 16 | const newpass = require("../controllers/auth/n-password"); 17 | const paid = require("../controllers/auth/payment"); 18 | 19 | 20 | 21 | // this is to create a new user 22 | // using the loged controller 23 | router.post( 24 | "/signin", 25 | [ 26 | body("name", "Enter a valid Name").isLength({ min: 3 }), 27 | body("email", "Enter a valid Email").isEmail(), 28 | body("password", "Password must be at least 5 characters").isLength({min: 5,}),], 29 | loged 30 | ); 31 | 32 | // this is to login in the account 33 | // using the login controller 34 | router.post('/login', [ 35 | body("password", "Password Cannot be blank").exists(), 36 | body("email", "Enter a valid Email").isEmail(),], 37 | login 38 | ); 39 | 40 | // this is to get the user details 41 | // using the id controller 42 | router.get("/getuser", fetchUser, id); 43 | 44 | // this is to send the email to the user 45 | // using the forget controller 46 | router.post('/reset-password', forget); 47 | 48 | // this is to reset the password 49 | // using the newpass controller 50 | router.post('/new-password/:token', newpass) 51 | 52 | 53 | // this is to update the payment details 54 | 55 | router.post('/payment',fetchUser, paid) 56 | 57 | 58 | module.exports = router; 59 | -------------------------------------------------------------------------------- /controllers/auth/signin.js: -------------------------------------------------------------------------------- 1 | const User = require("../../models/User"); 2 | const bcrypt = require("bcryptjs"); 3 | const jwt = require("jsonwebtoken"); 4 | const JWT_SECRET = "Aditya"; 5 | 6 | const loged = async (req, res) => { 7 | let user = await User.findOne({ email: req.body.email }); 8 | 9 | if (user) { 10 | return res 11 | .status(400) 12 | .json({ error: "Sorry, a user with this email already exists." }); 13 | } 14 | 15 | let name = await User.findOne({ name: req.body.name }); 16 | if (name) { 17 | return res 18 | .status(400) 19 | .json({ error: "Sorry, a user with this username already exists." }); 20 | } 21 | 22 | try { 23 | // Hashing the password 24 | const password = req.body.password; 25 | const salt = await bcrypt.genSalt(10); 26 | const nPass = await bcrypt.hash(password, salt); 27 | 28 | // Generating a unique avatar URL using Dicebear 29 | // const seed = Math.floor(Math.random() * 5000); 30 | const img = `https://robohash.org/${req.body.email}.png`; 31 | // https://api.dicebear.com/9.x/pixel-art/svg//${seed}.svg`; 32 | // https://api.dicebear.com/9.x/pixel-art/svg/4566 33 | // Creating the user and storing it in the database 34 | user = await User.create({ 35 | name: req.body.name, 36 | email: req.body.email, 37 | password: nPass, 38 | img: img, 39 | }); 40 | 41 | // Generating a JWT token 42 | const data = { 43 | user: { 44 | id: user.id, 45 | }, 46 | }; 47 | const token = jwt.sign(data, JWT_SECRET); 48 | const success = true; 49 | 50 | // Sending the response with the token and user information 51 | res.status(200).json({ success, token, img, name: req.body.name }); 52 | } catch (error) { 53 | console.log(error); 54 | res.status(500).json({ error: "Internal Server Error" }); 55 | } 56 | }; 57 | 58 | module.exports = loged; 59 | -------------------------------------------------------------------------------- /controllers/auth/forgot.js: -------------------------------------------------------------------------------- 1 | const User = require("../../models/User"); 2 | const crypto = require("crypto"); 3 | const nodemailer = require('nodemailer') 4 | // let aws = require("@aws-sdk/client-ses"); 5 | // let { defaultProvider } = require("@aws-sdk/credential-provider-node"); 6 | // const sendgridTransport = require('nodemailer-sendgrid-transport') 7 | 8 | var transporter = nodemailer.createTransport({ 9 | service: 'gmail', 10 | auth: { 11 | user: 'adityapandeyadp@gmail.com', 12 | pass: 'jonyjony123' 13 | } 14 | }); 15 | 16 | // const ses = new aws.SES({ 17 | // apiVersion: "2010-12-01", 18 | // region: "us-east-1", 19 | // defaultProvider, 20 | // }); 21 | 22 | // // create Nodemailer SES transporter 23 | // let transporter = nodemailer.createTransport({ 24 | // SES: { ses, aws }, 25 | // }); 26 | 27 | const forget = async (req,res)=>{ 28 | crypto.randomBytes(32,(err,buffer)=>{ 29 | if(err){ 30 | console.log(err) 31 | } 32 | const token = buffer.toString("hex") 33 | User.findOne({email:req.body.email}) 34 | .then(user=>{ 35 | if(!user){ 36 | return res.status(422).json({error:"User dont exists with that email"}) 37 | } 38 | user.resetToken = token 39 | user.expireToken = Date.now() + 3600000 40 | const host = 'http://localhost:5000' 41 | user.save().then((result)=>{ 42 | transporter.sendMail({ 43 | to:user.email, 44 | from:"adityapandeyadp@gmail.com", 45 | subject:"password reset", 46 | html:` 47 |

You requested for password reset

48 |
click in this link to reset password
49 | ` 50 | }) 51 | res.json({message:"check your email"}) 52 | }) 53 | 54 | }) 55 | }) 56 | } 57 | 58 | module.exports = forget; --------------------------------------------------------------------------------