Node.js 如何使用Passport.js重置/更改密码
使用Passport.js重置/更改密码是一个相对复杂的过程,但在本文中,您将学会用一种非常简单易懂的方式来实现这个过程。
语法:
requiredUser.changePassword(oldpassword,
newpassword, callback function)
其中,requiredUser是我们要更改密码的用户。
方法: 我们将使用passport的changePassword方法,该方法接受两个参数和一个回调函数,第一个参数是旧密码,第二个参数是新密码。
requiredUser.changePassword(oldpassword,
newpassword, function(err) {
})
实施: 下面是上述方法的逐步实施过程。
让我们创建一个简单的Node.js应用程序,使用护照来注册用户,并使用mongoose将他们的数据保存在MongoDB中。您可以使用任何想要的方式存储用户数据,如MySQL或简单的数组。在本文中,我们将使用MongoDB。
在此应用程序内部,我们创建了一个 changepassword 路由,该路由接受用户输入的值,如用户名、旧密码和新密码。
步骤1:初始化NPM: 在终端中创建和定位您的项目文件夹,然后键入该命令。
npm init -y
它初始化我们的节点应用程序并创建一个package.json文件。
步骤2:安装依赖项: 将您的根项目目录定位到终端并输入命令。
npm install express body-parser mongoose passport passportLocalMongoose
安装 Express、Body Parser、Mongoose、Passport 和 Passport Local Mongoose 作为项目的依赖项。
步骤3:创建服务器文件: 创建一个名为 ‘app.js’ 的文件,在该文件中引入所有模块,并创建一个名为 ‘app’ 的常量来创建 express 模块的实例,然后使用 mongoose 连接本地的 MongoDB 数据库。
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require("mongoose");
const passport = require("passport");
const passportLocalMongoose
= require("passport-local-mongoose");
const app = express();
mongoose.connect(
"mongodb://localhost:27017/passport-forget", {
useNewUrlParser: true
});
步骤4: 现在初始化passport,创建一个用户模式,使用 passportLocalMongoose 作为用户模式的插件,并创建 User 常量,用于创建 User 的实例并将其保存在MongoDB数据库中。
app.use(passport.initialize());
const userSchema = new mongoose.Schema({
username: String,
password: String,
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
步骤5: 现在,让我们使用护照模块对用户进行序列化和反序列化。
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
步骤6: 为用户注册创建一个 register 路由,并创建一个 changepassword 路由,从用户那里接收用户名、旧密码和新密码的输入值。
app.get('/register', function (req, res) {
res.sendFile('register.html', {
root: __dirname
});
});
app.get('/changepassword', function (req, res) {
res.sendFile('changepassword.html', {
root: __dirname
});
});
步骤7: 现在,使用护照注册方法为用户创建一个 注册 的路由,该方法接受两个参数,即用户名和密码,以及一个回调函数,回调函数可以抛出错误或成功消息。
app.post('/register', function (req, res) {
User.register({
username: req.body.username
}, req.body.password, function (err) {
if (err) {
res.send(err);
} else {
res.send('successfully registered')
}
});
});
步骤8: 如我们在实施部分已经看到的,我们首先需要找到要更改密码的用户,然后使用具有两个参数的changePassword方法,即旧密码和新密码,并使用回调函数,如果有任何错误则发送错误信息,否则发送成功消息。
app.post('/changepassword', function (req, res) {
User.findByUsername(req.body.username, (err, user) => {
if (err) {
res.send(err);
} else {
user.changePassword(req.body.oldpassword,
req.body.newpassword, function (err) {
if (err) {
res.send(err);
} else {
res.send('successfully change password')
}
});
}
});
});
完整代码:
app.js
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require("mongoose");
const passport = require("passport");
const passportLocalMongoose
= require("passport-local-mongoose");
mongoose.connect(
"mongodb://localhost:27017/passport-forget", {
useNewUrlParser: true
});
const app = express()
app.use(passport.initialize());
const userSchema = new mongoose.Schema({
username: String,
password: String,
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
app.get('/register', function (req, res) {
res.sendFile('register.html', {
root: __dirname
});
});
app.get('/changepassword', function (req, res) {
res.sendFile('changepassword.html', {
root: __dirname
});
});
app.post('/register', function (req, res) {
User.register({
username: req.body.username
}, req.body.password, function (err) {
if (err) {
res.send(err);
} else {
res.send('successfully registered')
}
});
});
app.post('/changepassword', function (req, res) {
User.findByUsername(req.body.username, (err, user) => {
if (err) {
res.send(err);
} else {
user.changePassword(req.body.oldpassword,
req.body.newpassword, function (err) {
if (err) {
res.send(err);
} else {
res.send('successfully change password')
}
});
}
});
});
app.listen(3000);
注册.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="/register" method="post">
<input type="text"
placeholder="Username" name="username">
<input type="password"
placeholder="Password" name="password">
<button type="submit">Register</button>
</form>
</body>
</html>
changepassword.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="/changepassword" method="post">
<input type="text" placeholder="Username"
name="username">
<input type="password"
placeholder="Old Password"
name="oldpassword">
<input type="password"
placeholder="New Password"
name="newpassword">
<button type="submit">
Change Password
</button>
</form>
</body>
</html>
输出: