Skip to content

Commit 78411dd

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

File tree

28 files changed

+680
-60
lines changed

28 files changed

+680
-60
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SECRET_KEY = "secret_key_value"

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SECRET_KEY = "secret_key_value"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
coverage/
33
dist/
44
.vscode/
5+
.env

README.md

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './jwt';

app/internal/adapters/auth/jwt.js

Lines changed: 24 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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
};
Lines changed: 14 additions & 0 deletions
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+
};
Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)