-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseapp.js
162 lines (139 loc) · 4.72 KB
/
baseapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const app= express();
app.use(bodyParser.json());
app.use(express.json());
const SECRET= 'secrat';
const userSchema = new mongoose.Schema({
username:{type: String},// above and below same
password: String, // above and below same
purchasedCourses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }]
})
const adminSchema = new mongoose.Schema({
username: String,
password: String
});
const courseSchema = new mongoose.Schema({
title: String,
description: String,
price: Number,
imageLink: String,
published: Boolean
});
const USer = mongoose.model('User',userSchema);
const Admin = mongoose.model('Admin',adminSchema);
const Course =mongoose.model('Course',courseSchema);
const authenticateJwt =(req,res,next) =>{
const authHeader =req.headers.authorization;
if(authHeader){
const token =authHeader.split(' ')[1];
jwt.verify(token,SECRET,(err,user)=>{
if(err){
return res.sendStatus(402);
}
req.user =user;
next();
});
}else{
res.sendStatus(401);
}
};
mongoose.connect('mongodb+srv://plevenansh:[email protected]/')
// Admin route
app.post('/admin/signup', async(req, res) => {
const {username,password} =req.body;
const admin =await Admin.findOne({username,password});
if(admin){
res.json({
message:'Admin Exits'
});
}
else{
const obj ={ username: username, password: password };
const newAdmin =new Admin(obj);
newAdmin.save();
const token =jwt.sign({username,role:'admin'},SECRET,{expiresIn:'1h'});
res.json({ message: 'Admin created successfully', token });
}
});
app.post('/admin/login',async (req, res) => {
const {username,password} =req.headers;
const admin=await Admin.findOne({username,password});
if (admin) {
const token = jwt.sign({ username, role: 'admin' }, SECRET, { expiresIn: '1h' });
res.json({ message: 'Logged in successfully', token });
} else {
res.status(403).json({ message: 'Invalid username or password' });
}
});
app.post('/admin/courses',authenticateJwt, async(req, res) => {
const course = new Course(req.body);
await course.save();
res.json({ message: 'Course created successfully', courseId: course.id });
});
app.put('/admin/courses/:courseId',authenticateJwt, async (req, res) => {
const course =await Course.findByIdAndUpdate(req.params.courseId,req.body,{new:true});
if (course) {
res.json({ message: 'Course updated successfully' });
} else {
res.status(404).json({ message: 'Course not found' });
}
});
app.get('/admin/courses', authenticateJwt, async(req, res) => {
const courses = await Course.find({});
res.json({ courses });
});
// User routes
app.post('/users/signup', async (req, res) => {
const { username, password } = req.body;
const user = await User.findOne({ username });
if (user) {
res.status(403).json({ message: 'User already exists' });
} else {
const newUser = new User({ username, password });
await newUser.save();
const token = jwt.sign({ username, role: 'user' }, SECRET, { expiresIn: '1h' });
res.json({ message: 'User created successfully', token });
}
});
app.post('/users/login', async (req, res) => {
const { username, password } = req.headers;
const user = await User.findOne({ username, password });
if (user) {
const token = jwt.sign({ username, role: 'user' }, SECRET, { expiresIn: '1h' });
res.json({ message: 'Logged in successfully', token });
} else {
res.status(403).json({ message: 'Invalid username or password' });
}
});
app.get('/users/courses', authenticateJwt, async (req, res) => {
const courses = await Course.find({published: true});
res.json({ courses });
});
app.post('/users/courses/:courseId', authenticateJwt, async (req, res) => {
const course = await Course.findById(req.params.courseId);
console.log(course);
if (course) {
const user = await User.findOne({ username: req.user.username });
if (user) {
user.purchasedCourses.push(course);
await user.save();
res.json({ message: 'Course purchased successfully' });
} else {
res.status(403).json({ message: 'User not found' });
}
} else {
res.status(404).json({ message: 'Course not found' });
}
});
app.get('/users/purchasedCourses', authenticateJwt, async (req, res) => {
const user = await User.findOne({ username: req.user.username }).populate('purchasedCourses');
if (user) {
res.json({ purchasedCourses: user.purchasedCourses || [] });
} else {
res.status(403).json({ message: 'User not found' });
}
});
app.listen(3000, () => {console.log('Server is listening on port 3000');})