Added Login + Register Frontend and Authorization
This commit is contained in:
+9
-4
@@ -2,15 +2,17 @@ const express = require("express");
|
||||
const app = express();
|
||||
const swaggerUi = require("swagger-ui-express");
|
||||
const swaggerDocument = require("./swagger.json");
|
||||
|
||||
const path = require("path");
|
||||
app.use(express.static(path.join(__dirname, "public")));
|
||||
const fs = require("fs");
|
||||
const dotenv = require("dotenv").config();
|
||||
const jwt = require("jsonwebtoken");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const port = 3000;
|
||||
|
||||
app.use(express.json());
|
||||
app.use("/swagger-ui", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
||||
app.use(cookieParser());
|
||||
|
||||
function get_data() {
|
||||
const dataPath = path.join(__dirname, "data.json");
|
||||
@@ -39,9 +41,9 @@ function authenticateToken(request, response, next) {
|
||||
/* #swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}] */
|
||||
const authHeader = request.headers.authorization;
|
||||
console.log(authHeader);
|
||||
const token = authHeader && authHeader.split(" ")[1];
|
||||
const token =
|
||||
request.headers.authorization?.split(" ")[1] ||
|
||||
request.cookies.authorization;
|
||||
|
||||
if (token == null) return response.sendStatus(401);
|
||||
|
||||
@@ -76,6 +78,9 @@ app.post("/register", (request, response) => {
|
||||
response.json({ username, token });
|
||||
});
|
||||
|
||||
app.get("/login", (request, response) => {
|
||||
response.sendFile(path.join(__dirname, ".\\public\\login.html"));
|
||||
})
|
||||
app.post("/login", (request, response) => {
|
||||
const { username, password } = request.body;
|
||||
if (!username && !password)
|
||||
|
||||
Reference in New Issue
Block a user