나는 아래와 같이 각 API에 대한 JWT 인증을 구현 해요 :작성 가능 미들웨어에서 익스프레스 - JWT의 오류를 처리
auth.js
import expressJwt from 'express-jwt';
import compose from 'composable-middleware';
var validateJwt = expressJwt({
secret: config.secrets.session
});
function isAuthenticated() {
return compose()
.use(function(req, res, next) {
validateJwt(req, res, next);
})
.use(function(req, res, next) {
User.find({
where: {
id: req.user.id
}
}).then(function(user){
//Handle User
}).catch(function(err){
//Handle DB Error
});
});
}
하는 index.js
import auth from '../../auth';
import express from 'express';
import controller from './user_group.controller';
import * as validators from './user_group.validations';
// Create router object
const router = express.Router();
// Get all user groups
router.get('/', [auth.isAuthenticated(), validators.index], controller.index);
JWT의 오류 처리를 제외한 모든 기능이 완벽하게 작동합니다. 나는 함수를 이해하지 못한다. validateJwt(req, res, next);
그 다음 미들웨어로 이동하기 전에 Unauthorized Error stack
을 처리하는 방법.
후 다른 .use을 추가 할 수 있습니다, 그러나, 그것은 "예기치 않은 토큰 구문 에러"를 캡처 할 수 없습니다 ... – KiwenLau