-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (145 loc) · 4.82 KB
/
index.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 jwt = require('jsonwebtoken');
const fs = require('fs');
const app = express();
app.use(express.json());
let ADMINS = [];
let USERS = [];
let COURSES = [];
try {
ADMINS = JSON.parse(fs.readFileSync('admins.json', 'utf8'));
USERS = JSON.parse(fs.readFileSync('users.json', 'utf8'));
COURSES = JSON.parse(fs.readFileSync('courses.json', 'utf8'));
} catch {
ADMINS = [];
USERS = [];
COURSES = [];
}
const SECRET = 'dvwy8qwh';
const authenticateJwt = (req, res, next) => {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.split(' ')[1];
console.log(token);
jwt.verify(token, SECRET, (err, user) => {
if (err) {
res.sendStatus(403);
}
req.user = user;
next();
});
} else {
res.sendStatus(401);
}
};
// Admin routes
app.post('/admin/signup', (req, res) => {
// logic to sign up admin
const { username, password } = req.body;
const admin = ADMINS.find(a => a.username === username);
if (admin) {
res.status(403).json({ message: 'Admin already exists' })
} else {
newadmin = { username, password };
ADMINS.push(newadmin);
fs.writeFileSync('admins.json', JSON.stringify(ADMINS));
const token = jwt.sign({ username, role: 'admin' }, SECRET, { expiresIn: '1h' });
res.json({ message: 'Admin created successfully', token });
}
});
app.post('/admin/login', (req, res) => {
// logic to log in admin
const { username, password } = req.body;
const admin = ADMINS.find(a => a.username === username && a.password === 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, (req, res) => {
// logic to create a course
const course = req.body;
course.id = COURSES.length + 1;
COURSES.push(course);
fs.writeFileSync('courses.json', JSON.stringify(COURSES));
res.json({ message: 'Course created successfully', courseId: course.id });
});
app.put('/admin/courses/:courseId', authenticateJwt, (req, res) => {
// logic to edit a course
const course = COURSES.find(c => c.id === parseInt(req.params.courseId));
if (course) {
Object.assign(course, req.body);
fs.writeFileSync('courses.json', JSON.stringify(COURSES));
res.json({ message: "Course updated successfully." });
} else {
res.status(404).json({ message: 'Course not found' });
}
});
app.get('/admin/courses', authenticateJwt, (req, res) => {
// logic to get all courses
res.json({ courses: COURSES });
});
// User routes
app.post('/users/signup', (req, res) => {
// logic to sign up user
const {username, password} = req.body;
const user = USERS.find(u => u.username === username);
if(user){
res.status(403).json({message: 'User already exists'});
}
else{
const newUser = {username, password};
USERS.push(newUser);
fs.writeFileSync('users.json', JSON.stringify(USERS));
const token = jwt.sign({username, role: 'user'}, SECRET, {expiresIn: '1h'});
res.json({message: 'User created successfully', token});
}
});
app.post('/users/login', (req, res) => {
// logic to log in user
const { username, password } = req.body;
const user = USERS.find(u => u.username === username && u.password === 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, (req, res) => {
// logic to list all courses
res.json({courses: COURSES});
});
app.post('/users/courses/:courseId', authenticateJwt, (req, res) => {
// logic to purchase a course
const course = COURSES.find(c => c.id === parseInt(req.params.courseId));
if (course) {
const user = USERS.find(u => u.username === req.user.username);
if (user) {
if (!user.purchasedCourses) {
user.purchasedCourses = [];
}
user.purchasedCourses.push(course);
fs.writeFileSync('users.json', JSON.stringify(USERS));
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, (req, res) => {
// logic to view purchased courses
const user = USERS.find(u => u.username === req.user.username);
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');
});