用户通过身份验证后使用multer上传文件

时间:2019-10-13 19:12:35

标签: node.js reactjs multer

使用multer,我可以从我的reactjs客户端成功将任何类型的文件成功上传到我的node.js后端,也可以创建一个用户。现在,我试图找出一种只允许经过身份验证的用户上传文件的方法。有人知道有什么资源可以帮助我弄清楚我需要做什么吗?

3 个答案:

答案 0 :(得分:1)

对于那些没有得到 req.body 的人,你可以像这样使用 Headers 传递你的令牌

const options = {
    headers: {'jwt': sessionStorage.getItem("token")}
};

和请求

axios.post("http://xxx", your data, options)

答案 1 :(得分:0)

使用mongodb数据库对用户进行身份验证。

答案 2 :(得分:0)

您可以在上传路线上使用任何身份验证中间件。 例如。

 const upload = multer({ dest: 'uploads/' })

   const authentication = function (req, res, next) {
   // handle authentication
    if(authenticated) return next()
    next({error})
   })


   app.post('/profile', authentication, upload.single('avatar'),
   function (req, res, next) {

   })

这将在multer处理请求之前对请求进行身份验证