initial commit - far from runnable

This commit is contained in:
Anika Raemer 2025-09-21 12:39:54 +02:00
commit db057ce342
8614 changed files with 1032171 additions and 0 deletions

26
src/data-source.ts Normal file
View file

@ -0,0 +1,26 @@
import "reflect-metadata";
import { DataSource } from "typeorm";
import * as dotenv from "dotenv";
import { UserEntity } from "./entities/UserEntity";
dotenv.config();
const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_DATABASE, NODE_ENV } =
process.env;
export const AppDataSource = new DataSource({
type: "postgres",
host: DB_HOST,
port: parseInt(DB_PORT || "5432"),
username: DB_USERNAME,
password: DB_PASSWORD,
database: DB_DATABASE,
synchronize: NODE_ENV === "dev" ? false : false,
//logging logs sql command on the terminal
logging: NODE_ENV === "dev" ? false : false,
entities: [UserEntity],
migrations: [__dirname + "/migration/*.ts"],
subscribers: [],
});

0
src/dtos/AbstractDto.ts Normal file
View file

8
src/dtos/UserDto.ts Normal file
View file

@ -0,0 +1,8 @@
export class UserDto {
id: string;
firstName: string;
lastName: string;
userName: string;
email: string;
role: string;
}

View file

View file

View file

View file

View file

@ -0,0 +1,37 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from "typeorm";
@Entity({ name: "user" })
export class UserEntity {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ nullable: false })
userName: string;
@Column({ nullable: false })
email: string;
@Column({ nullable: false })
password: string;
@Column({ nullable: true })
firstName: string;
@Column({ nullable: true })
lastName: string;
@Column({ default: "user" })
role: string;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}

34
src/index.ts Normal file
View file

@ -0,0 +1,34 @@
import { AppDataSource } from "./data-source"
import * as express from "express";
import * as dotenv from "dotenv";
import { Request, Response } from "express";
import { authPoint } from "./endpoints/authPoint";
import { userPoint } from "./endpoints/userPoint";
import { recipePoint } from "./endpoints/recipePoint";
import {errorHandler} from "./middleware/errorHandler"
import "reflect-metadata";
dotenv.config();
const app = express();
app.use(express.json());
app.use(errorHandler);
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 4000;
const HOST = process.env.HOST || "localhost";
app.use("/auth", authPoint);
app.use("/user", userPoint);
app.use("/recipe", recipePoint);
app.get("*", (req: Request, res: Response) => {
res.status(505).json({ message: "Bad Request" });
});
AppDataSource.initialize()
.then(async () => {
app.listen(PORT, HOST, () => {
console.log(`Server is running on http://${HOST}:${PORT}`);
});
console.log("Data Source has been initialized!");
})
.catch((error) => console.log(error));

View file

View file

View file

@ -0,0 +1,25 @@
import { NextFunction, Request, Response } from "express";
import * as jwt from "jsonwebtoken";
import * as dotenv from "dotenv";
dotenv.config();
export const authentication = (
req: Request,
res: Response,
next: NextFunction
) => {
const header = req.headers.authorization;
if (!header) {
return res.status(401).json({ message: "Unauthorized" });
}
const token = header.split(" ")[1];
if (!token) {
return res.status(401).json({ message: "Unauthorized" });
}
const decode = jwt.verify(token, process.env.JWT_SECRET);
if (!decode) {
return res.status(401).json({ message: "Unauthorized" });
}
req[" currentUser"] = decode;
next();
};

View file

@ -0,0 +1,17 @@
import { NextFunction, Request, Response } from "express";
import { AppDataSource } from "../data-source";
import { UserEntity } from "../entities/UserEntity";
export const authorization = (roles: string[]) => {
return async (req: Request, res: Response, next: NextFunction) => {
const userRepo = AppDataSource.getRepository(UserEntity);
const user = await userRepo.findOne({
where: { id: req[" currentUser"].id },
});
console.log(user);
if (!roles.includes(user.role)) {
return res.status(403).json({ message: "Forbidden" });
}
next();
};
};

View file

@ -0,0 +1,11 @@
import { NextFunction, Request, Response } from "express";
export const errorHandler = (
error: Error,
req: Request,
res: Response,
next: NextFunction
) => {
console.error(`Error: ${error.message}`);
return res.status(500).json({ message: "Internal server error" });
};

View file

@ -0,0 +1,19 @@
import * as jwt from "jsonwebtoken";
import * as bcrypt from "bcrypt";
import * as dotenv from "dotenv";
import { payload } from "../dtos/userDto";
dotenv.config();
const { JWT_SECRET = "" } = process.env;
export class encrypt {
static async encryptpass(password: string) {
return bcrypt.hashSync(password, 12);
}
static comparepassword(hashPassword: string, password: string) {
return bcrypt.compareSync(password, hashPassword);
}
static generateToken(payload: payload) {
return jwt.sign(payload, JWT_SECRET, { expiresIn: "1d" });
}
}