DEV Community


#Help with passport and postgresql

Pedro Fumero
・1 min read

I have a question, I’m making an application that uses passportjs for authentication, on the official website to find the user the findUser method is used (which if I remember correctly belongs to mongoose and mongodb), but I’m using postgresql, the equivalent to findUser in postgresql what would it be?

Discussion (3)

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);

  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, 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.

lucretius profile image
Robert Lippens

If you store your user in postgresql, you could interact with your database with raw SQL queries or use an ORM to manage the interaction for you. I'd suggest putting all your database access behind a common interface so that if you swap to using a different database later on you don't have issues.

Basically, just make a db module with an exported function called "findUser" and in there you can implement the method by retrieving the user from the database using your ORM/raw SQL. Then import this db module where you want to call your passport auth and then call the function "findUser" that you have written.

faraazahmad profile image
Syed Faraaz Ahmad

You can also use sequelize by connecting it to your postgres db