你好护照模块和快递有问题.
这是我的代码,我只想在第一次尝试时使用硬编码登录.
我总是得到这样的信息:
我搜索了很多,并在stackoverflow中发现了一些帖子,但我没有得到失败.
__CODE__
我的代码看起来像这样.
Error: failed to serialize user into session
at pass (c:\Development\private\aortmann\bootstrap_blog\node_modules\passport\lib\passport\index.js:275:19)
Run Code Online (Sandbox Code Playgroud) 抱歉,我对node.js很新,所以我还没有能够解决这个问题.我正在使用Node.js作为iPhone客户端的后端API服务器.我正在使用Passport.js通过本地策略进行身份验证.相关代码如下:
// This is in user.js, my user model
UserSchema.static('authenticate', function(username, password, callback) {
this.findOne({ username: username }, function(err, user) {
if (err){
console.log('findOne error occurred');
return callback(err);
}
if (!user){
return callback(null, false);
}
user.verifyPassword(password, function(err, passwordCorrect){
if (err){
console.log('verifyPassword error occurred');
return callback(err);
}
if (!passwordCorrect){
console.log('Wrong password');
return callback(err, false);
}
console.log('User Found, returning user');
return callback(null, user);
});
});
});
Run Code Online (Sandbox Code Playgroud)
和
// This is in app.js
app.get('/loginfail', function(req, res){
res.json(403, {message: 'Invalid username/password'});
});
app.post('/login',
passport.authenticate('local', …Run Code Online (Sandbox Code Playgroud) javascript authentication node.js passport-local passport.js
我正试图让我的Passport本地策略正常运行.
我已经设置了这个中间件:
passport.use(new LocalStrategy(function(username, password, done) {
//return done(null, user);
if (username=='ben' && password=='benny'){
console.log("Password correct");
return done(null, true);
}
else
return done(null, false, {message: "Incorrect Login"});
}));
Run Code Online (Sandbox Code Playgroud)
但是在这里
app.use('/admin', adminIsLoggedIn, admin);
function adminIsLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
Run Code Online (Sandbox Code Playgroud)
它总是失败并重定向到主页.
我无法弄清楚为什么会这样?为什么不进行身份验证?
在我的控制台中,我可以看到Password Correct正在打印.为什么它不起作用?
我试图在身份验证失败时传递JSON消息,在LocalStrategy中使用完成回调,但我得到的只是401和响应中的"未授权"字符串.
var express = require('express');
var bodyParser = require('body-parser');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var app = express();
app.use(bodyParser.json());
app.use(passport.initialize());
passport.serializeUser(function(user, done) {
done(null, user.email);
});
var strategy = new LocalStrategy({ usernameField: 'email' },
function (email, password, done) {
if (email === 'test@gmail.com' && password === 'pass') {
return done(null, { email: 'test@gmail.com' });
} else {
// never get this json object on the client side when posting invalid credentials
return done(null, false, { message: 'invalid …Run Code Online (Sandbox Code Playgroud) 我正在使用PassportJS和ExpressJS.
我需要更新登录的用户详细信息.虽然我在数据库中更新了这个,但是如何在会话中更新它,以便request.user包含更新的用户详细信息?
也就是说,在更新数据库之后,如何更新用户的会话信息?
我尝试直接分配更新的详细信息,request.user但它没有用.然后我尝试了request.session.passport.user- 这有效但是在request.user中更新之前还有大约5到10秒的延迟.
是否需要调用一个函数来更新存储在会话中的用户信息?或者是否有一些其他对象我可以更新,其中更改没有延迟
我正在尝试将护照整合到我的代码的登录表单中.客户端呼叫服务器端工作正常,直到我在请求中调用passport.authenticate,返回400 Bad Request.我在这里想念的是什么
HTML
<div>
<div class="row">
<div class="input-field col s12">
<input id="user-email" type="text" ng-model="user.email">
<label for="user-email">Your email address</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="user-password" type="password" ng-model="user.password">
<label for="user-password">Your password</label>
</div>
</div>
<div id="login-button-panel" class="center-align">
<button class="btn" id="login-btn" ng-click="vm.login(user);">Login</button>
</div>
<div class="section center">
<a class="modal-trigger">Forgot password?</a>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
$http.post('/api/login',user).success(function(result){
console.log(result)
})
Run Code Online (Sandbox Code Playgroud)
server.js
passport.use(new LocalStrategy(
function(username, password, done) {
return done(null, false, {message:'Unable to login'})
}
));
passport.serializeUser(function(user,done){
done(null,user);
});
passport.deserializeUser(function(user,done){
done(null,user);
});
app.post('/api/login', passport.authenticate('local'), function(req,res){ …Run Code Online (Sandbox Code Playgroud) 我在开发中使用带有护照的node/express.我看到一篇文章说:
Express加载会话数据并将其附加到req.由于passport将序列化用户存储在会话中,因此可以在req.session.passport.user中找到序列化的用户对象.
但令我惊讶的是,浏览器cookie中sessionID存储的值在登录前后保持不变.那么序列化用户对象存储在哪里?
我认为它最初存储在用户sessionidcookie中,但似乎并非如此,因为我仍然可以访问我的用户对象req.session.passport.user
现在已经工作了几个小时,非常令人沮丧......
router.post('/',
passport.authenticate('local-signup', function(err, user, info) {
console.log(err);
}), function(req, res){
console.log(req);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ a: 1 }));
});
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我使用了console.log输出{ message: 'Missing credentials' },这让我相信身体解析器没有正确解析正文消息.当我使用这条路线时......
router.post('/',
function(req, res){
console.log(req.body);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ a: 1 }));
});
Run Code Online (Sandbox Code Playgroud)
当我使用时console.log,输出为{password: 'password', email: 'email@email.com'},表示req.body变量设置正确且可用.
app.js
var express = require('express');
var app = express();
var routes = require("./config/routes.config");
var models = require("./config/models.config");
var session = require('express-session');
var bodyParser = require('body-parser');
models.forEach(function(model){
GLOBAL[model] = require('./models/'+model+".model");
});
var …Run Code Online (Sandbox Code Playgroud) 有没有办法允许用户使用他的密码,电子邮件和姓名注册本地策略?
我在网上找到的每个例子都只使用名称/密码或电子邮件/密码.
我还搜索了整个护照文档,但该文档根本没用.这只是一个充满例子的臃肿网站.
我只需要一份护照使用的函数,类和变量列表,并解释它们和它们的每个参数的作用.每个好的图书馆都有这样的东西,为什么我找不到护照?
以下是我的代码的关键部分:
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
//are there other options?
//emailField did not seem to do anything
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, email, password, done) {
//check if email not already in database
//create new user using "email" and "password"
//I want an additional parameter here "name"
}));
Run Code Online (Sandbox Code Playgroud)
护照真的那么有限吗?必须有办法做到这一点,对吧?
我只是在寻找解决方案,这使得验证电子邮件带有令牌,用于我在本地的身份验证.在miss.js中是否有一些节点的插件或组件可以让我验证更容易?或者我必须自己做?
我的控制器
exports.postSignup = function(req, res, next) {
req.assert('email', 'Email is not valid').isEmail();
req.assert('password', 'Password must be at least 4 characters long').len(4);
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if (errors) {
req.flash('errors', errors);
return res.redirect('/signup');
}
var user = User.build({
email: req.body.email,
password: req.body.password,
});
User
.find({ where: { email: req.body.email } })
.then(function(existingUser){
if (existingUser) {
req.flash('errors', { msg: 'Account with that email address already exists.' });
return res.redirect('/signup');
}
user
.save()
.complete(function(err){
if (err) return …Run Code Online (Sandbox Code Playgroud) passport-local ×10
passport.js ×10
node.js ×8
express ×6
javascript ×4
angularjs ×1
login ×1
token ×1