Implement backend structure with Docker, database schema, and user authentication

This commit is contained in:
2026-05-26 13:01:11 +02:00
parent ac9679ab27
commit 4c0c441e92
14 changed files with 313 additions and 33 deletions
+27
View File
@@ -0,0 +1,27 @@
import bodyParser from "body-parser";
import { SignJWT, jwtVerify } from "jose";
import env from "dotenv";
env.config();
const secret = new TextEncoder().encode(process.env.SECRET_KEY);
export async function generateToken(payload) {
const newToken = await new SignJWT(payload)
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setExpirationTime("24h") // Token valid for 24 hours
.sign(secret);
console.log("Generated token: ", newToken);
return newToken;
}
export async function authenticate(req, res, next) {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1]; // Bearer <token>
if (token == null) return res.sendStatus(401); // No token present
const { payload } = await jwtVerify(token, secret);
req.user = payload;
next();
}