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?