注册后,Passport不会保存用户,也不会引发错误

时间:2019-05-31 19:19:14

标签: node.js express passport.js bcrypt passport-local

我正在使用nodejs,express和mongoDB建立在线商店。 我正在使用通行证和bcrypt进行身份验证,但这并没有保存用户,也没有引发任何错误

在过去的几天中,我已经多次检查并重新检查我的代码,但均未成功,并且Google找不到我的问题的答案。

https://github.com/Tototomi258/WorkUp-Store在这里您可以检查整个项目,但是这里有一些代码:

app.js

const serverConfig = {
    entry: { main: './src/server/index.js' },
    target: 'node',
    output: {
        path: path.resolve(__dirname, 'server'),
        filename: 'lib/server.js',
        libraryTarget: 'commonjs2'
    },
    devtool: 'cheap-module-source-map',
    module: {
        rules: [
            {
                test: [/\.svg$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
                use: {
                    loader: 'file-loader',
                    options: {
                        name: '[name].[hash].[ext]',
                        outputPath: 'media'
                    }
                }
            },
            {
                test: /\.s?css$/,
                use: [ MiniCSSExtractPlugin.loader, 'css-loader' ]
            },
            {
                test: /\.(js|jsx)$/,
                include: [
                    path.resolve(__dirname, 'src/server'),
                    path.resolve(__dirname, 'src/shared')
                ],
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    },
    plugins: [
        new MiniCSSExtractPlugin({
            filename: './styles/styles.css'
        }),
        new HtmlWebpackPlugin({
            template: './src/server/index.html'
        })
    ],
    externals: ['express']
};

passport.js

mongoose.connect('mongodb://localhost:27017/shopping', { useNewUrlParser: true });
mongoose.set('useCreateIndex', true);
require('./config/passport')(passport);

app.use(session({secret: 'secretPassword', resave: false, saveUninitialized: false}))
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());

user.js

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

module.exports = function(passport) {
  passport.serializeUser((user, done) => {
    done(null, user.id);
  });

  passport.deserializeUser((id, done) => {
    User.findById(id, (err, user) => done(err, user));
  });

  passport.use('local-signup', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  }, (req, username, password, done) => {
    process.nextTick(function() {
      User.findOne({'email' : email}, (err, user) => {
        if (err) {return done(err);}
        if (user) {return done(null, false, {message: 'El email ya está en uso'});}

        const newUser = new User();
        newUser.local.username = email;
        newUser.local.password = newUser.encryptPassword(password);
        newUser.save((err, result) => {
          if (err) {return done(err);}

          return done(null, newUser);
        });
      });
    });
  }));
}

index.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcryptjs = require('bcryptjs');

const UserSchema = new Schema({
  username: {type: String, required: true, unique: true},
  password: {type: String, required: true}
});

UserSchema.methods.encryptPassword = (password) => {
  return bcryptjs.hashSync(password, bcryptjs.genSaltSync(8), null);
}

UserSchema.methods.validPassword = (password) => {
  return bcryptjs.compareSync(password, this.local.password);
}

module.exports = mongoose.model('User', UserSchema);

正如我所说,这应该将用户保存到数据库中(或至少引发错误),但事实并非如此。

谢谢您的时间!

0 个答案:

没有答案