Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=9000
JWT_SECRET=your_jwt_secret_here
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"id": 1,
"name": "David Miller",
"username": "dad_miller",
"_password": "parent456",
"role": "parent",
"passwordHash": "$2a$10$V4nCDj9z1mpYmhilx43xy.Wl9whaKMmSMSpL4qXmrS.Z73JSq7vRe"
},
{
"id": 2,
"name": "Sarah Miller",
"username": "mom_miller",
"_password": "parent123",
"role": "parent",
"passwordHash": "$2a$10$Dct9yQRohwdBoW1MEiWfnOnnVWIOQoWAeJllJwPv34r.xqIUFKKNG"
},
{
"id": 3,
"name": "Jake Miller",
"username": "jake_miller",
"_password": "child123",
"role": "child",
"passwordHash": "$2a$10$SWOT8Vt10NXOkmVsz/kYWeDT1qa0a.6VajRe.Gf9RcPZjV3oZLaou"
},
{
"id": 4,
"name": "Sophie Miller",
"username": "sophie_miller",
"_password": "child456",
"role": "child",
"passwordHash": "$2a$10$hqW9lPmajKUKJwBJ2ByMEeB4QewQ4VPYnxMPtwXZ2FFCZUdmaShOq"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"1": [
{
"id": 1,
"title": "The Dark Knight",
"genre": "Action",
"watched": true
},
{
"id": 2,
"title": "Inception",
"genre": "Sci-Fi",
"watched": false
}
],
"2": [
{
"id": 1,
"title": "Mamma Mia!",
"genre": "Musical",
"watched": true
},
{
"id": 2,
"title": "The Notebook",
"genre": "Romance",
"watched": false
}
],
"3": [
{
"id": 2,
"title": "Avengers: Endgame",
"genre": "Action",
"watched": false
},
{
"id": 3,
"title": "Matrix World",
"genre": "Action",
"watched": false
}
],
"4": [
{
"id": 1,
"title": "Frozen",
"genre": "Animation",
"watched": true
},
{
"id": 2,
"title": "Moana",
"genre": "Animation",
"watched": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const express = require("express");
require("dotenv").config();
const helmet = require("helmet");

const authRoutes = require("./routes/auth");
const watchlistRoutes = require("./routes/watchlist");

const PORT = process.env.PORT || 9000;
const app = express();

app.use(helmet());
app.use(express.json());

app.get("/", (req, res) => {
res.json({ message: "Family Movie Watchlist API" });
});

app.use("/api/auth", authRoutes);
app.use("/api/watchlist", watchlistRoutes);

app.listen(PORT, () => {
console.log(`Server running on port ${PORT}...`);
});

module.exports = app;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { verifyToken } = require('../utils/jwt');

function authenticate(req, res, next) {
const authHeader = req.headers.authorization;

if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ message: 'No token provided' });
}

const token = authHeader.split(' ')[1];
const decoded = verifyToken(token);

if (!decoded) {
return res.status(401).json({ message: 'Invalid or expired token' });
}

req.user = decoded;
next();
}

module.exports = authenticate;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function authorizeModification(req, res, next) {
const requestedUserId = parseInt(req.params.userId);
const { role, id } = req.user;

if (role === 'parent') {
return next();
}

if (role === 'child' && id === requestedUserId) {
return next();
}

return res.status(403).json({ message: 'Access denied' });
}

module.exports = { authorizeModification };
Loading