gpt4 book ai didi

node.js - Passport.changePassword 不是一个函数 - passor-local-mongoose

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:11 24 4
gpt4 key购买 nike

我在我的 React 应用程序中使用 passportpassport-local-mongoose 进行身份验证。登录和注册没有任何问题。但重置密码/更改密码无法正常工作。我有重置密码组件,如下

ResetPassword.js

var React = require("react");
var helpers = require("../utils/helpers");

class ResetPassword extends React.Component {
constructor(props){
super(props);
this.state = {
oldpassword: "",
newpassword: "",
confirmpassword: "",
usertype: "",
}
this.handleUserChange = this.handleUserChange.bind(this);
this.handleLogin = this.handleLogin.bind(this);
}

componentDidMount() {
helpers.getCurrentUser().then(function(response) {
if (response !== this.state.username) {
this.setState({
usertype: response.data.userType
});
}
}.bind(this));
}

handleUserChange(event) {
this.setState({ [event.target.name]: event.target.value});
}

handleLogin(event) {
// just in case we need it
// event.preventDefault();
}


render() {
return (
<div className="container">
<div className="row" id="loginForm">
<div className="col m6 offset-m3 s12">
<div className="card-panel">
<div className="row grey lighten-5">
<div className="col s12 center">
<h4 className="blue-text text-darken-1"><img id="logo" src="/assets/images/logo.png"/><span className="hide-on-med-and-down">Reset Password</span></h4>
</div>
</div>
<form action="/manager/reset-password" method="POST" onSubmit={this.handleLogin}>
<div className="row">
<div className="col s12">
<input
placeholder="Old Password"
type="password"
className="validate"
value={this.state.oldpassword}
name="oldpassword"
onChange={this.handleUserChange}
required />
</div>
</div>
<div className="row">
<div className="col s12">
<input
placeholder="New Password"
type="password"
className="validate"
value={this.state.newpassword}
name="newpassword"
onChange={this.handleUserChange}
required />
</div>
</div>
<div className="row">
<div className="col s12">
<input
placeholder="Confirm New Password"
type="password"
className="validate"
value={this.state.confirmnewpassword}
name="confirmpassword"
onChange={this.handleUserChange}
required />
</div>
</div>

<div className="row">
<div className="col s12">
<input
placeholder="userType"
type="hidden"
className="validate"
value={this.state.usertype}
name="usertype"
onChange={this.handleUserChange}
required />
</div>
</div>
<div className="row">
<div className="col s12">
<button className="btn waves-effect waves-light btn-large blue accent-3 loginButtons" type="submit" value="Submit" name="action">Submit<i className="material-icons right">send</i></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
);
}
};

module.exports = ResetPassword;

server.js文件

  var passport = require("passport");
var LocalStrategy= require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
//Passport
app.use(passport.initialize());
app.use(passport.session());

//change password (passport-local-mongoose)
app.post('/manager/reset-password', function(req, res, next){
passport.changePassword(req.body.oldpassword,req.body.newpassword, function(err) {
if (err){
return next(err)
}
else {
if (req.body.userType === "manager" || req.body.userType === "su") {
res.redirect("/manager");
} else {
res.redirect("/employee");
}
}
});
});

提交时抛出错误:

TypeError: passport.changePassword is not a function at C:\Users\admin\Documents\react_lms\server.js:212:14 at Layer.handle [as handle_request] (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\layer.js:95:5) at C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:275:10) at serveStatic (C:\Users\admin\Documents\react_lms\node_modules\serve-static\index.js:75:16) at Layer.handle [as handle_request] (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:317:13) at C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:275:10) at jsonParser (C:\Users\admin\Documents\react_lms\node_modules\body-parser\lib\types\json.js:101:7) at Layer.handle [as handle_request] (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:317:13) at C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\index.js:275:10) at textParser (C:\Users\admin\Documents\react_lms\node_modules\body-parser\lib\types\text.js:60:7) at Layer.handle [as handle_request] (C:\Users\admin\Documents\react_lms\node_modules\express\lib\router\layer.js:95:5)

原因是什么?怎么解决呢?

package.json

{
"name": "react-shift-scheduler",
"version": "1.0.0",
"description": "Employee management system",
"main": "server.js",
"scripts": {
"build": "webpack -p --progress --config webpack.config.js",
"start": "node server",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --progress -d --config webpack.config.js --watch"
},
"author": "HB,NC,AR,CS",
"license": "ISC",
"dependencies": {
"axios": "^0.15.3",
"bluebird": "^3.4.6",
"body-parser": "^1.15.2",
"dotenv": "^4.0.0",
"express": "^4.14.0",
"express-session": "^1.14.2",
"json-loader": "^0.5.7",
"loader-utils": "^1.1.0",
"moment-timezone": "^0.5.21",
"mongoose": "^4.7.3",
"morgan": "^1.7.0",
"nodemailer": "^4.6.8",
"passport": "^0.4.0",
"passport-google-auth": "^1.0.1",
"passport-google-oauth": "^1.0.0",
"passport-linkedin-oauth2": "^1.4.1",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.5.0",
"path": "^0.12.7",
"react": "^15.6.2",
"react-dom": "^15.6.2",
"react-live-clock": "^2.0.3",
"react-router": "^3.0.0"
},
"devDependencies": {
"babel-core": "^6.3.13",
"babel-loader": "^7.0.0-alpha.3",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"react-hot-loader": "^4.3.6",
"webpack": "2.1.0-beta.25"
}
}

最佳答案

由于 changePassword 是一种模式方法,因此它必须用于模型的实例,而不是模型本身或导入的 PassportLocalMongoose。

Admin.findById(req.user._id)
.then(foundAdmin => {
foundAdmin.changePassword(req.body.old, req.body.new)
.then(() => {
console.log('password changed');
})
.catch((error) => {
console.log(error);
})
})
.catch((error) => {
console.log(error);
});

关于node.js - Passport.changePassword 不是一个函数 - passor-local-mongoose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52437369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com