Skip to content

Commit 78411dd

Browse files
DafloresdiazDaniel Flores Diaz
and
Daniel Flores Diaz
authored
Feat/jwt (#6)
Co-authored-by: Daniel Flores Diaz <[email protected]>
1 parent 5865c31 commit 78411dd

28 files changed

+680
-60
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SECRET_KEY = "secret_key_value"

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SECRET_KEY = "secret_key_value"

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
coverage/
33
dist/
44
.vscode/
5+
.env

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ A Microservice API based on the hexagonal architecture using NodeJS + ExpressJS
3939
```zsh
4040
npm install
4141
```
42+
### Before running the project
43+
```zsh
44+
cp .env.example .env
45+
```
4246

4347
#### Run
4448

app/internal/adapters/auth/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './jwt';

app/internal/adapters/auth/jwt.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import jsonwebtoken from "jsonwebtoken";
2+
import { defaultConfig } from "../../core/providers";
3+
4+
const generateToken = (admin_id, expires_in) => {
5+
return jsonwebtoken.sign({id: admin_id}, defaultConfig.security_key, {expiresIn:expires_in});
6+
}
7+
8+
export const VerifyToken = (req, res, next) => {
9+
const token = req.headers["authorization"] && req.headers["authorization"].split(" ")[1];
10+
if (token !== "undefined"){
11+
try {
12+
jsonwebtoken.verify(token, defaultConfig.security_key);
13+
next();
14+
} catch(err){
15+
next(err);
16+
}
17+
}else{
18+
next();
19+
}
20+
};
21+
22+
export const Token ={
23+
generateToken,
24+
};

app/internal/adapters/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MainRoute, UsersRoute} from './routes';
1+
import { MainRoute, UsersRoute, AuthRoute} from './routes';
22

33
export const Adapters = {
4-
Routes: { MainRoute, UsersRoute},
4+
Routes: { MainRoute, UsersRoute, AuthRoute},
55
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
import { AuthController } from "../../core/controllers";
3+
4+
/**
5+
* Create a new instance of UserRoutes.
6+
* @param {express.Application} app - The Express application instance.
7+
*/
8+
export const AuthRoute = (app) =>{
9+
const router = express.Router();
10+
app.use('/auth', router);
11+
12+
//Get Token
13+
router.get('/token', AuthController.getToken);
14+
};

app/internal/adapters/routes/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './main.routes';
22
export * from './users.routes';
3+
export * from './auth.routes';

app/internal/adapters/routes/users.routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { UsersController } from "../../core/controllers";
88
export const UsersRoute = (app) =>{
99
const router = express.Router();
1010
app.use('/users', router);
11-
11+
1212
//List by id
1313
router.get('/:id', UsersController.getById);
1414
// Create a new user
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AuthServices } from '../services';
2+
import { constants } from 'http2';
3+
4+
/**
5+
* Callback function for performing a CRUD.
6+
* @param {import('express').Request} req - Express request object.
7+
* @param {import('express').Response} res - Express response object.
8+
* @param {import('express').NextFunction} next - Express next middleware function.
9+
* @function getToken - Obtain the JWT token.
10+
*/
11+
export const AuthController = {
12+
getToken: (req, res, next) => {
13+
try {
14+
const token = AuthServices.getToken(req.body.admin_id, req.body.expires_in);
15+
res.status(constants.HTTP_STATUS_OK).json(token);
16+
} catch (err) {
17+
next(err);
18+
}
19+
},
20+
};
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './main.controller';
2-
export * from './users.controller';
2+
export * from './users.controller';
3+
export * from './auth.controller';

app/internal/core/domain/models/responses.models.js

+22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ const simple = (message) => ({
1313
message,
1414
});
1515

16+
/**
17+
* Represents a token response object.
18+
* @typedef {Object} TokenResponse
19+
* @property {string} access_token - The token created.
20+
* @property {string} expires_in - The expiration time of the toke.
21+
* @property {string} token_type - The token type for the request, by default is Bearer.
22+
*/
23+
24+
/**
25+
* Creates a token response object.
26+
* @param {string} token - The token created with the expiration time.
27+
* @property {string} expires_in - The expiration time of the token.
28+
* @returns {TokenResponse} The token response with the information about the token created.
29+
*/
30+
31+
const tokenResponse = (token, expires_in) =>({
32+
access_token: token,
33+
expires_in: expires_in,
34+
token_type: "Bearer"
35+
});
36+
1637
export const Responses = {
1738
simple,
39+
tokenResponse,
1840
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defaultConfig } from '../providers';
2+
3+
export const configurations = (req, res, next) => {
4+
req.config = defaultConfig;
5+
next();
6+
};
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './logs';
22
export * from './error';
3+
export * from './configurations';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'dotenv/config';
2+
3+
export const defaultConfig = {
4+
security_key: process.env.SECRET_KEY,
5+
};

app/internal/core/providers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './config.provider';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Domain } from '../domain';
2+
import { Token } from '../../adapters/auth';
3+
4+
const {
5+
Models: { Responses },
6+
} = Domain;
7+
8+
/**
9+
* Get Token response.
10+
* @returns {import('../domain/models/responses.models.js').TokenResponse} The getByID response.
11+
*/
12+
export const AuthServices = {
13+
getToken:(admin_id, expires_in) =>{
14+
const token = Token.generateToken(admin_id, expires_in);
15+
return Responses.tokenResponse(token, expires_in);
16+
},
17+
};

app/internal/core/services/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './main.services';
2-
export * from './users.services'
2+
export * from './users.services';
3+
export * from './auth.services';

app/internal/core/services/users.services.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {UserModel} from '../models';
21
import { Domain } from '../domain';
32

43
const {

app/server/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import bodyParser from 'body-parser';
22
import cors from 'cors';
33
import express from 'express';
44
import { Adapters } from '../internal/adapters';
5-
import { Logger, HandlerError } from '../internal/core/middlewares';
5+
import { VerifyToken } from '../internal/adapters/auth';
6+
import { Logger, HandlerError, configurations } from '../internal/core/middlewares';
67

78
const { Routes } = Adapters;
89

@@ -11,8 +12,11 @@ App.use(cors());
1112
App.use(bodyParser.json());
1213
App.use(bodyParser.urlencoded({ extended: false }));
1314
App.use(Logger);
15+
App.use(configurations);
16+
App.use(VerifyToken);
1417

1518
Routes.MainRoute(App);
19+
Routes.AuthRoute(App);
1620
Routes.UsersRoute(App);
1721

1822
App.use(HandlerError);

0 commit comments

Comments
 (0)