add athorization to all calls except auth
This commit is contained in:
parent
8fb48f7243
commit
5dd79374c1
5 changed files with 41 additions and 12 deletions
|
|
@ -10,10 +10,14 @@ post {
|
||||||
auth: inherit
|
auth: inherit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
headers {
|
||||||
|
Authorization: bearcer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU2NGE5NjY0LTI2ZWYtNGMxMS1hNjIyLWU4MDI2MzczYmRkZCIsImlhdCI6MTc1ODc0MTM5MywiZXhwIjoxNzU4ODI3NzkzfQ.q33R9FfhGUIn92PTIIAmKmUnGxcLlv6om7KwiDD61Rc
|
||||||
|
}
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"userData": {
|
"userData": {
|
||||||
"userName": "test",
|
"userName": "test2",
|
||||||
"email": "test@raemer.net"
|
"email": "test@raemer.net"
|
||||||
},
|
},
|
||||||
"password": "test"
|
"password": "test"
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,18 @@ meta {
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
url: http://localhost:4000/login
|
url: http://localhost:4000/auth/login
|
||||||
body: none
|
body: json
|
||||||
auth: inherit
|
auth: inherit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"userName": "admin",
|
||||||
|
"password": "1J7HgWRZ2OfaiFgrKb1BULIXN"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
settings {
|
settings {
|
||||||
encodeUrl: true
|
encodeUrl: true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,9 @@ import {
|
||||||
} from "../errors/httpErrors.js";
|
} from "../errors/httpErrors.js";
|
||||||
import { LoginRequestDto } from "../dtos/LoginRequestDto.js";
|
import { LoginRequestDto } from "../dtos/LoginRequestDto.js";
|
||||||
|
|
||||||
const router = Router();
|
export const authBasicRoute = "/auth"
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
const userRepository = new UserRepository();
|
const userRepository = new UserRepository();
|
||||||
const mapper = new UserDtoEntityMapper();
|
const mapper = new UserDtoEntityMapper();
|
||||||
const authController = new AuthController(userRepository, mapper);
|
const authController = new AuthController(userRepository, mapper);
|
||||||
|
|
|
||||||
16
src/index.ts
16
src/index.ts
|
|
@ -2,10 +2,11 @@ import "reflect-metadata";
|
||||||
import express, { NextFunction, Request, Response } from "express";
|
import express, { NextFunction, Request, Response } from "express";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import { AppDataSource } from "./data-source.js";
|
import { AppDataSource } from "./data-source.js";
|
||||||
import authRoutes from "./endpoints/AuthPoint.js";
|
import authRoutes, { authBasicRoute } from "./endpoints/AuthPoint.js";
|
||||||
import userRoutes from "./endpoints/UserPoint.js";
|
import userRoutes from "./endpoints/UserPoint.js";
|
||||||
// import recipeRoutes from "./endpoints/RecipePoint.js";
|
// import recipeRoutes from "./endpoints/RecipePoint.js";
|
||||||
import { errorHandler } from "./middleware/errorHandler.js";
|
import { errorHandler } from "./middleware/errorHandler.js";
|
||||||
|
import { authentication } from "./middleware/authenticationMiddleware.js";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
|
@ -15,16 +16,19 @@ app.use(errorHandler);
|
||||||
|
|
||||||
async function startServer() {
|
async function startServer() {
|
||||||
try {
|
try {
|
||||||
// 1️⃣ Initialize database
|
// Initialize database
|
||||||
await AppDataSource.initialize();
|
await AppDataSource.initialize();
|
||||||
console.log("Data Source initialized");
|
console.log("Data Source initialized");
|
||||||
|
|
||||||
// Optional: run pending migrations
|
// Run pending migrations
|
||||||
await AppDataSource.runMigrations();
|
await AppDataSource.runMigrations();
|
||||||
console.log("Migrations executed");
|
console.log("Migrations executed");
|
||||||
|
|
||||||
// 2️⃣ Setup routes
|
// Activate Authentication
|
||||||
app.use("/auth", authRoutes);
|
app.use(authentication);
|
||||||
|
|
||||||
|
// Setup routes
|
||||||
|
app.use(authBasicRoute, authRoutes);
|
||||||
app.use("/user", userRoutes);
|
app.use("/user", userRoutes);
|
||||||
// app.use("/recipe", recipeRoutes);
|
// app.use("/recipe", recipeRoutes);
|
||||||
|
|
||||||
|
|
@ -34,7 +38,7 @@ async function startServer() {
|
||||||
});
|
});
|
||||||
console.log("Routes set up")
|
console.log("Routes set up")
|
||||||
|
|
||||||
// 3️⃣ Start listening
|
// Start listening
|
||||||
const PORT = Number(process.env.PORT) || 4000;
|
const PORT = Number(process.env.PORT) || 4000;
|
||||||
const HOST = process.env.HOST || "localhost";
|
const HOST = process.env.HOST || "localhost";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import * as jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import * as dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
|
import { authBasicRoute } from "../endpoints/AuthPoint.js";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
//@todo this seems to be clumsy... We need some propper session handling as we'll have multiple users accessing the app
|
||||||
declare global {
|
declare global {
|
||||||
namespace Express {
|
namespace Express {
|
||||||
interface Request {
|
interface Request {
|
||||||
|
|
@ -12,11 +14,22 @@ declare global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const JWT_SECRET = process.env.JWT_SECRET;
|
||||||
|
|
||||||
|
if (!JWT_SECRET) {
|
||||||
|
throw new Error("JWT_SECRET not defined");
|
||||||
|
}
|
||||||
|
|
||||||
export const authentication = (
|
export const authentication = (
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
) => {
|
) => {
|
||||||
|
// allow unauthenticated access to auth routes
|
||||||
|
if (req.path.startsWith(authBasicRoute)) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
const header = req.headers.authorization;
|
const header = req.headers.authorization;
|
||||||
if (!header) {
|
if (!header) {
|
||||||
return res.status(401).json({ message: "Unauthorized" });
|
return res.status(401).json({ message: "Unauthorized" });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue