DEV Community

Discussion on: #Help with passport and postgresql

Collapse
 
dmfay profile image
Dian Fay

It depends on what you're using to talk to Postgres. Here's some boilerplate I use with Massive and bcrypt:

  const db = await massive(config.database);

  passport.serializeUser((user, done) => {
    done(null, user.user_id);
  });

  passport.deserializeUser((userId, done) => {
    db.users.findOne(userId).then(user => {
      if (!user) {
        return done(null, false);
      }

      return done(null, user);
    }).catch(done);
  });

  passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  }, async (username, password, done) => {
    const user = await db.users.findOne({email: username});

    if (user) {
      const match = await bcrypt.compare(password, user.password);

      if (match) {
        return done(null, user);
      }
    }

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

If you're not using Massive, you'd need to change each invocation involving db.