-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jwt auth created and added in every API CRUD operation
- Loading branch information
1 parent
f1a5fd8
commit 9484314
Showing
4 changed files
with
33 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import jwt from 'jsonwebtoken' | ||
import createError from 'http-errors' | ||
|
||
export function guard (req, res, next) { | ||
//sacar el TokenJWT de la cabecera, body o de la query | ||
const tokenJWT = req.get('Authorization') || req.body.jwt || req.query.jwt //para leer la cabeceza .get() | ||
// controlar si no hay token con error | ||
if (!tokenJWT) { | ||
next(createError(401, 'NO TOKEN PROVIDED 🆘')) | ||
return | ||
} | ||
// compruebo que es valido | ||
jwt.verify(tokenJWT, process.env.JWT_SECRET, (err, payload) =>{ | ||
if (err) { | ||
next(createError(401, 'Invalid Token JWT 🆘')) | ||
return | ||
} | ||
next() // si es valido lo dejo pasar | ||
} ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters