Skip to content

Commit 1a7de73

Browse files
Viktoriia ShevchenkoViktoriia Shevchenko
authored andcommitted
started backend inplementation
1 parent 7335e38 commit 1a7de73

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

backend/.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DB_NAME=your_database_name
2+
DB_USER=your_database_user
3+
DB_PASS=your_database_password
4+
DB_HOST=localhost
5+
DB_DIALECT=postgres
6+
PORT=5000
7+
JWT_SECRET=your_secret_key
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const jwt = require("jsonwebtoken");
2+
3+
exports.authenticateUser = (req, res, next) => {
4+
const token = req.header("Authorization");
5+
if (!token) return res.status(401).json({ message: "Access Denied: No Token Provided" });
6+
7+
try {
8+
const decoded = jwt.verify(token, process.env.JWT_SECRET);
9+
req.user = decoded; // Attach user data to request
10+
next();
11+
} catch (error) {
12+
res.status(400).json({ message: "Invalid Token" });
13+
}
14+
};
15+
16+
// Role-based authorization
17+
exports.authorizeRoles = (...roles) => {
18+
return (req, res, next) => {
19+
if (!roles.includes(req.user.role)) {
20+
return res.status(403).json({ message: "Access Denied: Insufficient Permissions" });
21+
}
22+
next();
23+
};
24+
};

backend/routes/authRoutes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Router } from "express";
2+
const router = Router();
3+
4+
router.post("/login", (req, res) => {
5+
res.send("Login route");
6+
});
7+
8+
export default router;

backend/routes/protectedRoutes.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const express = require("express");
2+
const { authenticateUser, authorizeRoles } = require("../middlewares/authMiddleware");
3+
const router = express.Router();
4+
5+
// Admin-only route
6+
router.get("/admin", authenticateUser, authorizeRoles("admin"), (req, res) => {
7+
res.json({ message: "Welcome, Admin!" });
8+
});
9+
10+
// Beauty master-only route
11+
router.get("/beauty_master", authenticateUser, authorizeRoles("beauty_master"), (req, res) => {
12+
res.json({ message: "Welcome, Beauty Master!" });
13+
});
14+
15+
// Customer-only route
16+
router.get("/customer", authenticateUser, authorizeRoles("customer"), (req, res) => {
17+
res.json({ message: "Welcome, Customer!" });
18+
});
19+
20+
module.exports = router;

backend/server.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import bodyParser = require("body-parser");
2+
import authRoutes from "./routes/authRoutes";
3+
import { Sequelize } from "sequelize";
4+
const dotenv = require("dotenv");
5+
6+
7+
8+
dotenv.config();
9+
10+
const sequelize = new Sequelize(process.env.DATABASE_URL || '', {
11+
dialect: 'postgres',
12+
protocol: 'postgres',
13+
dialectOptions: {
14+
ssl: {
15+
require: true,
16+
rejectUnauthorized: false,
17+
},
18+
},
19+
logging: false,
20+
});
21+
22+
const app = express();
23+
app.use(bodyParser.json());
24+
25+
// Routes
26+
app.use("/api/auth", authRoutes);
27+
28+
29+
// Database Sync and Server Start
30+
sequelize
31+
.sync()
32+
.then(() => {
33+
app.listen(5000, () => console.log("Server running on port 5000"));
34+
})
35+
.catch((err) => console.log(err));

backend/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "CommonJS",
5+
"outDir": "./dist",
6+
"rootDir": "./src",
7+
"strict": true,
8+
"esModuleInterop": true
9+
}
10+
}

0 commit comments

Comments
 (0)