【发布时间】:2021-09-20 15:03:38
【问题描述】:
我的目标: 让患者进行自我验证,然后使用 passport.js 通过更新他们现有的文件来注册患者。
我的结果: 我有它可以验证患者,但是当它注册密码和用户名护照时会创建一个全新的患者对象。
我的代码:
app.use(passport.initialize());
app.use(session({
secret:"secret",
resave: false,
saveUninitialized: false
}));
app.use(passport.session());
我的本地策略:
passport.use(Patient.createStrategy());
passport.serializeUser((patient, done) =>{
done(null, patient.id);
});
passport.deserializeUser((id, done) => {
Patient.findById(id, (err, patient) => {
done(err, patient);
})
});
我的架构:
const patientInfo = new mongoose.Schema({
username: String,
password: String,
accessCode: String,
dateOfBirth: String,
ssn: String,
firstName: String,
lastName: String,
contact: {
address: {
street: String,
city: String,
state: String,
zip: Number
},
phone: String,
email: String,
},
emergency: [{
name: String,
relation: String,
phone: String
}],
records: {
drNotes: [{
type: String,
date: String,
notes: String,
vitals: {
bpm: Number,
o2: Number,
bp: String,
hieght: String,
wieght: String
}
}],
techNotes: [{
type: String,
date: String,
notes: String,
image: {
data: Buffer,
contentType: String
}
}],
labs: [{
type: String,
date: String,
results: String
}],
medications: [{
type: String,
quantity: Number,
date: String
}]}
});
patientInfo.plugin(passportLocalMongoose);
我的发帖路线:
app.post('/register', (req, res) => {
let patientID = req.body.objID;
Patient.register(({username: req.body.username}), req.body.password, (err, u) =>{
if(err){
console.log(err);
}
Patient.findByIdAndUpdate({username: req.body.username}, (err, patient) => {
if(err){
console.log(err);
}
passport.authenticate('local')(req,res, () =>{
res.redirect('patient');
});
});
});
请帮忙!
【问题讨论】:
标签: node.js mongoose passport.js