我正在尝试为Web应用程序构建后端,但是我被困在护照上。现在,我可以注册一个用户并登录该用户,但是我不能连续登录两次。每当调用反序列化用户时,我都会收到“ TypeError:无法读取未定义的属性'findOne'”,我看到该用户是未定义的,但是当我在login()和signup()上调用它时就定义了该用户。
var login = require('./login');
var signup = require('./signup');
module.exports = function (passport: any, user: any) {
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function (user: any, done: any) {
done(undefined, user.username);
});
// deserialize user
passport.deserializeUser(function (id: any, done: any) {
user.findOne(id).then(function (user: any) {
if (user) {
done(null, user.get());
} else {
done("error", undefined);
}
});
});
// Setting up Passport Strategies for Login and SignUp/Registration
login(passport, user);
signup(passport, user);
}
在我的app.ts文件中,我实现了以下策略:
var models = require("./config/models/User");
require('./config/passport/init')(passport, models.user);
signup.js
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
module.exports = function(passport : any, user : any) {
var User = user
passport.use('signup', new LocalStrategy(
{
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true // allows us to pass back the entire request to the callback
}, function(req: Request, username: string, password: string, done: any) {
var User = user
console.log("signup"+User)
var generateHash = function(password: string) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);
};
User.findOne({
where: {
username: username
}
}).then(function(user: any) {
if (user)
{
console.log('already taken')
return done(null, false, {
message: 'That email is already taken'
});
} else {
var userPassword = generateHash(password);
var data =
{
username: username,
password: userPassword,
firstname: req.body.firstname,
lastname: req.body.lastname
};
User.create(data).then(function(newUser: any, created: boolean) {
if (!newUser) {
return done(null, false);
}
if (newUser) {
return done(null, newUser);
}
});
}
});
}
));
}
login.js
import { passport } from '../../app'
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
module.exports = function(passport : any, user : any) {
//LOCAL SIGNIN
passport.use('login', new LocalStrategy(
{
// by default, local strategy uses username and password, we will override with email
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true // allows us to pass back the entire request to the callback
},
function(req : Request, username : string, password : string, done : any) {
var User = user;
console.log("login: "+User)
var isValidPassword = function(userpass : string, password : string) {
return bCrypt.compareSync(password, userpass);
}
User.findOne({
where: {
username: username
}
}).then(function(user : any) {
if (!user) {
return done(null, false, {
message: 'User does not exist'
});
}
if (!isValidPassword(user.password, password)) {
return done(null, false, {
message: 'Incorrect password.'
});
}
var userinfo = user.get();
return done(null, userinfo);
}).catch(function(err : Error) {
console.log("Error:", err);
return done(null, false, {
message: 'Something went wrong with your Signin'
});
});
}
));
}
答案 0 :(得分:0)
我自己解决了,
我这样导入模型:
var models = require("../models/User");
然后我像这样更改了findOne调用:
models.user.findOne({ where: {username: id} }).then(function (user: any) {
if (user) {
done(null, user.get());
} else {
done("error", undefined);
}
});