Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easy 2 done #275

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 109 additions & 16 deletions week-3/02-course-app-easy-2/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();

app.use(express.json());
Expand All @@ -7,46 +8,138 @@ let ADMINS = [];
let USERS = [];
let COURSES = [];

//how to check if request is from login route or any other route
//soln found out at login auth token is genrated so no need to put it behind middleware
//i.e. user is loggig in for first time or already logged in
//soln make another middle ware for authentication token and attach it
const generateAuthToken = (role)=>{
const payload = {role : role.username};
console.log({payload : payload});
return token = jwt.sign(payload,'SeCret',{expiresIn:'1h'});
}

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(403);
}

req.user = user;
next();
});
} else {
res.sendStatus(401);
}
};


// Admin routes
app.post('/admin/signup', (req, res) => {
// logic to sign up admin
const admin = req.body;
const existingAdmin = ADMINS.find(a => a.username === admin.username);
if (existingAdmin) {
res.status(403).json({ message: 'Admin already exists' });
} else {
ADMINS.push(admin);
const jwToken = generateAuthToken(admin);
res.json({ message: 'Admin created successfully' ,token: jwToken});
}
});

app.post('/admin/login', (req, res) => {
// logic to log in admin
const {username,password} = req.headers;
const validAdmin = ADMINS.find(a=>a.username == username && a.password == password);
if(validAdmin){
const jwtToken = generateAuthToken();
res.json({ message: 'Logged in successfully' });
}else {
res.status(403).json({ message: 'Admin authentication failed' });
}
});

app.post('/admin/courses', (req, res) => {
// logic to create a course
app.post('/admin/courses', authenticateJwt,(req, res) => {
const course = req.body;

course.id = COURSES.length +1; // use timestamp as course ID
COURSES.push(course);
res.json({ message: 'Course created successfully', courseId: course.id });
});

app.put('/admin/courses/:courseId', (req, res) => {
// logic to edit a course
app.put('/admin/courses/:courseId',authenticateJwt, (req, res) => {
const courseId = parseInt(req.params.courseId);
const course = COURSES.find(c => c.id === courseId);
if (course) {
Object.assign(course, req.body);
res.json({ message: 'Course updated successfully' });
} else {
res.status(404).json({ message: 'Course not found' });
}
});

app.get('/admin/courses', (req, res) => {
// logic to get all courses
app.get('/admin/courses',authenticateJwt, (req, res) => {
res.json({ courses: COURSES });
});

// User routes
app.post('/users/signup', (req, res) => {
// logic to sign up user
const user = {...req.body, purchasedCourses: []};
const existingUser = USERS.find(u =>u.username === user.username);
if(existingUser){
res.status(403).json({ message: 'User already exists' });
}else{
USERS.push(user);
const token = generateAuthToken(user);
res.json({ message: 'User created successfully' });
}
});

app.post('/users/login', (req, res) => {
// logic to log in user
const { username, password } = req.headers;
const user = USERS.find(u => u.username === username && u.password === password);
if (user) {
const token = generateJwt(user);
res.json({ message: 'Logged in successfully', token });
} else {
res.status(403).json({ message: 'User authentication failed' });
}
});

app.get('/users/courses', (req, res) => {
// logic to list all courses
app.get('/users/courses',authenticateJwt, (req, res) => {
let filteredCourses = COURSES.filter(c=>c.published);
res.json({ courses: filteredCourses });
});

app.post('/users/courses/:courseId', (req, res) => {
// logic to purchase a course
app.post('/users/courses/:courseId',authenticateJwt, (req, res) => {
const courseId = parseInt(req.params.courseId);
const course = COURSES.find(c => c.id === courseId);
if (course) {
const user = USERS.find(u => u.username === req.user.username);
if (user) {
if (!user.purchasedCourses) {
user.purchasedCourses = [];
}
user.purchasedCourses.push(course);
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', (req, res) => {
// logic to view purchased courses
app.get('/users/purchasedCourses',authenticateJwt, (req, res) => {
const user = USERS.find(u => u.username === req.user.username);
if (user && user.purchasedCourses) {
res.json({ purchasedCourses: user.purchasedCourses });
} else {
res.status(404).json({ message: 'No courses purchased' });
}
});

app.listen(3000, () => {
Expand Down
6 changes: 5 additions & 1 deletion week-3/02-course-app-easy-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
},
"keywords": [],
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"express": "^4.20.0",
"jsonwebtoken": "^9.0.2"
}
}
Loading