diff --git a/Server/tests/test6/.gitignore b/Server/tests/test6/.gitignore new file mode 100644 index 0000000..f52e6f5 --- /dev/null +++ b/Server/tests/test6/.gitignore @@ -0,0 +1,2 @@ +node_modules +pnpm-lock.yaml \ No newline at end of file diff --git a/Server/tests/test6/index.js b/Server/tests/test6/index.js new file mode 100644 index 0000000..7395963 --- /dev/null +++ b/Server/tests/test6/index.js @@ -0,0 +1,54 @@ +const express = require("express"); +const session = require("express-session"); +const bodyParser = require("body-parser"); + +const app = express(); +const port = 3000; + +app.use(bodyParser.urlencoded({ extended: false })) +app.use(bodyParser.json()) +app.use(session({ + secret: "hello kitty", + resave: false, + saveUninitialized: true, + cookie: { secret: false } +})) + +const isAuthenticated = (req, res, next) => { + if (req.session.user) + next(); + else + res.status(401).send("You are not authenticated!"); +} + +app.post("/login", (req, res) => { + const { username, password } = req.body; + if (username === "admin" && password === "password") { + req.session.user = { id: 1, username }; + res.send("User logged in"); + } else { + res.status(401).send("Invaild credentials"); + } +}) + +app.post("/logout", (req, res) => { + req.session.destroy((error) => { + if (error) + return res.status(500).send("Could not log out."); + res.send("Logout successful") + }) +}) + +app.get("/protected", isAuthenticated, (req, res) => { + res.send("This is a protected route"); +}) + +app.get("/profile", isAuthenticated, (req, res) => { + res.send(`Welcome ${req.session.user.username}`); +}) + +app.listen(port, (error) => { + if (error) + console.log(error); + console.log(`server is running: http://localhost:${port}`); +}) diff --git a/Server/tests/test6/index2.js b/Server/tests/test6/index2.js new file mode 100644 index 0000000..95e3016 --- /dev/null +++ b/Server/tests/test6/index2.js @@ -0,0 +1,52 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const jwt = require("jsonwebtoken"); + +const app = express(); + +app.use(bodyParser.json()); + +const generateToken = (user) => { + return jwt.sign({ user }, "hello kitty", { expiresIn: "1h" }) +} + +const authenticateToken = (req, res, next) => { + const authHeader = req.headers['authorization']; + const token = authHeader && authHeader.split(" ")[1]; + + if (token == null) + return res.sendStatus(401); + + jwt.verify(token, "hello kitty", (error, user) => { + if (error) + return res.sendStatus(401); + + req.user = user; + next(); + }) +} + +app.post('/jwt-login', (req, res) => { + const { username, password } = req.body; + if (username == "admin" && password == "password") { + const user = { id: 1, username }; + const accessToken = generateToken(user); + res.json(accessToken); + } else { + res.status(401).send("Invaild credentials") + } +}) + +app.post("/jwt-logout", authenticateToken, (req, res) => { + res.send("Logged out successful"); +}) + +app.get("/jwt-profile", authenticateToken, (req, res) => { + res.send(`Welcome ${req.user.user.username}`) +}) + +app.listen(3000, (error) => { + if (error) + console.log(error); + console.log("Server is running: http://localhost:3000"); +}) \ No newline at end of file diff --git a/Server/tests/test6/package.json b/Server/tests/test6/package.json new file mode 100644 index 0000000..d213beb --- /dev/null +++ b/Server/tests/test6/package.json @@ -0,0 +1,19 @@ +{ + "name": "test6", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "packageManager": "pnpm@10.14.0", + "dependencies": { + "body-parser": "^2.2.1", + "express": "^5.1.0", + "express-session": "^1.18.2", + "jsonwebtoken": "^9.0.2" + } +}