Skip to content

Commit c8c1f21

Browse files
Nisreen OweidatNisreen Oweidat
authored andcommitted
add JWT , auth for login/sign in and gust
1 parent f2be01c commit c8c1f21

File tree

8 files changed

+2205
-251
lines changed

8 files changed

+2205
-251
lines changed

src/app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ const logger = require("morgan");
55
const swaggerUi = require("swagger-ui-express");
66
const YAML = require("yamljs");
77
const swaggerDocument = YAML.load("./docs/swagger.yaml");
8-
8+
const bcrypt = require("bcrypt");
9+
const jwt = require("jsonwebtoken");
910

1011
const indexRouter = require("./routes/index");
1112
const pokemonRouter = require("./routes/pokemon");
13+
const authRouter = require("./routes/auth");
14+
1215

1316
const app = express();
1417

@@ -19,6 +22,7 @@ app.use(cookieParser());
1922
app.use(express.static(path.join(__dirname, "public")));
2023

2124
app.use("/", indexRouter);
25+
app.use("/auth", authRouter);
2226
app.use("/pokemon", pokemonRouter);
2327
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
2428

src/controllers/pokemon.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
const x = require("../db/pokedex.json");
3-
const pokedex = x;
2+
const pokedex = require("../db/pokedex.json");
43

54

5+
//=======================get/begin=======================//
6+
67
exports.getHp = (req, res) => {
78
const { gte, lte, gt, lt } = req.query;
89

@@ -29,7 +30,7 @@ exports.getType = (req ,res) => {
2930
}
3031
});
3132

32-
if (pokemon.length === 0) {
33+
if (!pokemon) {
3334
res.status(404).json({ error: "Pokemon not found" });
3435
return;
3536
}
@@ -58,4 +59,39 @@ exports.getId = (req, res) => {
5859
}
5960
res.json(pokemon);
6061
};
62+
//=======================get/ends=======================//
63+
64+
65+
//delete
66+
67+
exports.deletePokemon = (req, res) => {
68+
const id = req.params.id;
69+
const index = pokedex.findIndex(p => p.id == id);
70+
if (index === -1) {
71+
return res.status(404).json({ error: "Pokemon not found" });
72+
}
73+
pokedex.splice(index, 1);
74+
res.json({ message: "Pokemon deleted successfully" });
75+
};
76+
77+
78+
// //update
79+
80+
exports.updatePokemon = (req, res) => {
81+
const id = req.params.id;
82+
const index = pokedex.findIndex(p => p.id == id);
83+
if (index === -1) {
84+
return res.status(404).json({ error: "Pokemon not found" });
85+
}
86+
pokedex[index] = { ...pokedex[index], ...req.body };
87+
res.json(pokedex[index]);
88+
};
89+
90+
91+
// create
6192

93+
exports.createPokemon = (req, res) => {
94+
const newPokemon = req.body;
95+
pokedex.push(newPokemon);
96+
res.status(201).json(newPokemon);
97+
}

src/middleware/authMiddleware.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
const jwt = require("jsonwebtoken");
4+
5+
const SECRET_KEY = "your_secret_key";
6+
7+
8+
exports.authenticateUser = (req, res, next) => {
9+
const token = req.header("Authorization");
10+
11+
if (!token) {
12+
return res.status(403).json({ error: "Access denied" });
13+
}
14+
15+
try {
16+
const decoded = jwt.verify(token.replace("Bearer ", ""), SECRET_KEY);
17+
req.user = decoded;
18+
next();
19+
} catch (err) {
20+
res.status(401).json({ error: "Invalid token" });
21+
}
22+
};

0 commit comments

Comments
 (0)