简体   繁体   English

当我输入enctype =“multipart / form-data”和输入[type =“file”]时,使用PassportJS“丢失凭据”

[英]“Missing credentials” with PassportJS when I put enctype=“multipart/form-data” and an input[type=“file”]

THE PROBLEM 问题

I've got a problem, when I use enctype="multipart/form-data" to my form (with an upload (.jpg/.png, etc.)) and I sumbit it, my flash displays "Missing credentials", without precisions. 我有一个问题,当我使用enctype =“multipart / form-data”到我的表单(上传(.jpg / .png等))并且我对它进行汇总时,我的flash显示“Missing credentials”,没有精确。 So how do I fix this ? 那么我该如何解决这个问题呢?

What I've tried 我试过的

I searched solutions, but no one answered my problem. 我搜索了解决方案,但没有人回答我的问题。 So I tried to resolve this, during 1-2 hours. 所以我尝试在1-2小时内解决这个问题。 I've found nothing. 我一无所获。 I asked too in another forum (alsacreations.com, if you know) 我也在另一个论坛上问过(alsacreations.com,如果你知道的话)

What I know, it is NOT a problem with my config/passport.js because otherwise, in my console, I would have logs. 我所知道的,这不是我的config / passport.js的问题,因为否则,在我的控制台中,我会有日志。

Some code 一些代码

[2] GET /api/auth/user 304 0.621 ms - -
[2] GET /api/auth/user 304 0.816 ms - -
[2] GET /favicon.ico 200 0.072 ms - 2780
[2] GET /assets/dist/app.js 304 1.433 ms - -
[2] GET /signup 304 2.842 ms - -
[2] POST /api/auth/user 200 5.250 ms - 2
[2] POST /signup 302 0.951 ms - 58
[2] GET /signup 200 3.036 ms - 3596
[2] GET /assets/styles/main-css.css 304 0.919 ms - -
[2] GET /assets/dist/main-js.js 304 2.542 ms - -
[2] GET /assets/dist/app.js 304 1.797 ms - -
[2] GET /assets/config/config-en.json 304 0.720 ms - -
[2] GET /assets/dist/manifest.json 304 0.584 ms - -
[2] GET /api/auth/user 200 0.529 ms - 5
[2] GET /api/auth/user 304 1.049 ms - -
[2] GET /assets/dist/app.js 304 0.994 ms - -
[2] GET /signup 200 4.576 ms - 3376

As you see it there's no log... 如你所见,没有日志......

Let's the demo! 我们来试试吧!

https://forum.alsacreations.com/upload/1559134328-67719-alsa.png https://forum.alsacreations.com/upload/1559134328-67719-alsa.png

  • I've a form (with enctype="multipart/form-data", etc.) 我有一个表单(enctype =“multipart / form-data”等)

https://forum.alsacreations.com/upload/1559134431-67719-alsa.png https://forum.alsacreations.com/upload/1559134431-67719-alsa.png

  • I upload my picture (as it happens an avatar) 我上传了我的照片(因为它发生了一个头像)

https://forum.alsacreations.com/upload/1559134534-67719-alsa.png https://forum.alsacreations.com/upload/1559134534-67719-alsa.png

  • BUT (else it'll be too easy...) When I upload it, I've got this message ("Missing credentials"), is there any callback that I've not caught? 但是(否则它会太容易了......)当我上传它时,我收到了这条消息(“缺少凭证”),有没有我没有收到的回调? (I wanna have more informations) (我想要更多的信息)

Thanks for your help. 谢谢你的帮助。

(Sorry I can't place images, I'm under the 10 points of reputations, lol) (对不起,我不能放置图片,我在10点声誉,哈哈)

Before you handle logic of upload image, have you checked user is logged in or not? 在处理上传图像的逻辑之前,您是否检查过用户是否已登录? By req.isAuthenticated() like this: 通过req.isAuthenticated()像这样:

app.get('/some_path', checkAuthentication, function(req,res) {
    // your logic upload files
});

function checkAuthentication(req,res,next) {
    if(req.isAuthenticated()) {
        //req.isAuthenticated() will return true if user is logged in
        next();
    } else{
        res.redirect("/login");
    }
}

Yes I did! 是的,我做到了!

You can see too in my github: https://github.com/Soldat8889/psearch/tree/develop in app folder, routes and signupRouter.js 您也可以在我的github中看到: https//github.com/Soldat8889/psearch/tree/develop in app folder,routes和signupRouter.js

module.exports = {
    get: (req, res, _Template) => {
        fs.readFile(`public/config/config-${req.cookies.lang}.json`, 'utf-8', (e, data) => {
            if(e) {
                // REDIRECT
                res.redirect('/lang-select');
                return;
            }

            if(req.isAuthenticated()) {
                // REDIRECT
                res.redirect('/dashboard');
                return;
            }

            res.render('main', {
                title: JSON.parse(data)['title']['signup'],
                description: JSON.parse(data)['description']['signup'],
                lang: req.cookies.lang,
                url: `${req.protocol}://${req.get('host')}${req.originalUrl}`,
                env: nodeEnv,
                mainCSS: _Template.mainCSS,
                mainJS: _Template.mainJS,
                appJS: _Template.appJS,
                params: {
                    email: req.session.email,
                    username: req.session.username,
                    errorTarget: req.session.errorTarget
                }
            });
        });
    },
    authenticate: passport.authenticate('local-signup', { successRedirect: '/dashboard', failureRedirect: '/signup', failureFlash: true })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么ajax上传文件不需要form标签中的enctype="multipart/form-data"? - Why ajax upload file doesn't need enctype=“multipart/form-data” in the form tag? jQuery Form插件:enctype:multipart / form-data和文件上传-没有JSON返回? - jQuery Form Plugin: enctype:multipart/form-data and file-upload - no JSON returning? enctype="multipart/form-data" 总是返回 null 输入,但它是 multer 必须工作的,我们如何解决这个问题? - enctype=“multipart/form-data” always return null input but it is a must for multer to work how can we fix this? 如何使用AJAX使用enctype =“multipart / form-data”提交表单? - how to submit Form with AJAX Using enctype=“multipart/form-data”? 当表单设置为 enctype='multipart/form-data' 时,express-validator 给出未定义的值 - express-validator giving undefined value when form set to enctype='multipart/form-data' 当enctype =“ multipart / form-data”(Node.js)时,无法读取Jade Form字段 - Unable to read a jade form field when it has enctype=“multipart/form-data” (Nodejs) 如何在jQuery POST中将enctype作为multipart / form-data发送 - How to send enctype as multipart/form-data in jquery POST 为什么通过表单发送数据时为什么需要包含enctype =“ multipart / form-data”但通过xmlhttprequest发送时不需要 - why do I need to include enctype=“multipart/form-data” when sending data through form but don't need when sending through xmlhttprequest 我如何在angularjs中使用$ http.post()在post方法中使用enctype =“ multipart / form-data”发送表单数据 - how can i send the form data with enctype=“multipart/form-data” in the post method using $http.post() in angularjs 使用XMLHttprequest上传文件 - 在multipart / form-data中缺少边界 - Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM