Skip to content

Commit 5e7645d

Browse files
committed
feat: create authentication middleware
1 parent 4622bb6 commit 5e7645d

File tree

8 files changed

+37
-12
lines changed

8 files changed

+37
-12
lines changed

.env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ DB_USER=
33
DB_PASS=
44
DB_PORT=
55
DB_HOST=
6-
DB_NAME=
6+
DB_NAME=
7+
JWT_SECRET=

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
"cors": "^2.8.5",
2525
"dotenv": "^10.0.0",
2626
"express": "^4.17.1",
27+
"jsonwebtoken": "^8.5.1",
2728
"pg": "^8.7.1"
2829
},
2930
"devDependencies": {
3031
"@types/cors": "^2.8.12",
3132
"@types/dotenv": "^8.2.0",
3233
"@types/express": "^4.17.13",
34+
"@types/jsonwebtoken": "^8.5.6",
3335
"@types/node": "^16.11.12",
3436
"@types/pg": "^8.6.1",
3537
"@typescript-eslint/eslint-plugin": "^5.6.0",

src/app.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import express from 'express';
22
import cors from 'cors';
33
import router from './router';
4+
import errorMiddleware from './middlewares/errorMiddleware';
45

56
const app = express();
67

78
app.use(cors());
89
app.use(express.json());
910
app.use(router);
11+
app.use(errorMiddleware);
1012

1113
export default app;

src/controllers/usersController.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response, NextFunction } from 'express';
22

3-
export async function postUser(req: Request, res: Response, next: NextFunction) {
3+
export async function createUser(req: Request, res: Response, next: NextFunction) {
44

55
}

src/enums/httpStatusCode.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export default Object.freeze({
2-
OK: 200,
3-
CREATED: 201,
4-
NO_CONTENT: 204,
5-
BAD_REQUEST: 400,
6-
NOT_FOUND: 404,
7-
CONFLICT: 409,
8-
INTERNAL_SERVER_ERROR: 500,
9-
});
10-
2+
OK: 200,
3+
CREATED: 201,
4+
NO_CONTENT: 204,
5+
BAD_REQUEST: 400,
6+
UNAUTHORIZED: 401,
7+
NOT_FOUND: 404,
8+
CONFLICT: 409,
9+
INTERNAL_SERVER_ERROR: 500,
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Response, NextFunction } from 'express';
2+
import jwt from 'jsonwebtoken';
3+
import httpStatusCode from '../enums/httpStatusCode';
4+
import RequestAuthentication from '../protocols/IRequestAuthentication';
5+
6+
export default async function authenticationMiddleware(req: RequestAuthentication, res: Response, next: NextFunction) {
7+
const authorization = req.header('Authorization');
8+
const token = authorization?.replace('Bearer ', '');
9+
10+
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
11+
if (err) return res.sendStatus(httpStatusCode.UNAUTHORIZED);
12+
req.userId = decoded.userId;
13+
return next();
14+
});
15+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Request } from 'express';
2+
3+
export default interface RequestAuthentication extends Request {
4+
userId: number,
5+
}

src/routers/usersRouter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import * as usersController from '../controllers/usersController';
33

44
const router: Router = Router();
55

6-
export default router.post('/', usersController.postUser);
6+
export default router.post('/', usersController.createUser);

0 commit comments

Comments
 (0)