A short snippet to assist in linking of social media accounts in a userschema when using mongoDB & mongoose.js async function generateOrFindUser(accessToken, refreshToken, profile, done) { const email = profile.emails[0].value; const name = profile.displayName || profile.username; const facebookId = profile.id; if (email) { let updatedUser = await User.findOneAndUpdate({facebookId},{email,name,facebookId}, {‘upsert’: true, ‘new’: true}, done); } else { var emailError = new Error(“Your email privacy settings prevent
Sweet & Sour mongoose.js methods – 2
This is part of the ‘Sweet & Sour mongoose.js methods’ series. In this function we will write a method that acts upon the UserSchema to check whether an email OR username already exists in the database. As an example use-case, this method can then be used in the any of your routes files by exporting the “User” to check whether an email address already exists
Sweet & Sour mongoose.js methods – 1
I’ve been using Mongoose (ODM library for MongoDB) quite a bit for the authentication project. NoSQL databases like MongoDB do seem more flexible for web apps which depend quite a bit on user interaction. Example a user might decide to login with a social media account or register with their email, a password and secret question-answer. I find it way easier to write a mongoose