started login
This commit is contained in:
+102
-6
@@ -1,16 +1,112 @@
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const swaggerUi = require('swagger-ui-express');
|
||||
const swaggerDocument = require('./swagger.json');
|
||||
const swaggerUi = require("swagger-ui-express");
|
||||
const swaggerDocument = require("./swagger.json");
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const dotenv = require("dotenv").config();
|
||||
const jwt = require("jsonwebtoken");
|
||||
const port = 3000;
|
||||
|
||||
app.use('/swagger-ui', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
||||
app.use(express.json());
|
||||
app.use("/swagger-ui", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
||||
|
||||
app.get("/", (request, respond) => {
|
||||
respond.sendFile(path.join(__dirname, 'structure.png'));
|
||||
function get_data() {
|
||||
const dataPath = path.join(__dirname, "data.json");
|
||||
if (!fs.existsSync(dataPath)) {
|
||||
fs.writeFileSync(dataPath, JSON.stringify([]));
|
||||
}
|
||||
const data = fs.readFileSync(dataPath);
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
function save_data(data) {
|
||||
fs.writeFileSync(path.join(__dirname, "data.json"), JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
function saveUser(user) {
|
||||
const users = get_data();
|
||||
users.push(user);
|
||||
save_data(users);
|
||||
}
|
||||
|
||||
function generateAccessToken(user) {
|
||||
return jwt.sign(user, process.env.TOKEN_SECRET, { expiresIn: "1800s" });
|
||||
}
|
||||
|
||||
function authenticateToken(request, response, next) {
|
||||
const authHeader = request.headers["authorization"];
|
||||
const token = authHeader && authHeader.split(" ")[1];
|
||||
|
||||
if (token == null) return response.sendStatus(401);
|
||||
|
||||
jwt.verify(token, process.env.TOKEN_SECRET, (err, user) => {
|
||||
if (err) return response.sendStatus(403);
|
||||
|
||||
const users = get_data();
|
||||
const foundUser = users.find(u => u.username === user.username);
|
||||
if (!foundUser || foundUser.token !== token) {
|
||||
return response.status(403).json({ error: "Token has been revoked or is invalid" });
|
||||
}
|
||||
|
||||
request.user = user;
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
app.post("/register", (request, response) => {
|
||||
const { username } = request.body;
|
||||
if (!username)
|
||||
return response.status(400).json({ error: "Username is required" });
|
||||
|
||||
const users = get_data();
|
||||
if (users.some(u => u.username === username)) {
|
||||
return response.status(400).json({ error: "Username already exists" });
|
||||
}
|
||||
|
||||
const token = generateAccessToken({ username });
|
||||
saveUser({ username, token });
|
||||
|
||||
response.json({ username, token });
|
||||
});
|
||||
|
||||
app.post("/login", (request, response) => {
|
||||
const { username } = request.body;
|
||||
if (!username)
|
||||
return response.status(400).json({ error: "Username is required" });
|
||||
|
||||
const users = get_data();
|
||||
const user = users.find(u => u.username === username);
|
||||
if (!user) return response.status(401).json({ error: "Invalid credentials" });
|
||||
|
||||
|
||||
const token = generateAccessToken({ username });
|
||||
user.token = token;
|
||||
save_data(users);
|
||||
|
||||
response.json({ username, token });
|
||||
});
|
||||
|
||||
app.get("/", authenticateToken, (request, response) => {
|
||||
response.sendFile(path.join(__dirname, "structure.png"));
|
||||
});
|
||||
|
||||
app.post("/verify", authenticateToken, (request, response) => {
|
||||
response.json({ valid: true, user: request.user });
|
||||
});
|
||||
|
||||
app.delete("/logout", authenticateToken, (request, response) => {
|
||||
const username = request.user.username;
|
||||
const users = get_data();
|
||||
const user = users.find(u => u.username === username);
|
||||
if (user) {
|
||||
delete user.token;
|
||||
save_data(users);
|
||||
}
|
||||
response.json({ message: "Logged out successfully." });
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log("Server is running on port " + port);
|
||||
})
|
||||
});
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
const crypto = require("crypto");
|
||||
|
||||
const secret = crypto.randomBytes(64).toString('hex')
|
||||
|
||||
console.log(secret)
|
||||
@@ -12,7 +12,9 @@
|
||||
},
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"dotenv": "^16.5.0",
|
||||
"express": "^5.1.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"swagger-ui-express": "^5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
+124
-2
@@ -11,12 +11,134 @@
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"/register": {
|
||||
"post": {
|
||||
"description": "",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
}
|
||||
},
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"username": {
|
||||
"example": "any"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/login": {
|
||||
"post": {
|
||||
"description": "",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized"
|
||||
}
|
||||
},
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"username": {
|
||||
"example": "any"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "authorization",
|
||||
"in": "header",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized"
|
||||
},
|
||||
"403": {
|
||||
"description": "Forbidden"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/verify": {
|
||||
"post": {
|
||||
"description": "",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "authorization",
|
||||
"in": "header",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized"
|
||||
},
|
||||
"403": {
|
||||
"description": "Forbidden"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/logout": {
|
||||
"delete": {
|
||||
"description": "",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "authorization",
|
||||
"in": "header",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK"
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized"
|
||||
},
|
||||
"403": {
|
||||
"description": "Forbidden"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user