fixing auth apply foreach route
This commit is contained in:
parent
e1c47fed89
commit
92b65e02d0
6 changed files with 28 additions and 13 deletions
|
@ -6,11 +6,7 @@ import { User, IUser } from '../models/User';
|
|||
export async function createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { firstName, lastName, email, password, address } = req.body;
|
||||
|
||||
// Hash the password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Create the user in the database
|
||||
const user: IUser = await User.create({
|
||||
firstName,
|
||||
lastName,
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import express from 'express';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
import userRouter from './routes/user';
|
||||
import ProductrRouter from './routes/product';
|
||||
|
||||
import { authenticateToken } from './middlewares/authMiddleware';
|
||||
import userRoutes from './routes/user';
|
||||
|
||||
const env = require('dotenv').config().parsed;
|
||||
|
||||
|
@ -11,6 +14,7 @@ const PORT = 3000;
|
|||
app.use(express.json());
|
||||
app.use(authenticateToken);
|
||||
|
||||
|
||||
// Connect to MongoDB using Mongoose
|
||||
mongoose.connect(env.DATABASE_URL);
|
||||
|
||||
|
@ -23,7 +27,8 @@ db.once('open', () => {
|
|||
});
|
||||
|
||||
// Routes
|
||||
app.use('/users', userRoutes);
|
||||
app.use('/users', userRouter);
|
||||
app.use('/products', ProductrRouter);
|
||||
|
||||
// Start server
|
||||
app.listen(PORT, () => {
|
||||
|
|
|
@ -5,18 +5,22 @@ interface AuthenticatedRequest extends Request {
|
|||
user?: JwtPayload | string;
|
||||
}
|
||||
|
||||
const exceptionRoutes: string[] = ['/login', '/'];
|
||||
const exceptionRoutes: string[] = ['/users/login', '/users'];
|
||||
|
||||
// Middleware function to authenticate requests
|
||||
export function authenticateToken(req: AuthenticatedRequest, res: Response, next: NextFunction) {
|
||||
const authHeader = req.headers['authorization'];
|
||||
const token = authHeader && authHeader.split(' ')[1];
|
||||
|
||||
if (!token && exceptionRoutes.includes(req.originalUrl)) {
|
||||
if(token) {
|
||||
console.log('token', token);
|
||||
}
|
||||
|
||||
if (!token && exceptionRoutes.includes(req.route)) {
|
||||
return res.sendStatus(401); // Unauthorized
|
||||
}
|
||||
|
||||
if (!exceptionRoutes.includes(req.originalUrl)) {
|
||||
if (!exceptionRoutes.includes(req.route)) {
|
||||
// Skip authentication for exception routes
|
||||
return next();
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ export interface IProduct extends Document {
|
|||
name: string;
|
||||
description: string;
|
||||
price: number;
|
||||
userId: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
|
9
src/routes/product.ts
Normal file
9
src/routes/product.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import express from 'express';
|
||||
|
||||
const ProductrRouter = express.Router();
|
||||
|
||||
ProductrRouter.post('/', (req, res) => {
|
||||
res.send('Create product');
|
||||
})
|
||||
|
||||
export default ProductrRouter;
|
|
@ -1,9 +1,9 @@
|
|||
import express from 'express';
|
||||
import { createUser, login } from '../controllers/UserController';
|
||||
|
||||
const router = express.Router();
|
||||
const userRouter = express.Router();
|
||||
|
||||
router.post('/', createUser);
|
||||
router.post('/login', login)
|
||||
userRouter.post('/', createUser);
|
||||
userRouter.post('/login', login);
|
||||
|
||||
export default router;
|
||||
export default userRouter;
|
Loading…
Reference in a new issue