【问题标题】:JWT Auth for NodeJS and Auth0NodeJS 和 Auth0 的 JWT 身份验证
【发布时间】: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);
  });
};

【问题讨论】:

    标签: node.js jwt auth0


    【解决方案1】:

    首先,您应该在一个单独的文件中配置checkJwt(如docs)并在您的路由器文件中要求它。

    让我们看看如何在锻炼路由器中protect the routes

    module.exports = function(app) {
        var workouts = require('../controllers/workoutController');
        var checkJwt = require('./path/to/checkJwt');
    
        // workout Routes
        app.route('/api/workouts')
          .get(workouts.getAllWorkouts) // unprotected route
          .post(checkJwt, workouts.createWorkout); // protected route
    
        app.route('/api/workouts/benchmarks')
        .get(workouts.getBenchmarks);
    
        app.route('/api/workouts/:workoutId')
          .get(workouts.getWorkout)
          .put(workouts.updateWorkout)
          .delete(workouts.deleteWorkout);
    };
    

    函数checkJwt 是一个中间件,用于在到达控制器逻辑之前检查请求是否经过身份验证。

    checkJwt 文件:

    var checkJwt = jwt({
      ...
    })
    
    module.exports = checkJwt
    

    【讨论】:

    • 我想我更接近了。现在我得到“Route.post() 需要一个回调函数但有一个 [object Object]”
    • 我认为这是因为您在 checkJwt 文件中导出了一个对象而不是一个函数。我更新了我的答案,向您展示了 checkJwt 文件。
    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 2019-08-11
    • 2016-03-29
    • 1970-01-01
    • 2019-08-18
    • 2023-04-06
    • 2015-04-10
    • 2019-10-16
    相关资源
    最近更新 更多