【发布时间】:2018-12-06 19:45:24
【问题描述】:
我在我正在开发的 MEAN 应用程序上使用 Auth0 进行用户身份验证。我遇到的问题是我已将模型、路由和控制器分离到单独的文件中。我正在关注 Auth0 教程以获取有关在何处使用 JWT 令牌身份验证的指导,但我不确定它在我的设置中属于什么位置。
checkJwt 属于哪里?
https://auth0.com/docs/quickstart/backend/nodejs/01-authorization
健身路由器
module.exports = function(app) {
var workouts = require('../controllers/workoutController');
// workout Routes
app.route('/api/workouts')
.get(workouts.getAllWorkouts)
.post(workouts.createWorkout);
app.route('/api/workouts/benchmarks')
.get(workouts.getBenchmarks);
app.route('/api/workouts/:workoutId')
.get(workouts.getWorkout)
.put(workouts.updateWorkout)
.delete(workouts.deleteWorkout);
};
对应的控制器
var mongoose = require('mongoose'),
Workout = mongoose.model('Workout');
exports.getAllWorkouts = function(req, res) {
Workout.find({}, function(err, workouts) {
if (err)
res.send(err);
res.json(workouts);
});
};
exports.getBenchmarks = function(req, res) {
Workout.find({
"type":"Benchmark"
}, function(err, workouts) {
if (err)
res.send(err);
res.json(workouts);
});
};
exports.createWorkout = function(req, res) {
var newWorkout = new Workout(req.body);
newWorkout.save(function(err, workout) {
if (err)
res.send(err);
res.json(workout);
});
};
exports.getWorkout = function(req, res) {
Workout.findById(req.params.workoutId, function(err, workout) {
if (err)
res.send(err);
res.json(workout);
});
};
exports.updateWorkout = function(req, res) {
Workout.findOneAndUpdate({_id: req.params.workoutId}, req.body, {new: true}, function(err, workout) {
if (err)
res.send(err);
res.json(workout);
});
};
exports.deleteWorkout = function(req, res) {
Workout.remove({
_id: req.params.workoutId
}, function(err, workout) {
if (err)
res.send(err);
res.json({ message: 'Workout successfully deleted' });
});
};
锻炼后()
exports.createWorkout = function(req, res) {
var newWorkout = new Workout(req.body);
newWorkout.save(function(err, workout) {
if (err)
res.send(err);
res.json(workout);
});
};
【问题讨论】: