【问题标题】:How to use existing user and update schema with authentication threw passport.js如何使用现有用户并通过身份验证更新架构抛出passport.js
【发布时间】: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


    【解决方案1】:

    你可以使用req.login

    app.post('/register', (req, res,next) => {
        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);
                }
    
                req.login(patient, function(error){
                    if(error) {
                      return next(error);
                     }
                     res.redirect('patient');
                });
            });
        });
    

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2018-01-17
      • 2015-08-28
      • 1970-01-01
      • 2013-11-02
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多