简体   繁体   中英

I can't create multiple user in mongodb

so I am building an authentication system on my web with passport in MERN.

So everything works fine a register a user etc.

But when I try to re-register with different credentials it doesn't work.

Is there a way to fix that?

Here is my code:

const Strategy = require('passport-local').Strategy
const mongoose = require('mongoose')
const User = require('../models/user');
const bcrypt = require('bcryptjs')

const salt = bcrypt.genSaltSync(10);

const SignupStrategy = new Strategy ({ passReqToCallback:true, usernameField: 'email' }, function(req, email, password, done){ 

    User.findOne({email: req.body.email}).lean().exec((err, user) => {
        if (err) {
            return done(err, null);
        }

        if (!user) {
            const encryptedPassword = bcrypt.hashSync(password, salt);
            let newUser = new User({
                email,
                password: encryptedPassword,
                first_name: req.body.first_name,
                last_name: req.body.last_name,
                date: req.body.date
            })

            newUser.save((error, inserted) => {
                if (error) {
                    return done(error, null);
                }

                return done(null, inserted);

            })
        }   
        if (user) {
            return done("User already exist", null);
        }
    })

});

module.exports = SignupStrategy;

Nevermind, I was using the same MongoDB user to multiple projects and I had on multiple projects the same collection and that's why it didn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM