basic jwt auth test

This commit is contained in:
Philipp Dormann 2020-11-25 19:47:17 +01:00
parent 8e8aa774bc
commit 3a84cc8ef5
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import { Request, Response, NextFunction } from "express";
// import bodyParser from 'body-parser';
// import cors from 'cors';
import * as jwt from "jsonwebtoken";
export default (req: Request, res: Response, next: NextFunction) => {
const token = <string>req.headers["auth"];
try {
const jwtPayload = <any>jwt.verify(token, "secretjwtsecret");
// const jwtPayload = <any>jwt.verify(token, process.env.JWT_SECRET);
res.locals.jwtPayload = jwtPayload;
} catch (error) {
console.log(error);
return res.status(401).send();
}
next();
};

10
src/routes/v1/test.ts Normal file
View File

@ -0,0 +1,10 @@
import { Router } from "express";
import jwtauth from "../../middlewares/jwtauth";
const router = Router();
router.use("*", jwtauth, async (req, res, next) => {
return res.send("ok");
});
export default router;