0

I got a node.js app that connects to mongodb and tries to find a document in a collection. Unfortunately the call never completes.

This is the relevant part of my app.js, I get the console log connected to mongodb, but in the function signInComplete the UserS.findOne does nothing/does not call then.

var mongoose = require("mongoose");
//connect to mongodb
mongoose.connect(process.env.MONGO_DB_URI, { dbName: "newDb" }, () => {
  console.log("connected to mongodb");
});
const UserS = require("./models/user-model");

// Callback function called once the sign-in is complete
// and an access token has been obtained
async function signInComplete(iss, sub, profile, accessToken, refreshToken, params, done) {
  if (!profile.oid) {
    return done(new Error("No OID found in user profile."), null);
  }

  // Save the profile in user storage
  UserS.findOne({ msId: profile.oid }).then(currentUser => {
    if (currentUser) {
      //already have user
      console.log("user is:" + currentUser);
      done(null, currentUser);
    } else {
      //if not create new user
      new UserS({
        msId: profile.oid,
        profile,
        oauthToken
      })
        .save()
        .then(newUser => {
          console.log("new user created:" + newUser);
          done(null, newUser);
        });
    }
  });
}

My user model

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

const userSchema = new Schema({
    msId:String,
    profile:Object,
    oauthToken:Object
});

const User = mongoose.model('user',userSchema,"users");

module.exports=User;

signInComplete is called by passport

passport.use(new OIDCStrategy(
  {
    identityMetadata: `${process.env.OAUTH_AUTHORITY}${process.env.OAUTH_ID_METADATA}`,
    clientID: process.env.OAUTH_APP_ID,
    responseType: 'code id_token',
    responseMode: 'form_post',
    redirectUrl: process.env.OAUTH_REDIRECT_URI,
    allowHttpForRedirectUrl: true,
    clientSecret: process.env.OAUTH_APP_PASSWORD,
    validateIssuer: false,
    passReqToCallback: false,
    scope: process.env.OAUTH_SCOPES.split(' ')
  },
  signInComplete
));

I was trying the whole day to figure out what's wrong but what am I missing here?

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54
kcode
  • 1,220
  • 1
  • 18
  • 34
  • 1
    Can you post, how are you calling `signInComplete()` function as well – Shivam Dec 14 '19 at 17:14
  • signInComplete is called through passport. I added the code in the question. – kcode Dec 14 '19 at 17:39
  • Aren't you supposed to do `return done()` inside `findOne()` and after `save()` – Shivam Dec 14 '19 at 20:00
  • Even if I do `return done()` in all paths of `findOne()` it won't work. I `console.log()` every path as well and *none* is logged inside `findOne().then()`. – kcode Dec 14 '19 at 21:38

1 Answers1

0

I recreated the whole project and changed the way I connect to the DB

//Configure Database
mongoose.connect(process.env.MONGO_DB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

//Get the default connection
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

This way I finally got an error message, first:

MongoTimeoutError: Server selection timed out after 30000 ms

then after removing useUnifiedTopology: true

mongonetworkerror failed to connect to server

which the previous code did not give. It led me to the solution here: Error at connecting to MongoDb Atlas Server. So basically the issue was that my IP address had changed and it was not whitelisted in mongodb anymore.

kcode
  • 1,220
  • 1
  • 18
  • 34