- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个 Node/快速服务器,我决定使用 Passport-local 与本地 MongoDB 服务器一起进行用户身份验证。我使用 html 表单中的 POST 方法来发送两个项目:电子邮件和密码(我更改了 LocalStrategy 实例中的 usernameField 项目)。使用 console.log,我看到了 Mongo 和 Passport 的所有正确且预期的行为。但是,页面永远不会使用 successRedirect 或 failureRedirect 进行重定向。看起来 html 页面 (Login.html) 刚刚刷新。有趣的是,根据我决定如何从表单发出 POST 请求,我会得到不同的行为。最初,我使用 jQuery 和 $('#id').submit() 方法在 '/students' 处调用 $.post 请求,并且观察到了上述行为。但是,当我摆脱这个脚本并用于发送请求时,没有调用任何中间件(再次,我使用 console.log 确认了这一点),并且页面立即重定向到我的 failureRedirect url,无论我是否输入了有效的表单中的数据。
注意:“学生”是我的 Mongoose 模型的名称,问题不在于此
我已经尝试过的事情:1. 在我的 Passport.authenticate 中使用自定义回调并使用 res.redirect。事实上,我在这个函数中放置了一个 console.log 来查看它是否被调用,确实如此。但 res.redirect 完全不起作用。2. 摆脱我的自定义 usernameField 和 passwordField 并仅使用默认值。我观察到完全相同的行为。3. 使用 jQuery .click() 方法而不是 .submit() 方法。我以为提交表单与刷新页面有关,但这不起作用。
我认为可能有帮助但我不知道如何实现:1.更改我所有服务器端Javascript的顺序(也许有些东西没有被正确调用)2.我在某处看到使用了passport.authorize而不是passport.authenticate
在 app.js 中
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
(username, password, done) => {
console.log(username, password);
Student.findOne({ email: username, password:password }, function(err, user) {
console.log(user); //this works as expected
console.log(!user); //this works as expected
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (user.password != password) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
Student.findById(id, function(err, user) {
done(err, user);
});
});
app.post('/students', passport.authenticate('local', {
successRedirect:'/public/Schedule.html',
failureRedirect:'/'
}));
在Login.html的script标签中
$('#loginForm').submit(() => {
let currentUser = {
email: $('#email').val(),
password: $('#password').val()
}
checkForUser(currentUser);
});
function checkForUser(user) {
$.post('http://localhost:9000/students',user, (data) => {
console.log(data);
})
}
最佳答案
来自网页中 Javascript 的 Ajax 调用不会根据 http 响应自动重定向。自动重定向是指不直接从 Javascript 调用服务器的情况,例如单击链接、提交表单(不使用 Javascript)、在 URL 栏中输入 URL 等...
相反,当从 Javascript 发送请求时(正如您的请求所示),Javascript 会返回响应,并且该响应可供您的 Javascript 使用。如果您看到它是 302 并且想要重定向,那么您可以获取位置 header 并将 window.location
设置为该重定向 URL 以使页面重定向。
关于javascript - 我的express.js和passport.js中间件都正常工作并且正在被调用,但是页面没有重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553845/
已经为此工作了几个小时,非常令人沮丧...... router.post('/', passport.authenticate('local-signup', function(err, user,
我正在寻找一种方法让我的客户端使用 facebook JS SDK 进行授权,然后以某种方式将此授权传输到我的 Node 服务器(以便它可以使用 fb graph api 验证请求) 我偶然发现: h
passport.socketio 抛出此错误,同时无法授权用户。 Error: Error: Failed to deserialize user out of session 我已将问题范围缩小到
passport.js 函数passport.serializeUser 和passport.serializeUser 有什么作用?这是维基百科中描述的序列化示例:http://en.wikiped
现代 Passport 和身份证的底部带有machine-readable zone (MRZ),其中包含基本标识信息(可能是OCR友好格式)。 机读区的格式指定了check digits的数量,可帮
我尝试对我的应用程序实现 passport 身份验证策略,但在尝试登录后不断收到以下错误: TypeError: passport.authenticate is not a function at
我正在将 passport 与 loopback 集成,并且工作正常,问题是如何获取访问 token 。 我有一个 web 应用程序(服务于与环回不同的服务器)所以: 发出请求(在环回后端) 这会将我
我使用带有passport-saml 策略 的 Passport 。在策略 上,有一个我想使用的函数。我知道策略是这样使用的: const SamlStrategy = require('passpo
这是我的套接字配置: // set authorization for socket.io io.set('authorization', passportSocketIo.authorize
我是 Passport.js 的新手。我一直在关注以下博客来注册用户 https://scotch.io/tutorials/easy-node-authentication-setup-and-lo
Passport 似乎是简单例份验证、不显眼且不难设置的绝佳选择。我正在构建一个使用 JWT 进行身份验证的 MEAN 堆栈,因此我查看了 Passport JWT。然而,有几件事我很困惑。 1) 假
我已经开始开发一个小项目,在客户端使用 React 和 Redux,后端是用 Node、Express 和 Passport.js 完成的我将尽力描述我几个小时以来所苦苦挣扎的事情。身份验证后,当用户
我正在使用 Passport 本地策略和 Passport Mongoose 本地来创建用户并检查用户身份验证。但是,我想探索使用电子邮件而不是用户名的选项。我按照文件的规定进行操作,但我未经授权。有
我正在使用 Passport 本地策略进行身份验证。在我的快速服务器中,我收到一个注册帖子请求,我应该为新用户将密码保存到数据库。但是我需要在保存到数据库之前对密码进行哈希处理。 但我不确定如何对其进
我正在将 node 与 express + mongoose 一起使用,并尝试将 passport.js 与 restful api 一起使用。 身份验证成功后,我不断收到此异常(我在浏览器上看到回调
我正在尝试在 JWTStrategy 中检查列入黑名单的 JWT token 。 jwtFromRequest 没有采用异步函数,所以我无法在那里检查它。 validate 函数提供对 JWT 负载而
目前,我正在通过 Google 构建登录功能,但我遇到了一些让我感到困惑的问题。 我们可以在一个项目中同时使用 Restful API 和 Graphql API 吗?除了谷歌身份验证,我们需要一些路
我正在使用与 express 配合良好的 Passport 本地策略: passport.use(localStrategy); passport.serializeUser((user, done)
我正在尝试通过passport-facebook 对用户进行身份验证,并且发生了一件非常奇怪的事情,我真的不确定。定义我的路线时,我有这行代码 app.get('/login/facebook',
当我使用 NPM 包“passport-azure-ad”尝试连接到 Azure AD 时,出现以下错误。我已成功连接到 Facebook、Google 和 MSFT Live,成功,但无法弄清楚为什
我是一名优秀的程序员,十分优秀!