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 you from signing in.");
done(emailError, null);
}
}
User schema:
const UserSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
lowercase: true
},
password: {
type: String,
required: true,
trim: true,
validate: validatePass
},
name: {
type: String,
unique: true,
required: true,
lowercase: true,
trim: true,
minlength: 4
},
phone: {
type: Number,
trim: true
},
question: {
type: String,
select: true
},
answer: {
type: String,
required: true,
trim: true
},
status: {
type: Boolean,
required: true
},
date: {
type: Date,
default: Date.now
},
facebookId: {
type : String
}
});
Top comments (0)